# Confused about rotation matrices

Applying a rotation matrix to a vector means shifting its coordinates to perform the rotation effect.

Applying a rotation matrix to a model at the origin $$(0,0,0)$$ is not the same at performing a rotation to a model at a translated position, for instance $$(2,2,2)$$.

The most common way to rotate an object “around its own axis” would be translating to the origin before applying the rotation matrix and then translating back to the previous position:

$$T(x,y,z) * R * T(-x,-y,-z) (P)$$

If you don't, the rotation is not relative to the object's axis, thus lightly misshaped (see picture).

I would like to know if there is a way to directly compute the correct axis-angle representation, simulating a rotation around the own axis of my object in one way, without translating back to origin ?

Also, I red the “gimbal lock” occurs when using Euler angles matrices subsequently, breaking down the process in three rotation matrices for the $$X$$, $$Y$$ and $$Z$$ axes. But what about the axis-angle rotation matrix, which performs the rotation in one step, instead of three:

$$\begin{bmatrix} \cos \theta + {R_x}^2(1 - \cos \theta) & {R_x}{R_y}(1 - \cos \theta) - {R_z} \sin \theta & {R_x}{R_z}(1 - \cos \theta) + {R_y} \sin \theta & 0 \\ {R_y}{R_x} (1 - \cos \theta) + {R_z} \sin \theta & \cos \theta + {R_y}^2(1 - \cos \theta) & {R_y}{R_z}(1 - \cos \theta) - {R_x} \sin \theta & 0 \\ {R_z}{R_x}(1 - \cos \theta) - {R_y} \sin \theta & {R_z}{R_y}(1 - \cos \theta) + {R_x} \sin \theta & \cos \theta + {R_z}^2(1 - \cos \theta) & 0 \\ 0 & 0 & 0 & 1 \end{bmatrix}$$

Does this kind of matrix prevent the “gimbal lock” ? What is the difference between this matrix and a matrix computed from a quaternion $$a + bi + cj + dk$$ (using the quaternion multiplication table):

$$\begin{bmatrix} a & d & -b & -c\\-d & a & c & -b\\b & -c & a & -d\\c & b & d & a \end{bmatrix}$$

In three dimensions, a $$3\times 3$$ rotation matrix can perform any conceivable rotation you might want upon a vector.

But a vector by itself has no "location" about which to rotate. It merely changes direction. Even a large collection of vectors still has no "location."

The fact that you want to rotate a model that has a position means you are dealing with spatial coordinates, not merely vectors. One way to deal with spatial coordinates, of course, is to nominate one point in space as the "origin" and identify every point $$P$$ with the vector from the origin to $$P.$$ When you do that, rotations still change the directions of the vectors; since we still identify each point by its vector from the origin, rotating that vector rotates that point around an axis through the origin.

This means the origin, which is represented by a zero vector (all coordinates zero), never moves when you apply a $$3\times 3$$ rotation matrix in three dimensions. In fact, no matter what $$3\times 3$$ matrix you multiply with the zero vector you still get back the zero vector, even if the matrix does not represent a true rotation.

If you want to rotate a model around its own center which is not the origin, then unless you happen to pick the one rotation axis that also goes through the origin, your rotation will move the origin. Hence it is impossible to do with a $$3\times 3$$ matrix.

What you can do, if you want, is to construct a rotation $$R$$ by the desired angle around an axis in the desired orientation (parallel to the axis you really want). Then if the point $$C$$ is the center of your object, compute $$R(C).$$ Now find the vector $$v = C - R(C).$$ Then you can perform the desired rotation of any point $$P$$ around your object's own center by applying the rotation $$R$$ and the the translation $$T_v$$ (adding the vector $$v$$ to the point's coordinates): $$T_v(R(P)).$$

Now it's just one translation in addition to the rotation.

The "axis-angle rotation" matrix you showed, which is a $$4\times 4$$ matrix, is actually more than just a rotation matrix, however. The way it works is that any point $$P$$ is represented by a four-coordinate vector, $$\begin{bmatrix} x_P\\ y_P\\ z_P\\ 1, \end{bmatrix}$$ which you can rotate by multiplying by a matrix in the form you gave; but in order to follow the rotation by a translation, you just need to replace the zeros in the right-hand column of your matrix by the coordinates of the translation vector.

You can also just take the upper left $$3\times 3$$ submatrix of your "axis-angle" matrix and multiply it by the three-coordinate vector from the origin to each point, but then it's just a rotation around the origin like any other $$3\times 3$$ matrix rotation in three dimensions.

Whether this avoids "gimbal lock" depends on what you want to do with it. If you're actually using it to track the rotation of a three-axis gimbal, you can still "lock" the gimbal by putting it through some sequence of axis-angle rotations. If you have a free-floating object in space, not an actual gimbal, then the way in which "gimbal lock" might apply is if you used a set of Euler angles to represent every orientation of the object relative to its original orientation. Some sequence of orientations will force you to suddenly "flip" one of your Euler angles, which is the kind of manipulation that "locks" a real-life gimbal. That is, the Euler angles are a kind of fictional gimbal. But as long as you don't have a real gimbal or a fictional gimbal, you can't "lock" it.

The difference between matrices and quaternions is an entire question of its own. But when they are applied to three-dimensional rotations, each in their own usual way, each is just a different way to represent a rotation. And each has its own way of calculating the results of the rotation. There is plenty written about either of these techniques that you can study.

• Thank you for your detailed answer. For the “gimbal lock”, I see what you meant, If I were to use my “one-way” rotation matrix to perform three subsequent rotations in the Euler fashion (to represent a gimbal in a software, for instance), I would lock my gimbal, the solution would be to use quaternions here, no ? – eigenslacker Jan 5 '19 at 20:57
• I just don't understand how one rotation performed by one rotation matrix can “lock” the gimbal. – eigenslacker Jan 5 '19 at 21:24
• @Prion If you have a three-way gimbal, the object inside has bearings on "top" and "bottom" and the outermost bearings are in fixed places. Rotate the object so that its "top" and "bottom" bearings are aligned with the outermost bearings. Now your gimbal is locked. (That doesn't mean you can't rotate it any more, it just means the initial direction of rotation is limited.) – David K Jan 5 '19 at 21:46
• As I said, if there's no gimbal, there's no possibility of gimbal lock. So all you need to do is figure out whether your application has a gimbal (real or imaginary). If you're rotating a free body with a single matrix for each rotation, I agree there's no gimbal and no gimbal lock. – David K Jan 5 '19 at 23:08
• Many people have written about the reasons to use (or not use) quaternions, for example in the answers to math.stackexchange.com/questions/71/…, stackoverflow.com/questions/8919086/…, and stackoverflow.com/questions/1840314/… – David K Jan 6 '19 at 4:04