Affine Rotation Matrices

297 views Asked by At

I'm writing a Matrix class that has static RotationX() RotationY() and RotationZ() methods in C++. If I multiply the matrices together before multiplying by the vector, I get different results than if I had multiplied the matrices individually to the vector.

This code

Vec4 result1 { 1, 1, 1, 1 };
result1 = Matrix::RotationX(ToRadians(-90)) * result1;
result1 = Matrix::RotationY(ToRadians(90)) * result1;
result1 = Matrix::RotationZ(ToRadians(90)) * result1;
// result1 => { -1, -1, -1, 1 }

gives different results than this code

Vec4 result2 { 1, 1, 1, 1 };
auto rotation = Matrix::RotationX(ToRadians(-90)) *
                Matrix::RotationY(ToRadians(90)) *
                Matrix::RotationZ(ToRadians(90));
result2 = rotation * result2;
// result2 => { 1, 1, -1, 1 }

What is the issue here? I can provide my rotation function implementations, but I wanted to make sure this wasn't a conceptual issue with affine transformations before posting code.

1

There are 1 answers

1
MBo On BEST ANSWER

Your first example corresponds to the reverse order of matrix multiplication. Compare:

Z * (Y * (X * V))
((X * Y) * Z) * V

But matrix multiplication is not commutative!