I am a bit confused right now in 3D rotations. I have searched this question in many different ways but have not found a resource that could help me mentally/visually understand what is going on.
I'm familiar with matrices and utilizing them quite frequently in graphics, but this particular situation is making me realize I have a flaw in my reasoning at a base level.
I want to take two 3D (3x3 rotational information stored in a 4x4 matrix with last column and row staying as 0, 0, 0, 1 in the following examples) rotational matrices and combine them to perform a rotation in this order: x-axis then y-axis.
so I simply multiply them together to perform the operation. When I apply the matrices individually against a vector of [1, 1, 1, 1] I get the expected results:
Assuming +x toward the screen, +z to the right, +y is up
x-axis matrix (of 90 degrees) produces [1, -1, 1, 1] (a clockwise spin of the x-axis)
y-axis matrix (of 90 degrees) produces [1, 1, -1, 1] (a clockwise spin of the y-axis)
Now when I combine the matrices to rotate x then y and apply it against my vector (both still represent 90 degrees on their respective axis) it produces [1, 1, 1, 1].
I have tested this result against many online calculators that express this result as correct; however, to achieve that result would mean you did a counter-clockwise spin on the x-axis and a counter-clockwise spin on the y-axis.
I do not understand why this is happening. Again as a mental model...mathematically it just works just fine, but it really breaks down some of my perceptions on ordered rotations.
I feel like this has to deal with gimble lock somehow, just not sure how it plays into this especially since I split up the rotations and applied it to my vector one step at a time an lo! The result is more what I expected.
It doesn’t really have to do with anything fancy-sounding like gimbal lock or that the matrices for rotation about the $y$-axis look “backwards” because the $y$- and $z$-axis form a left-handed coordinate system. It’s simply because matrix multiplication is not commutative: the order in which you multiply them matters. In particular, 3-D rotations don’t commute unless they have a common rotation axis. You’re almost certainly multiplying the two primitive rotation matrices together in the wrong order when computing the combined transformation matrix.
You haven’t said whether you’re working with row or column vectors, but either way, when you chain transformations together like this, remember that the input to the next transformation in the chain is the output of the preceding transformation. If you’re working with column vectors so that left-multiplication by a matrix gives you the transformed vector, the result of applying the first rotation is $\mathbf v'=R_x\mathbf v$, and the result of applying the second rotation to that is $R_y\mathbf v' = R_y(R_x\mathbf v) = (R_yR_x)\mathbf v$, therefore the matrix that represents the combination of the two rotations is $R_yR_x$. You likely computed $R_xR_y$ instead, which corresponds to first rotating about the $y$-axis and then rotating about the $x$-axis. An easy way to remember this is that if you left-multiply by a matrix to transform a vector, then the chain of matrices grows to the left.
If you’re instead working with row vectors, then you’d be right-multiplying by the transformation matrices, and the chain would grow to the right. In that case, $R_xR_y$ would give you the correct result, since $\mathbf v(R_xR_y)=(\mathbf vR_x)R_y$.
Now, since any combination of rotations is itself a rotation, and every rotation in 3-D has a unique axis that is unchanged by the rotation, there is a set of vectors for which the order in which you apply the rotations doesn’t matter. Those vectors all lie along the effective axis of the combination of individual rotations.