In xNa, like a vector, a matrix is also defined as a struct to ensure its speed During computation. XNa only defines a 4x4 matrix. This may be different from some other 3D mathematical or 3D platforms. Although this may cause a waste of memory during system operation, there is only one matrix, which can greatly reduce the difficulty of development. In addition, the 4x4 matrix can store information such as object position, rotation, displacement, and scaling. (Most of the above are my own guesses)
When you look at the structure of a matrix, you can see some of the complex operators of the matrix at the beginning. From the perspective of the number of functions, the matrix definition seems simpler than the vector. ButAlgorithmIt is much more complex than vector. Fortunately, xNa encapsulates most matrix computing functions, so that we do not need to care about such complex algorithms. If you can understand those algorithms, it would be better. In this way, you will be able to understand what operations are behind the running of the 3D system and when the computing efficiency is the highest. What are the bottlenecks of system performance and how to improve them in the future. Well, I think it is necessary to study the underlying 3D mathematics.
BelowCodeIt is a number of operators in the xNa matrix.
Public static Matrix Operator-(matrix matrix1 );
Public static Matrix Operator-(matrix matrix1, matrix matrix2 );
Public static bool Operator! = (Matrix matrix1, matrix matrix2 );
Public static Matrix Operator * (float scalefactor, matrix );
Public static Matrix Operator * (matrix, float scalefactor );
Public static Matrix Operator * (matrix matrix1, matrix matrix2 );
Public static Matrix Operator/(matrix matrix1, float divider );
Public static Matrix Operator/(matrix matrix1, matrix matrix2 );
Public static Matrix Operator + (matrix matrix1, matrix matrix2 );
Public static bool operator = (matrix matrix1, matrix matrix2 );
The following code shows some attributes to obtain and set the sub-vectors of the matrix in the left and right directions.
Public vector3 backward {Get; set ;}
Public vector3 down {Get; set ;}
Public vector3 forward {Get; set ;}
Public vector3 left {Get; set ;}
Public vector3 right {Get; set ;}
Public vector3 translation {Get; set ;}
Public vector3 up {Get; set ;}
What are the meanings of these sub-vectors? This issue is hard to explain clearly. First, let's explain it intuitively. The most intuitive way is to try xNa again.ProgramDeclare a matrix to view the values of each attribute. First, define the following matrix in xNa.
11,12, 13,14
, 34
41,42, 43,44
When we track the code, the values of each attribute are obtained as follows:
Forward:-31,-32,-33
Backward: 31,32, 33
Left:-11,-12,-13
Right: 11,12, 13
Up: 21,22, 23
Down:-21,-22,-23
Translation:, 43
We know that the coordinate system used in xNa is the right-hand coordinate system. The coordinate system is as follows:
As shown in the figure, is forward equivalent to the inverse direction of the Z axis? Backward is equivalent to the positive direction of the Z axis. Right and Left are equivalent to the positive and negative directions of the X axis. Up and down are equivalent to the positive and negative directions of the Y axis. From this we can infer that the first three values of the 4x4 matrix in the first line, that is, the values 11, 12, and 13 are the components on the X axis, and the first three values in the second line are, 22, 23 is the value in the Y axis direction, and the first three values in the third row are 31,32, and 33 are the value in the Z axis direction. In this way, the geometric meaning of the 4x4 matrix seems to be clear. However, it is still unclear how to use these representatives. In my opinion, since xNa defines the values 11,12 and 13 as right, there must be some principles and usage. I cannot access the Internet now, so it is troublesome to query the information. I will explain it later.
In addition, xNa provides some other useful static functions, which provide great convenience for creating a matrix and hide the complexity behind the matrix. However, to understand the essence, we still need to look at these methods one by one. After creating the matrix, you can see what the values look like.
Rotate
Matrix mymatrix = matrix. createfromaxisangle (vector3.right, mathhelper. PI/6 );
The code above indicates that the rotation around the X positive axis is 30 °, and the system runs. Let's take a look at the obtained matrix value, as shown below:
1 |
0 |
0 |
0 |
0 |
0.866 |
0.5 |
0 |
0 |
-0.5 |
0.866 |
0 |
0 |
0 |
0 |
1 |
Let's figure out the formula for rotating the matrix on the X axis.
Three-dimensional rotation matrix around the X axis
The following is an example about the Y axis:
Matrix mymatrix = matrix. createfromaxisangle (vector3.up, mathhelper. PI/6 );
0.866 |
0 |
-0.5 |
0 |
0.5 |
1 |
0 |
0 |
0.5 |
0 |
0.866 |
0 |
0 |
0 |
0 |
1 |
Three-dimensional rotation matrix around Y axis
The following is an example of the Z axis
Matrix mymatrix = matrix. createfromaxisangle (vector3.backward, mathhelper. PI/6 );
0.866 |
0.5 |
0 |
0 |
-0.5 |
0.866 |
0 |
0 |
0.5 |
0 |
1 |
0 |
0 |
0 |
0 |
1 |
Formula for rotating the matrix around the Z axis
Next, let's take a look at the instances that rotate along any axis.
Matrix mymatrix = matrix. createfromaxisangle (New vector3 (1, 2, 3), mathhelper. PI/6 );
1 |
1.7679 |
-0.598 |
0 |
-1.2320 |
1.4019 |
1.3038 |
0 |
1.4019 |
3,3038 |
2.07179 |
0 |
0 |
0 |
0 |
1 |
Rotate along any matrix.
How do these formulas come from? There are complicated mathematical calculations. It's too difficult, but xNa has already helped us encapsulate it, so we can use it directly without worrying about complicated formulas and obscure formula derivation. But if you are interested, it would be nice to see it.
XNa also provides some fast rotation operations, such as rotating around the X axis and rotating around the Y axis. Definition:
Public static matrix createrotationx (float radians );
Public static matrix createrotationy (float radians );
Public static matrix createrotationz (float radians );
Zoom
Matrix mymatrix = matrix. createscale (2, 3, 4 );
The values of X, Y, and Z axes are 2, 3, and 4 times larger.
2 |
0 |
0 |
0 |
0 |
3 |
0 |
0 |
0 |
0 |
4 |
0 |
0 |
0 |
0 |
1 |
The formula is as follows:
Translation
Matrix mymatrix = matrix. createtranslation (2, 3, 4 );
1 |
0 |
0 |
0 |
0 |
1 |
0 |
0 |
0 |
0 |
1 |
0 |
2 |
3 |
4 |
1 |
It can be seen that translation mainly uses the fourth group of vectors.