I am baffled as to why android’s matrix class’s multiplyMV method seems to be rotating my vector coordinate clockwise, when I thought it would be counter-clockwise.
In this code pos is the vector coordinate and set to be <0.0f, 5.0f ,0.0f>, and the matrix will rotate the coordinate vector by -45 degrees about the Z axis. I would expect the result vector coordinate in the <+,+> quadrant, namely <3.535534, 3.535534, 0.0>. But instead, it rotates the coordinate in the opposite direction, <-,+> quadrant, namely <-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);
Here’s my in 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;
}
And here is my Matrix4.TransformCoordVec method
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;
}
Any help is greatly appreciated!
FIXED
InitRotateEulerXYZ and my Quaternion ToMatrix() methods needed to be transposed in order to make rotation counter-clockwise for positive angles. Here are the corrected methods.
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;
}
Simplifying the matrices you have created, by setting X and Y to 0 as arguments in
InitRotateEulerXYZ, and multiplying by (0, 5, 0, 0) you get:Substituting θ=-π/4, the resulting vector is given by:
Which is exactly the answer you have been seeing. So, I suggest it’s not a problem with the Android matrix multiplication routines. You should recheck the derivation of your matrix multiplications or simply choose one of those already derived at another site and adapt your code accordingly.
Alternatively, you can use the built in setRotateEulerM method and have the system generate it for you. You just need to make certain the order of the matrix multiplications is consistent with your intent, as there are a variety of ways to generate these matrices.
One other thing to consider is that using Euler Rotation matrices are generally a poor method for generalized rotations as they are subject to things like gimble lock. You may want to consider one of the Axis/Angle methods such as Rodrigues’ method, or by using quaternions. The Android library appears to have native implementations of axis/angle routines if you would prefer to use one of those instead.