# Visualizing Combining Rotational Matrices

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.

• A 3D rotation matrix is not a 4x4 matrix, it is a 3x3 matrix. Jul 4, 2019 at 21:22
• Yeah I could have expressed it all in 3x3. It's just helpful for me to keep it all in the dimensions I'm using currently to make sure I'm not doing something stupid. I realize, strictly speaking, 3x3 is where the 3d rotational information is stored, but for consistency I assume 4x4 with the last column and row staying as 0, 0, 0, 1. Technically in this case, the vector being 1 does not alter the results anywho. I realize I'm on a math forum though, would it be more proper to be a bit more explicit and less implied here? Jul 4, 2019 at 21:42
• So, it would seem if you use a "left-hand rule" for rotations around an oriented axis, rotating $90^{\circ}$ around the $x$-axis and then rotating $90^{\circ}$ around the $y$-axis does indeed get the point $(1,1,1)$ back to where it started. This is relatively easy to visualize by coloring the vertex of a cube and watching it rotate appropriately. That means the composition of these two rotations must be a rotation around the axis through $(1,1,1)$. Also, if you use a "right-hand rule" to rotate $90^{\circ}$ around the $x$ then $y$-axis, the point $(1,1,1)$ ends up somewhere else. Jul 4, 2019 at 21:54
• @runway44 If you’re working in homogeneous coordinates (usually so that translations can also be represented by matrix multiplication), which is what is going on here, transformation matrices for $\mathbb R^3$ are $4\times4$.
– amd
Jul 6, 2019 at 1:52
• Yes, OP pointed that out after my first comment. Jul 6, 2019 at 4:19

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$$.