我很困惑为什么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 和我的四元数 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;
}