XNA中的Matrix结构

来源:互联网 发布:查看系统版本 linux 编辑:程序博客网 时间:2024/05/18 02:26

 

原址:http://blog.csdn.net/tanmengwen/article/details/7626244

 

 

The Matrix structure in XNA is a 4x4 matrix, which looks something like this:

[csharp] view plaincopyprint?
  1. P1x   P1y   P1z   w1   
  2. P2x   P2y   P2z   w2   
  3. P3x   P3y   P3z   w3   
  4. vx     vy      vz     w4   

        While 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:

[csharp] view plaincopyprint?
  1. 2.0   0.0   0.0   0.0   
  2. 0.0   2.0   0.0   0.0   
  3. 0.0   0.0   2.0   0.0   
  4. 0.0   0.0   10.0  1.0   



在XNA中使用的坐标系是右手坐标系

      4X4矩阵在第一行的前三个值,即11,12,13值是在X轴上的分量,第二行前三个值即21,22,23为Y轴方向上的值,第三行前三个值31,32,33为Z轴方向上的值。

下面的代码,是一些属性,得到和设置矩阵在前后左右上下方向的分向量。

[csharp] view plaincopyprint?
  1. public Vector3 Backward { getset; }  
  2.   
  3. public Vector3 Down { getset; }  
  4.   
  5. public Vector3 Forward { getset; }  
  6.   
  7. public Vector3 Left { getset; }  
  8.   
  9. public Vector3 Right { getset; }  
  10.   
  11. public Vector3 Translation { getset; }  
  12.   
  13. public Vector3 Up { getset; }   


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

可见平移主要用到了第四组向量。