旋转矩阵面向方向矢量

时间:2016-06-25 03:41:18

标签: c# matrix xna

我有一个角色的位置,例如xyz坐标x = 102,y = 0.75,z = -105.7。我有角色的旋转矩阵

M11 = -0.14
M12 = 0
M13 = -0.99
M21 = 0
M22 = 1
M23 = 0
M31 = 0.99
M32 =0
M33 = 0.14

我对图形以及这些数据如何与角色的面向方向相关联的理解不多。我想找到一个矢量,这样我就可以使用该矢量来瞄准角色所面对的方向。我该怎么做?

2 个答案:

答案 0 :(得分:1)

角色面向的方向是旋转矩阵的第3行。那就是:

Vector3 facingDirection = new Vector3(0.99f, 0f, 0.14f);//(m31,m32,m33)

此向量似乎已标准化,但如果不是,则会执行以下操作:

Vector3 facingDirection = Vector3.Normalize(new Vector3(0.99f, 0f, 0.14f));

如果矩阵是XNA矩阵,那么您只需使用Matrix结构的Matrix.Forward属性并获得相同的结果。

答案 1 :(得分:1)

我终于能够解决它了。实际上它是非常简单的矢量数学。旋转矩阵已经给了我史蒂夫在上面提出的方向向量,但是我不得不沿着那条线向某个特定的点开始使我的动画工作......所以我实际上需要一个沿着方向向量表示的线的点。所以,我只是计算了一个距离角色当前位置大约1000个单位的方向向量并向该线开火,它有效!

 Vector3 facingDirection = new Vector3(RotMatrix[2, 0], RotMatrix[2, 1], RotMatrix[2, 2]); // direction vector
                    Vector3 currentPos = new Vector3(character.PosX, character.PosY, character.PosZ); // I also have position of the character
// Now calculate a point 10000 units away along the vector line 
                    var px = currentPos.X + facingDirection.X * 10000;
                    var py = currentPos.Y + facingDirection.Y * 10000;
                    var pz = currentPos.Z + facingDirection.Z * 10000;
                    return new Vector3(px, py, pz);