Use of vector ry in game programming 5

Source: Internet
Author: User

<5> rotating an object
-Twinsen compiling

-My skills are limited, and mistakes are inevitable. Please give me some advice from mathematical experts and programming experts.
-My email-Address: popyy@netease.com

Welcome back! In this article, we will discuss the vector rotation, including Plane Point Rotation and space axis rotation. For game programmers, the rotation of vectors represents the key to manipulate the rotation of objects in the game, whether it is a plane genie, a set of Space Grids, or a camera that we place at a certain point in the 3-D world. We still need to use vectors to complete our journey, but this is not enough. We also need a friend, matrix, and Gool guy, which we use to linearly transform vectors. Just like what we did when we talked about vectors, let's review the coming mathematical knowledge. (I will only pass this part of the knowledge, because I will focus on the analysis of the rotation problem)

I. Basic OPERATIONS AND PROPERTIES OF MATRICES

For a 3x3 matrix (also known as a 3x3 matrix, a matrix with the same number of rows and columns is also called a matrix) M and M

1. matrix addition and subtraction

M + (-) M =

[A B c] [a B c] [A + (-) a B + (-) B C + (-) C]
[D e f] + (-) [d e f] = [d + (-) d e + (-) e f + (-) F]
[G h I] [g h I] [G + (-) g H + (-) h I + (-) I]

Nature:
1) Combination Law M + (m + n) = (m + M) + n
2) Exchange Law M + M = m + m

2. number multiplication Matrix

K x m =

[A B C] [kxa kxb kxc]
K x [d e f] = [kxd kxe kxf]
[G h I] [kxg KxH kxi]

Nature:

K and l are constants.
1) (K + l) x m = k x m + L x m
2) k x (m + M) = k x m + k x m
3) k x (L x m) = (k x L) x m
4) 1 x m = m
5) k x (m x m) = (k x m) x m = m x (k x m)

3. Matrix Multiplication

M x m =

[A B c] [a B C} [Axa + BXD + cxg AXB + bxe + cxh axc + BXF + CXI]
[D e f] X [d e f] = [DXA + EXD + fxg dxb + EXE + fxh dxc + exf + fxi]
[G h I] [g h I] [gxa + hxd + ixg gxb + hxe + ixh GXC + hxf + ixi]

It can be seen that the condition that the number of columns in the first matrix is equal to the number of rows in the second matrix.
According to the definition of matrix multiplication, matrix multiplication does not meet the exchange rate, that is, in general, m x m! = M x m.

Nature:
1) Combination Law (m x m) x n = m x (m x n)
2) multiplication addition allocation law m x (m + n) = m x m + m x n; (m + M) x n = m x n + m x n

4. transpose a Matrix

M' =
 
[A B C] '[a d G]
[D e f] = [B E h]
[G h I] [C f I]

Nature:
1) (m x m) '= M' x m'
2) (m') '= m
3) (m + M) '= M' + m'
4) (k x m) '= k x m'

5. Unit Matrix

[1 0 0]
E = [0 1 0] is called a 3-level unit array.
[0 0 1]

Properties: For any 3-level matrix m, e x m = m; m x e = m

6. Matrix Inversion

If there is m x m = e in the 3 x matrix, here E is a 3-level unit array, it can be said that M is reversible, and its inverse matrix is m, it is also recorded as m ^-1. On the contrary, it can be said that M is reversible, and the inverse matrix is m, which is also recorded as m ^-1.

Nature:
1) (M ^-1) ^-1 = m
2) (k x m) ^-1 = 1/k x m ^-1
3) (m') ^-1 = (M ^-1 )'
4) (m x m) ^-1 = m ^-1 x n ^-1

There are several algorithms for matrix inversion, which are not studied in depth here. We will discuss them when using them.
After we have established the matrix concept, we can use it for linear transformation of coordinates. Okay. Now let's start using it.

 

Ii. Basic 2-D rotation around the origin

The first is the simple 2-D vector rotation. Based on it, we will go deep into the complex 3-D rotation, and finally we can freely rotate in the 3-D.

In the 2-D dikar coordinate system, the rotation formula of a positional vector can be derived from the geometric meaning of the trigonometric function. For example, the position vector is shown.RThe condition of the clockwise rotation angle before and after B. In the left figure, we have a relationship:

X0 = |R| * Cosa
Y0 = |R| * Sina
=>
Cosa = x0/|R|
Sina = Y0/|R|

In the picture on the right, we have a relationship:

X1 = |R| * Cos (A + B)
Y1 = |R| * Sin (A + B)

(X1, Y1) is the point obtained after (x0, y0) Rotation Angle B, that is, the position vector.RThe last point. Expand cos (A + B) and sin (a + B) to obtain

X1 = |R| * (Cosacosb-sinasinb)
Y1 = |R| * (Sinacsb + cosasinb)

Now
Cosa = x0/|R|
Sina = Y0/|R|

Enter the formula above to obtain

X1 = |R| * (X0 * CoSb/|R|-Y0 * sinb/|R|)
Y1 = |R| * (Y0 * CoSb/|R| + X0 * sinb/|R|)
=>
X1 = x0 * CoSb-y0 * sinb
Y1 = x0 * sinb + y0 * CoSb

In this way, we get the counter-clockwise rotation formula of the vector around the dot in the 2-D dikar coordinate. Clockwise rotation changes the angle to negative:

X1 = x0 * Cos (-B)-y0 * sin (-B)
Y1 = x0 * sin (-B) + y0 * Cos (-B)
=>
X1 = x0 * CoSb + y0 * sinb
Y1 =-x0 * sinb + y0 * CoSb

Now I want to write this rotation formula in the form of a matrix. There is a concept I will briefly mention that each linear transformation in a plane or space (here it is a Rotation Transformation) corresponds to a matrix, it is called a transformation matrix. Linear transformation of a point is achieved by multiplying the matrix of the linear transformation. Okay, stop it, or you will be able to skip the question.

Therefore, the 2-D Rotation Transformation Matrix is:

[Cosa Sina] [cosa-Sina]
[-Sina cosa] or [Sina cosa]

The point rotation and transformation can be completed through a matrix. For example, the point I (x, y) rotates counter-clockwise around the origin:

[Cosa Sina]
[X, y] X [-Sina cosa] = [x * cosa-y * Sina x * SINA + y * cosa]

For programming convenience, we write it into two square arrays.

[X, y] [cosa Sina] [x * cosa-y * Sina x * SINA + y * cosa]
[0, 0] X [-Sina cosa] = [0 0]

It can also be written

[Cosa-Sina] [x 0] [x * cosa-y * Sina 0]
[Sina cosa] X [Y 0] = [x * SINA + y * cosa 0]

3. Any point of rotation of 2-D

Let's take a deeper look at another situation: Find a point to rotate around any non-origin center.

The formula we just exported is a formula that rotates around the origin, so to continue using it, we need to move the non-origin center around to the origin. According to this idea, we first move the center point to the origin point through a displacement vector, and the surrounding point must remain unchanged from the center point, and the corresponding displacement is based on this displacement vector, now, because the center point has been moved to the dot, we can use the formula above to calculate the position after the same displacement. After calculation, then let the calculated point follow the inverse displacement of the displacement vector to get a new position after a certain angle of rotation around the point center. See the following figure.


This is the new position after the blue point in the lower left rotates around the red point to a certain angle. Because the red points are not at the origin, you can use the red vector to move them to the origin. At this time, the Blue Points also follow this vector. As you can see, the relative positions of the red and blue points are not changed. Now the red points are at the origin, and the blue points can be rotated using the Rotation Transformation Matrix above. The rotated points return to the position after the red points are actually rotated using the inverse amount of the red vector.

In this process, we perform cubic linear transformation around the point: displacement transformation-Rotation Transformation-displacement transformation. We write it in the matrix form:

Set the red vector to (RTX, rty)

[X y 1] [1 0 0] [cosa Sina 0] [1 0 0] [x 'y'-]
[0 1 0] X [0 1 0] X [-Sina cosa 0] X [0 1 0] = [---]
[0 0 1] [RTX rty 1] [0 0 1] [-RTX-rty 1] [---]

The X' and y' of the final matrix are the coordinate points after our rotation.

Note that matrix multiplication satisfies the combination law: (m x m) x n = m x (m x n). We can first multiply all transformation matrices, that is

[1 0 0] [cosa Sina 0] [1 0 0]
M = [0 1 0] X [-Sina cosa 0] X [0 1 0]
[RTX rty 1] [0 0 1] [-RTX-rty 1]

And then let

[X y 1]
[0 1 0] x m
[0 0 1]

Merging transformation matrices like this is a common method for matrix operations, because after many transformation matrices are merged into one matrix, repeated transformation of a point or vector can be completed by multiplying a matrix, reducing the computing overhead.

This idea of "other transformations-rotating transformations around points-Other transformations" discussed in this section is very important, because sometimes complicated Rotation Transformations cannot be completed in one step, this method must be used to reduce the complexity of side-by-side attacks. Especially in 3-D space, some other necessary Rotation Transformations may need to be performed before the rotation of a specified degree, that is to say, we need to perform many rounds of rotation, but the overall idea is to divide complicated problems into several simple problems to solve, and a transformation matrix is required for every simple problem, therefore, we hope that readers will think deeply about this method.

Good. The 2-D rotation is fully discussed. Next, we will go to the 3-D space to discuss more complex rotations. Here we go!

 
4. Basic 3-D rotation around the coordinate axis

Like the 2-D rotation around the origin, the 3-D axis rotation is the basis of the 3-D rotation, because other complex 3-D rotation is eventually reduced to the axis rotation. In fact, the formula we just deduced for rotating O around the xoy coordinate plane can be easily extended to the 3-D space, because in the 3-D Cartesian coordinate system, the three coordinate axes are orthogonal to each other, therefore, the Z axis is perpendicular to the xoy plane. In this way, the O-point rotation on the xoy Plane actually rotates around the Z axis in the 3-D space, as shown in the left:

This figure describes how a point in the left-hand system rotates around the origin point on the xoy, yoz, and XOZ planes, and also rotates around the Z, X, and Y axes respectively. It can be seen that the coordinate axis rotation in the 3-D space is equivalent to the rotation around the origin point in the corresponding 2-D plane. We use matrices to describe:

SetP(X, y, z) is a point in a 3-D space. It can also be called a positional vector,PWhen the center axis around the point points out of your screen

PThe clockwise rotation angle A is written as follows:

[X y z 1] [cosa-Sina 0 0] [x y z 1] [cosa Sina 0 0]
[0 1 0 0] X [Sina cosa 0 0] and [0 1 0 0] X [-Sina cosa 0 0]
[0 0 1 0] [0 0 1 0] [0 0 1 0] [0 0 1 0]
[0 0 0 1] [0 0 0 1] [0 0 0 1] [0 0 0 1]

PThe clockwise rotation angle A is written as follows:

[X y z 1] [1 0 0 0] [x y z 1] [1 0 0 0]
[0 1 0 0] X [0 cos-Sina 0] and [0 1 0 0] X [0 cosa Sina 0]
[0 0 1 0] [0 sin cosa 0] [0 0 1 0] [0-Sina cosa 0]
[0 0 0 1] [0 0 0 1] [0 0 0 1] [0 0 0 1]

PThe Rotation Angle a around the Y axis is written as follows:

[X y z 1] [cosa 0 Sina 0] [x y z 1] [cosa 0-Sina 0]
[0 1 0 0] X [0 1 0 0] and [0 1 0 0] X [0 1 0 0]
[0 0 1 0] [-Sina 0 cosa 0] [0 0 1 0] [Sina 0 cosa 0]
[0 0 0 1] [0 0 0 1] [0 0 0 1] [0 0 0 1]

In the future, we will write them as such a standard 4x4 matrix, why? To facilitate translation and transformation, do you still remember to write the 2x2 square matrix as a 3x3 square matrix when performing translation in the previous section?

Let's continue our research. We will extend the conclusion to apply it to all the axes parallel to the coordinate axis, specifically, to apply it to all the axes parallel to the Y axis.
We can quickly think of this, we can follow the 2-D method "Translation Transform-rotation transform-Translation Transform" to do it.

To realize point rotation around axis, we need to move axis to the position that overlaps the Y axis according to a displacement vector, that is, convert axis to axis. In order to keep the relative positions of point and axis unchanged, point also uses the same displacement vector for corresponding displacement. Well, now the point after moving can be rotated around axis using the rotation matrix above, that is, the Y axis. After rotation, use the opposite displacement vector to shift to the actual position around the corresponding degree of axis. We still use matrices to describe:

Assume that axis is X = s, Z = T, and point (x, y, z) is used to rotate degrees a counterclockwise around it ", we have

[X y z 1] [1 0 0 0] [cosa 0 Sina 0] [1 0 0 0] [x 'y Z'-]
[0 1 0 0] [0 1 0 0] [0 1 0 0] [0 1 0 0] [---]
[0 0 1 0] X [0 0 1 0] X [-Sina 0 cosa 0] X [0 0 1 0] = [--]
[0 0 0 1] [-S 0-T 1] [0 0 0 1] [S 0 T 1] [---]

The obtained (x', Y, Z') is the position after the point rotates around axis.

Similarly, the X axis is parallel and the rotation angle a around the Y = s, Z = T counterclockwise is

[X y z 1] [1 0 0 0] [1 0 0 0] [1 0 0 0 0] [x y 'Z'-]
[0 1 0 0] [0 1 0 0] [0 cosa-Sina 0] [0 1 0 0] [---]
[0 0 1 0] X [0 0 1 0] X [0 Sina cosa 0] X [0 0 1 0] = [---]
[0 0 0 1] [0-s-t 1] [0 0 0 1] [0 s t 1] [---]

Converts the rotation angle A

[X y z 1] [1 0 0 0] [cosa-Sina 0 0] [1 0 0 0] [x 'y' Z-]
[0 1 0 0] [0 1 0 0] [Sina cosa 0 0] [0 1 0 0] [---]
[0 0 1 0] X [0 0 1 0] X [0 0 1 0] X [0 0 1 0] = [--]
[0 0 0 1] [-s-t 0 1] [0 0 0 1] [s t 0 1] [---]

The counter-clockwise rotation takes the corresponding counter-clockwise rotation transformation matrix introduced above. Now we have discussed all the basic rotations of the 3-D space. The next section is the highlights of our 3-D rotation, which is also the most powerful Rotation Transformation in the 3-D.


V. 3-D rotation around any axis


Wow! Finally, we come to the last part. In this section, we will comprehensively apply all the rotation knowledge involved above, complete the Rotation Transformation of a space point or a positional vector around the rotation axis of any direction in the space (one of the methods I will introduce below is a slightly tedious method, in general, it is a combination of several basic rotations. I will introduce a more advanced method in the next article ).

What is the rotation axis in any direction? It is actually a straight line of space. In space resolution ry, the two values that determine the Spatial Straight Line position areStraight LineAndDirection vector of a straight line. In rotation, we call this line a rotating axis. Therefore, this direction vector of a line is called its axial volume, which is similar to the axial volume of the Quaternary element in a 3-D animation. The transformation matrix before actual rotation needs to be obtained by moving this axial volume to the origin.

Let's first discuss how the rotation axis passes through the origin. So far, all we can do is rotate around the axis direction. Therefore, when we consider non-axis direction rotation, it is natural to think that we can change this axis of rotation to coincide with a certain axis, at the same time, in order to keep the rotation point and the relative position of this axis unchanged, the rotation point is also changed accordingly, and then the rotation point is rotated around the coordinate axis of the corresponding rotation axis. Finally, the rotated point and the rotation axis are converted back to the original position, at this point, the rotation around the non-axis is completed. Let's look at the graph analysis.

The figure contains an axial value of the red component (x0, y0, z0), and a blue position vector rotates around it, because this axial volume is not parallel to any coordinate axis, we have no way to use the Rotation Transformation Matrix derived above, so we must transform the axis to a coordinate axis. Here we select the Z axis. While changing the red axis, in order to keep the blue position vector unchanged from the relative position of the axis, the corresponding transformation is also made, and then the situation described in the figure appears. Then we can use the transformation matrix to rotate the corresponding degrees of the blue vector around the Z axis. After the rotation is complete, use the inverse transformation just transformed to restore the two vectors to the initial position unchanged, and then complete the rotation of a point around any axis that has passed the origin, we still use the "displacement transformation-Rotation Transformation-displacement transformation" Method for the axis at the origin point. We will discuss it later.

After understanding the basic idea, let's study the transformation! Let's change the red axis to the Z axis. Let's start!

First, we assume that the red axis vector is a unit vector, because in this way, sin and cos can be computed in a short time, and the axial vector can be standardized in actual programming. Then I want to change the red axis to the Z axis in two steps:

1) convert the red axis to the yoz plane
2) Change the red axis on the yoz plane to the Z axis.

As for the methods of these two transformations, I have no other way, but I can only rotate them. What do you think? First, rotate it to the yoz plane.

Let's change the axial rotation to (rotate around the Z axis ):

[Cosa Sina 0 0]
[-Sina cosa 0 0]
[0 0 1 0]
[0 0 0 1]

Then we asked for cosa and sina. From, we can see the orientation along the Z axis. We can see that the rotation axis vector is to the yoz plane. On the xoy plane, the rotation angle of the projection vector of the axis is a to the Y axis, now I don't know angle A, but we can use it to directly find cosa and Sina, because we know the relationship:

Cosa = Y0/the projection length of the axial volume on the xoy plane
Sina = x0/the projection length of the axial volume on the xoy plane

We set the projection length of the axial volume to LR = SQRT (x0 ^ 2 + y0 ^ 2). Now, the transformation matrix in step 1 is displayed:

[Y0/LR X0/LR 0 0]
[-X0/LR Y0/LR 0 0]
[0 0 1 0]
[0 0 0 1]

At the same time, we obtain the inverse transformation matrix:

[Y0/LR-X0/LR 0 0]
[X0/LR Y0/LR 0 0]
[0 0 1 0]
[0 0 0 1]

Next we will perform Step 2: Change the red axis on the yoz plane to the Z axis. Our transformation matrix is (rotating around the X axis ):

[1 0 0 0]
[0 CoSb sinb 0]
[0-sinb CoSb 0]
[0 0 0 1]

From the figure, this is the first rotation of the axial volume in the yoz plane, this time we require the CoSb and sinb in the above transformation, we still do not know the angle B, but we can still use it to calculate CoSb and sinb. Since the first rotation is around the Z axis, the Z component of the axial volume is not changed, or z0. In addition, the Y component of the axial volume is different from that of the original one. Let's look at the figure that is transformed for the first time. We can find that the axial volume is after it is rotated to the yoz surface, the Y component becomes the projection long LR of the axial volume on the xoy surface. Yes! I think it's time to write CoSb and sinb:

CoSb = z0/long axial volume
Sinb = LR/axial length

Do you still remember if we just assumed that the axial volume is a unit vector? So

CoSb = z0
Sinb = LR

So far our second transformation has come out:

[1 0 0 0]
[0 z0 LR 0]
[0-LR z0]
[0 0 0 1]

Corresponding inverse transformation matrix:

[1 0 0 0]
[0 z0-LR 0]
[0 LR z0]
[0 0 0 1]

To sum up, the transformation matrix for any point in the space to rotate around an axis in any direction and over the origin is:

[Y0/LR X0/LR 0 0] [1 0 0 0] [cosa Sina 0 0] [1 0 0 0] [Y0/LR-X0/LR 0 0]
[-X0/LR Y0/LR 0 0] [0 z0 LR 0] [-Sina cosa 0 0] [0 z0-LR 0] [X0/LR Y0/LR 0 0]
M = [0 0 1 0] X [0-LR z0 0] X [0 0 1 0] X [0 LR z0 0] X [0 0 1 0]
[0 0 0 1] [0 0 0 1] [0 0 0 1] [0 0 0 1] [0 0 0 1]

The preceding transform is the conversion group of "rotating transformation-rotating transformation. When we need to turn a vector at a certain position in the space around an axis rotation angle A, we can use the matrix corresponding to this vector to multiply this m, for example

[X y 0 0] [x 'y' Z'-]
[0 1 0 0] [---]
[0 0 1 0] x m = [---]
[0 0 0 1] [---]

Of course, the elements of the matrix in m are obtained based on the axial volume.

The transformation matrix above is obtained by changing the axial volume to the Z axis. It is first rotated to the yoz surface and then to the Z axis. We can also not do this, but first rotate the axial volume to the XOZ surface, and then rotate to the Z axis. In addition, we can also change the axial volume to X or Y axis, which can be determined by ourselves. Although the transformations are different, the derivation principle is the same, which is the penetration form of "other transformations-actual Rotation Transformations-Other transformations.

The analysis just now shows that the rotation axis is over the origin. For the general rotation axis, although we also put its axial volume to the origin for consideration, but we can't just let the rotation point rotate the axial volume around the origin, even if it is done, we still need to use the "Translation transformation-Rotation Transformation-translation transformation" method. That is, the rotation axis is first translated to the direction of the origin point, and the rotation point is also translated accordingly. Then, it is rotated according to the Transform Array introduced above, and finally the rotation axis and point are reversed. Here, we only need to add a translation transformation on both sides of M. The element of this translation transformation is obtained based on the distance vector between the axial volume and the origin. For example, if the distance vector between the rotation axis and the origin is (LX, Ly, LZ), then our transformation becomes

[1 0 0 0] [1 0 0 0]
[0 1 0 0] [0 1 0 0]
M = [0 0 1 0] x m x [0 0 1 0]
[-Lx-ly-LZ 1] [lx ly LZ 1]

Transformation matrix m is the combination of all seven transformation matrices, suitable for various rotating situations.

Now we have discussed the general 2-D and 3-D rotation. It can be seen that its basic idea can still be transformed and merged into a simplified one. The actual rotation is still using our most basic 2-D rotation formula around the origin. In fact, there are still a lot of Rotation effects that can be obtained by using the transformations and formulas above. For example, spiral rotation, rotation plus forward, random rotation, and so on. The next article will introduce one of the most advanced methods.

Trackback: http://tb.blog.csdn.net/TrackBack.aspx? Postid = 376948

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.