为什么Matrix.MutiplyMV顺时针旋转矢量?

时间:2012-07-12 00:55:12

标签: android math vector matrix

我很困惑为什么android的矩阵类的multiplyMV方法似乎顺时针旋转我的矢量坐标,当我认为它是逆时针时。

在此代码中,pos是矢量坐标并设置为< 0.0f,5.0f,0.0f> ,矩阵将围绕Z轴旋转坐标矢量-45度。我希望结果向量坐标在< +,+>中。象限,即< 3.535534,3.535534,0.0> 。但相反,它以相反的方向旋转坐标,< - ,+>象限,即< -3.535534,3.535534,0.0>

Matrix4 mtxRot = Matrix4.InitRotateEulerXYZ(0.0f, 0.0f, -45f);
pos.Set(0.0f, 5.0f ,0.0f);
mtxRot.TransformCoordVec(pos);

这是我的Matrix4.InitRotateEulerXYZ

public static Matrix4 InitRotateEulerXYZ(float x, float y, float z)
{
    Matrix4 rotMatrix = new Matrix4();

    /*  XYZ = | cz*cy,   sz*cx + cz*sy*sx,  sz*sx - cz*sy*cx |
              | -sz*cy,  cz*cx - sz*sy*sx,  cz*sx + sz*sy*cx |
              | sy,     -cy*sx,             cy*cx            |  */

    // Convert from degrees to radians
    x = MathHelper.DegreesToRadians(x);
    y = MathHelper.DegreesToRadians(y);
    z = MathHelper.DegreesToRadians(z);

    rotMatrix.GetArray()[0] =  MathHelper.Cos(z) * MathHelper.Cos(y);
    rotMatrix.GetArray()[1] = -MathHelper.Sin(z) * MathHelper.Cos(y);
    rotMatrix.GetArray()[2] =  MathHelper.Sin(y);

    rotMatrix.GetArray()[4] =  (MathHelper.Sin(z) * MathHelper.Cos(x)) + (MathHelper.Cos(z) * MathHelper.Sin(y) * MathHelper.Sin(x));
    rotMatrix.GetArray()[5] =  (MathHelper.Cos(z) * MathHelper.Cos(x)) - (MathHelper.Sin(z) * MathHelper.Sin(y) * MathHelper.Sin(x));
    rotMatrix.GetArray()[6] = -(MathHelper.Cos(y) * MathHelper.Sin(x));

    rotMatrix.GetArray()[8 ] = (MathHelper.Sin(z) * MathHelper.Sin(x)) - (MathHelper.Cos(z) * MathHelper.Sin(y) * MathHelper.Cos(x));
    rotMatrix.GetArray()[9 ] = (MathHelper.Cos(z) * MathHelper.Sin(x)) + (MathHelper.Sin(z) * MathHelper.Sin(y) * MathHelper.Cos(x));
    rotMatrix.GetArray()[10] =  MathHelper.Cos(y) * MathHelper.Cos(x);

    return rotMatrix;
}

这是我的Matrix4.TransformCoordVec方法

public Vector3 TransformCoordVec(Vector3 vec3)
{
    Matrix4.inVec[0] = vec3.X;
    Matrix4.inVec[1] = vec3.Y;
    Matrix4.inVec[2] = vec3.Z;
    Matrix4.inVec[3] = 1.0f; // homogeneousCoord

    Matrix.multiplyMV(Matrix4.outVec, 0, this.matrix, 0, Matrix4.inVec, 0);     

    vec3.X = Matrix4.outVec[0]; vec3.Y = Matrix4.outVec[1]; vec3.Z = Matrix4.outVec[2];     
    return vec3;
}

非常感谢任何帮助!

已修复

InitRotateEulerXYZ和我的Quaternion ToMatrix()方法需要进行转置,以便逆时针旋转正角度。以下是更正的方法。

Quaternion.ToMatrix

/**Converts a quanternion to its equivilant matrix form**/
public Matrix4 ToMatrix()
{       
    // First, lets check if we need to re-normalize our quaternion
    if(normalRegenerationCount <= 1000)
    {
        Normalize();
    }

    float x2 = x * x;
    float y2 = y * y;
    float z2 = z * z;
    float xy = x * y;
    float xz = x * z;
    float yz = y * z;
    float wx = w * x;
    float wy = w * y;
    float wz = w * z;

    Matrix4 result = new Matrix4();

    // This calculation would be a lot more complicated for non-unit length quaternions
    // Note: The constructor of Matrix4 expects the Matrix in column-major format like expected by
    //   OpenGL
    result.Set_11(1.0f - (2.0f * (y2 + z2)));
    result.Set_12(2.0f * (xy + wz)); 
    result.Set_13(2.0f * (xz - wy));        
    result.Set_14(0.0f);        

    result.Set_21(2.0f * (xy - wz));  
    result.Set_22(1.0f - (2.0f * (x2 + z2)));
    result.Set_23(2.0f * (yz + wx));
    result.Set_24(0.0f); 

    result.Set_31(2.0f * (xz + wy)); 
    result.Set_32(2.0f * (yz - wx)); 
    result.Set_33(1.0f - (2.0f * (x2 + y2))); 
    result.Set_34(0.0f);

    result.Set_41(0.0f);
    result.Set_42(0.0f);        
    result.Set_43(0.0f);        
    result.Set_44(1.0f);

    return result;
}

Matrix.InitRotateEulerXYZ

public static Matrix4 InitRotateEulerXYZ(float x, float y, float z)
{
    Matrix4 rotMatrix = new Matrix4();

    // Convert from degrees to radians
    x = MathHelper.DegreesToRadians(x);
    y = MathHelper.DegreesToRadians(y);
    z = MathHelper.DegreesToRadians(z);

    rotMatrix.matrix[0] =  MathHelper.Cos(z) * MathHelper.Cos(y);
    rotMatrix.matrix[1] = (MathHelper.Sin(z) * MathHelper.Cos(x)) + (MathHelper.Cos(z) * MathHelper.Sin(y) * MathHelper.Sin(x)); 
    rotMatrix.matrix[2] = (MathHelper.Sin(z) * MathHelper.Sin(x)) - (MathHelper.Cos(z) * MathHelper.Sin(y) * MathHelper.Cos(x));

    rotMatrix.matrix[4] =  -MathHelper.Cos(y) * MathHelper.Sin(z);
    rotMatrix.matrix[5] =  (MathHelper.Cos(z) * MathHelper.Cos(x)) - (MathHelper.Sin(z) * MathHelper.Sin(y) * MathHelper.Sin(x));
    rotMatrix.matrix[6] =  (MathHelper.Cos(z) * MathHelper.Sin(x)) + (MathHelper.Sin(z) * MathHelper.Sin(y) * MathHelper.Cos(x)); 

    rotMatrix.matrix[8 ] =  MathHelper.Sin(y); 
    rotMatrix.matrix[9 ] = -(MathHelper.Cos(y) * MathHelper.Sin(x));
    rotMatrix.matrix[10] =  MathHelper.Cos(y) * MathHelper.Cos(x);

    return rotMatrix;
}

1 个答案:

答案 0 :(得分:1)

通过在InitRotateEulerXYZ中将X和Y设置为0作为参数,然后乘以(0,5,0,0),简化您创建的矩阵:

| cos(θ) sin(θ)    0     0 | | 0 |   | 5*sin(θ) |
|-sin(θ) cos(θ)    0     0 | | 5 |   | 5*cos(θ) |
|    0     0       1     0 | | 0 | = |     0    |
|    0     0       0     0 | | 0 |   |     0    |

代入θ=-π/ 4,得到的矢量由下式给出:

| 5*sin(-π/4) |   | -5*sin(π/4) |
| 5*cos(-π/4) |   |  5*cos(π/4) |
|      0      | = |       0     |
|      0      |   |       0     |

这正是您所看到的答案。所以,我认为这不是Android矩阵乘法例程的问题。您应该重新检查矩阵乘法的推导,或者只选择其中一个already derived at another site并相应地调整您的代码。

或者,您可以使用内置的setRotateEulerM方法并让系统为您生成它。您只需要确定矩阵乘法的顺序与您的意图一致,因为有多种方法可以生成这些矩阵。

另一件需要考虑的事情是,使用Euler Rotation矩阵通常是广义旋转的一种不好的方法,因为它们受到像gimble lock这样的事情的影响。您可能需要考虑其中一个Axis/Angle methods,例如Rodrigues' method,或使用quaternions。如果您希望使用其中一个,那么Android库似乎有native implementations of axis/angle routines