The Matrix structure in XNA is a 4x4 matrix, which looks something like this:
P1x P1y P1z w1 P2x P2y P2z w2 P3x P3y P3z w3 vx vy vz w4While the 16 elements can really be any information you like, matrices are generally used for transformations (and most - but not all - of those transformations are "affine" transformations). In the matrix above, P1, P2, and P3 would be points, and v would be a vector. For an affine transformation,the 3x3 matrix of points (P1, P2, and P3) would indicate transformations like scaling and rotation, while the vector v would represent translation. In an affine transformation, w1, w2, and w3 would be 0, while w4 would be 1.
For example, a matrix representing doubling the scale of an object (uniform scaling of 2.0) and moving it 10 units along the Z axis would look like this:
2.0 0.0 0.0 0.0 0.0 2.0 0.0 0.0 0.0 0.0 2.0 0.0 0.0 0.0 10.0 1.0
在XNA中使用的坐标系是右手坐标系
4X4矩阵在第一行的前三个值,即11,12,13值是在X轴上的分量,第二行前三个值即21,22,23为Y轴方向上的值,第三行前三个值31,32,33为Z轴方向上的值。
下面的代码,是一些属性,得到和设置矩阵在前后左右上下方向的分向量。
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; }
Vector3.Forward=(0,0,-1) Vector3.Backward=(0,0,1)
Vector3.Left=(-1,0,0) Vector3.Right=(1,0,0)
Vector3.Up=(0,1,0) Vector3.Down=(0,-1,0)
Matrix myMatrix = Matrix.CreateFromAxisAngle(Vector3.Right,MathHelper .Pi/6);
上面代码的意思是围绕X正轴旋转30°,系统运行,让我们看一下得到的矩阵值,如下:
1 |
0 |
0 |
0 |
0 |
0.866 |
0.5 |
0 |
0 |
-0.5 |
0.866 |
0 |
0 |
0 |
0 |
1 |
下面是绕Y轴的例子:
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 |
下面是绕Z轴的例子
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 |
缩放:
Matrix myMatrix = Matrix.CreateScale(2, 3, 4);
分别沿x,y,z轴扩大2,3,4倍。
2 |
0 |
0 |
0 |
0 |
3 |
0 |
0 |
0 |
0 |
4 |
0 |
0 |
0 |
0 |
1 |
Matrix myMatrix = Matrix.CreateTranslation(2, 3, 4);
1 |
0 |
0 |
0 |
0 |
1 |
0 |
0 |
0 |
0 |
1 |
0 |
2 |
3 |
4 |
1 |
可见平移主要用到了第四组向量。
http://www.cnblogs.com/mytudousi/archive/2011/10/27/2227042.html