Can someone show mathematically how gimbal lock happens when doing matrix rotation with Euler angles for yaw, pitch, roll? I'm having a hard time understanding what is going on even after reading several articles on Google.

Is the only work-around to use quaternions?


5 Answers 5


"Euler Angles" you can think of as a function $(S^1)^3 \to SO_3$ or $\mathbb R^3 \to SO_3$. The derivative of this function does not always have rank 3, so you have degenerate submanifolds where the function is many-to-one. In this special case that's called "gimbal lock".

One formalism that avoids this is quaternions. You can of course use other formalisms, and many other formalisms are naturally related to the quaternion version, so people tend to gravitate to the quaternion version. One version that's closely related to quaternions would be to use the exponential map for the unit quaternion group. But this also has "gymbal lock" but of a different kind. But it does have the rather appealing interpretation as rotations about arbitrary axis -- this is perhaps more useful if you're only interested in rotations that differ from the identity matrix (or some given matrix) by a small amount, they're very natural coordinates on "small scales" in $SO_3$.

Are there any special properties you'd like for coordinates on $SO_3$? That might give a sense for where you want to go with this.

Edit in response to 1st comment: There's various conventions for Euler angles. Let's use what Wikipedia calls "Proper Euler Angles" http://en.wikipedia.org/wiki/Euler_angles

Given a unit vector $\vec v \in \mathbb R^3$ and a number $\theta \in \mathbb R$, let $R_{\vec v, \theta}$ be the rotation matrix which fixes $\vec v$ and which rotates counter-clockwise by an angle $\theta$ in the plane orthogonal to $\vec v$. One version of "Proper Euler angles" would be:

$$R_{(1,0,0), \theta_1} R_{(0,1,0), \theta_2} R_{(1,0,0), \theta_3}$$

The "Tait-Bryan" variant would use the three distinct vectors, notice above we only used the standard vectors for the xy-plane. The Tait-Bryan variant is the one appropriate to pitch/roll/yaw of an aeroplane. Wikipedia has an excellent picture:


In this case you'd have

$$R_{(1,0,0),\theta_1} R_{(0,1,0), \theta_2} R_{(0,0,1),\theta_3} $$

$\theta_3$ would be the roll, $\theta_2$ the pitch and $\theta_1$ the yaw. To relate my coordinates to the picture, $(1,0,0)$ is the direction the plane is pointing. $(0,1,0)$ is the direction of the left wing. $(0,0,1)$ is the direction of the yellow axis sticking out of the top of the plane.

You can write out the matrices quite explicitly:

$$ R_{(1,0,0), \theta} = \pmatrix{ 1 & 0 & 0 \cr 0 & \cos \theta & -\sin \theta \cr 0 & \sin \theta & \cos \theta }$$

So in this case, one occurance of "gimbal lock" is $\theta_2 = 0$. In the Tait-Bryan variant it would be when the aeroplane is either pointing straight up or down, which is $\theta_2 = \pm \pi/2$.

  • $\begingroup$ I would like to see mathematically where we fail to get the expected result through the use of matrix rotation. So if have the Rx(Pitch) Ry(yaw) Rz(roll) rotation matrices and multiply them, where does it fail to get the expected result. Is the problem that I can get an identity matrix and fail to rotate accordingly? Sorry I don't know which order the matrices are supposed to be multiplied in. $\endgroup$
    – Xavier
    Commented Nov 4, 2010 at 23:28
  • $\begingroup$ I've posted a comment in separate answer. $\endgroup$ Commented Nov 5, 2010 at 8:16

This refers to Ryan's answer, but was too long for a comment.

I was getting confused for a while too, but I think I understand now. To avoid misunderstandings, the Tait–Bryan angles should be called heading/elevation/bank instead of yaw/pitch/roll. The heading angle refers to rotations about the $z$ axis in space, not about the yaw axis which by definition is perpendicular to the "body-wing-plane" of the airplane, so that "excellent picture" is in fact a bit misleading in this context! The gimbal lock illustrated on Wikipedia occurs when the plane is pointing straight up or down (elevation = 90 degrees), so that changing the bank is the same thing as changing the heading. (That is, there is momentarily no way to change the yaw by manipulating the heading/elevation/bank angles!)

  • $\begingroup$ I'll use those names from now on, they appeal to my imagination better. :) $\endgroup$ Commented Nov 5, 2010 at 11:21

If you only care about the beginning and end, you can express any rotation with the Euler angles. But if you want a smooth transition between the beginning and the end, then with some starting orientations, you have a problem, and this is called the gimbal lock.

For example, here we start from (x,y,z) = (1,0,0) and simultaneously turn yaw left and lift pitch up at 5 degree steps, until both reach 45 degrees. The black dots move on the surface of the unit sphere. (Roll could be whatever, it cannot be seen as I am only plotting dots.)

Rotation: yaw 45, pitch 45

But say, we start from a position pointing directly upwards, so our object is pointing to (x,y,z) = (0,0,1) and we want to rotate it down around the x-axis by 45 degrees. Now we have to turn yaw (or heading) 90 degrees, and lower the pitch 45 degrees (and perhaps roll -90 degrees, depending what orientation we want, but my dots cannot illustrate this).

Now if we do the yaw and pitch transformations simultaneously:

(yaw=0,pitch=0), (yaw=5,pitch=-5), (yaw=10,pitch=-10), ...

we get the black dots in the figure below, whereas a straight line by 5 degree turns around the x-axis is plotted with the white dots. We can still reach the same endpoint, but we cannot start to turn directly into the "correct" or "straight" direction.

Rotation: yaw 90, pitch -45

You could redefine the Euler angles so that the first rotation is not around the z-axis, but around the x-axis, and then this situation could be remedied. But then a similar problem could happen with an object that is initially pointing to the x-direction. However you reorder the three Euler rotations, there is always some orientation from which you cannot start turning directly to a certain direction.

Here is also a Youtube-video on the topic.

  • $\begingroup$ Is the x axis in the first picture pointing towards lower-left? If the starting point is pointing in the direction (1, 0, 0) and the path is made by yaw left + pitch up then seems as the x axis is pointing from right to left. Shouldn't the last point in th first picture be on the "cube's" diagonal? It doesn't look like it is. The explanation is good but it is not clear why wouldn't we just rotate the object by following the straight line in the second example. Axis labels and the 3D arrow representing the object would help with easier understanding. $\endgroup$
    – Chupo_cro
    Commented Oct 19, 2020 at 4:54

Let's say we have an object in 3D space located at P(1,1,1) and if we decided we wanted to rotate this object within any of the planes XY, XZ, YZ along any of the 3 axis where this is a basic 3D Cartesian system in a Right-Hand System the rotation matrices $R_n$ for P will look like these:

$$ R_x \space|| \space R_y \space || \space R_z $$

$$R_x(\theta) P = \begin{bmatrix} 1 & 0 & 0 \\ 0 & \cos\theta & -\sin\theta \\ 0 & \sin\theta & \space\space\cos\theta \\ \end{bmatrix} \begin{bmatrix} P_x \\ P_y \\ P_z \\ \end{bmatrix},$$

$$R_y(\theta) P = \begin{bmatrix} \space\cos\theta & 0 & \sin\theta \\ 0 & 1 & 0 \\ -\sin\theta & 0 & \cos\theta \\ \end{bmatrix} \begin{bmatrix} P_x \\ P_y \\ P_z \\ \end{bmatrix},$$

$$R_z(\theta) P= \begin{bmatrix} \cos\theta & -\sin \theta & 0 \\ \sin\theta & \space\space\cos \theta & 0 \\ 0 & 0 & 1 \\ \end{bmatrix} \begin{bmatrix} P_x \\ P_y \\ P_z \\ \end{bmatrix}$$

When doing rotations in 3D among any of the arbitrary axis, the order of the rotations, the handedness of the system, the direction of the rotations and doing rotations among multiple axis does matter. To demonstrate this let's say angle $\theta = 90°$ and we apply this rotation consecutively in multiple dimensions you will see that we eventually end up with Gimbal Lock. First we will do $R_x$ by 90° then $R_y$ and finally try $R_z$

Here we are going to apply a 90° rotation to the point or vector P(1,1,1) on the $R_x$ axis

$R_x(90°)$ $\begin{bmatrix} 1 \\ 1 \\ 1 \\ \end{bmatrix}$ = $\begin{bmatrix} 1 & 0 & 0 \\ 0 & \cos 90° & -\sin 90° \\ 0 & \sin 90° & \space\space\cos 90° \\ \end{bmatrix}$ $\begin{bmatrix} 1 \\ 1 \\ 1 \\ \end{bmatrix}$ = $\begin{bmatrix} 1 & 0 & \space\space 0 \\ 0 & 0 & -1 \\ 0 & 1 & \space\space 0 \\ \end{bmatrix}$ $\begin{bmatrix} 1 \\ 1 \\ 1 \\ \end{bmatrix}$ = $\begin{bmatrix} 1 \\ -1 \\ 1 \\ \end{bmatrix}$

Now that our vector P has been transformed we will apply another 90° rotation but this time to the $R_y$ axis with the new values.

$R_y(90°)$ $\begin{bmatrix} 1 \\ -1 \\ 1 \\ \end{bmatrix}$ = $\begin{bmatrix} \cos 90° & 0 & \sin 90°\\ 0 & 1 & 0 \\ -\sin 90° & 0 & \cos 90°\\ \end{bmatrix}$ $\begin{bmatrix} 1 \\ -1 \\ 1 \\ \end{bmatrix}$ = $\begin{bmatrix} 0 & 0 & 1 \\ 0 & 1 & 0 \\ -1 & 0 & 0\\ \end{bmatrix}$ $\begin{bmatrix} 1 \\ -1 \\ 1 \\ \end{bmatrix}$ = $\begin{bmatrix} 1 \\ -1 \\ -1 \\ \end{bmatrix}$

We can now finish with $R_z$

$R_z(90°)$ $\begin{bmatrix} 1 \\ -1 \\ -1 \\ \end{bmatrix}$ = $\begin{bmatrix} \cos 90° & -\sin 90° & 0 \\ \sin 90° & \cos 90° & 0 \\ 0 & 0 & 1 \\ \end{bmatrix}$ $\begin{bmatrix} 1 \\ -1 \\ -1 \\ \end{bmatrix}$ = $\begin{bmatrix} 0 & -1 & 0 \\ 1 & 0 & 0 \\ 0 & 0 & 1 \\ \end{bmatrix}$ $\begin{bmatrix} 1 \\ -1 \\ -1 \\ \end{bmatrix}$ = $\begin{bmatrix} 1 \\ 1 \\ -1 \\ \end{bmatrix}$

And as you can see in the calculations of the matrices there has been a change in direction for each time we rotated by 90 degrees. Here we have lost a degree of freedom of rotation. What this means is we rotated the X components by 90° which happens to be perpendicular or orthogonal to both the Y & Z axis which is evident of the fact that $\cos(90°) = 0$. Then when we rotate again by 90° along the Y axis and once again the Y axis is perpendicular to both the X & Z axis now we have 2 axis of rotations that are aligned so when we try to rotate in the 3rd dimension of space we lost a degree of freedom because we can no longer distinguish between the X & Y as they will both rotate simultaneously and there is no method to separate them. This can be seen from the calculations that were done by the matrices. It may not be completely evident now, but if you were to do all 6 permutations of the order of axis rotations you will see the pattern emerge. These kind of rotations are called Euler Angles.

It also doesn't matter what combination of axis you rotate with because it will happen with every combination when two axis of rotations become parallel.

$$R_x(90°)P \to R_y(90°)P \to R_z(90°)P \implies Gimbal Lock$$ $$R_x(90°)P \to R_z(90°)P \to R_y(90°)P \implies Gimbal Lock$$ $$R_y(90°)P \to R_x(90°)P \to R_z(90°)P \implies Gimbal Lock$$ $$R_y(90°)P \to R_z(90°)P \to R_x(90°)P \implies Gimbal Lock$$ $$R_z(90°)P \to R_x(90°)P \to R_y(90°)P \implies Gimbal Lock$$ $$R_z(90°)P \to R_y(90°)P \to R_x(90°)P \implies Gimbal Lock$$

If I simplify this by showing all 6 combinations with the ending transformation vectors or matrices for that same point you should see the pattern and these transformations are:

$$R_x(90°) P(1,1,1) \to \begin{bmatrix} 1 \\ -1 \\ 1 \\ \end{bmatrix} R_y(90°) \to \begin{bmatrix} 1 \\ -1 \\ -1 \\ \end{bmatrix} R_z(90°) \to \begin{bmatrix} 1 \\ 1 \\ -1 \\ \end{bmatrix}$$

$$R_x(90°) P(1,1,1) \to \begin{bmatrix} 1 \\ -1 \\ 1 \\ \end{bmatrix} R_z(90°) \to \begin{bmatrix} 1 \\ 1 \\ 1 \\ \end{bmatrix} R_y(90°) \to \begin{bmatrix} 1 \\ 1 \\ -1 \\ \end{bmatrix}$$

$$R_y(90°) P(1,1,1) \to \begin{bmatrix} 1 \\ 1 \\ -1 \\ \end{bmatrix} R_x(90°) \to \begin{bmatrix} 1 \\ 1 \\ 1 \\ \end{bmatrix} R_z(90°) \to \begin{bmatrix} -1 \\ 1 \\ 1 \\ \end{bmatrix}$$

$$R_y(90°) P(1,1,1) \to \begin{bmatrix} 1 \\ 1 \\ -1 \\ \end{bmatrix} R_z(90°) \to \begin{bmatrix} -1 \\ 1 \\ -1 \\ \end{bmatrix} R_x(90°) \to \begin{bmatrix} -1 \\ 1 \\ 1 \\ \end{bmatrix}$$

$$R_z(90°) P(1,1,1) \to \begin{bmatrix} -1 \\ 1 \\ 1 \\ \end{bmatrix} R_x(90°) \to \begin{bmatrix} -1 \\ -1 \\ 1 \\ \end{bmatrix} R_y(90°) \to \begin{bmatrix} 1 \\ -1 \\ 1 \\ \end{bmatrix}$$

$$R_z(90°) P(1,1,1) \to \begin{bmatrix} -1 \\ 1 \\ 1 \\ \end{bmatrix} R_y(90°) \to \begin{bmatrix} 1 \\ 1 \\ 1 \\ \end{bmatrix} R_x(90°) \to \begin{bmatrix} 1 \\ -1 \\ 1 \\ \end{bmatrix}$$

If you look at the combinations of the axis that you started with it doesn't matter the order of which two you finished with because the result will be the same for that starting axis of rotation. So intuitively we can say this about using Euler Angles of Rotation in 3D while considering the handedness of the coordinate system; the handedness matters because it will change the rotation matrices along with the trig functions and their signs for they will be different and so will your results. Now as for this particular coordinate system we can visually conclude this about Euler Angles:

$$R_x(\theta) P \implies \begin{bmatrix} a \\ a \\ -a \\ \end{bmatrix}$$

$$R_y(\theta) P \implies \begin{bmatrix} -a \\ a \\ a \\ \end{bmatrix}$$

$$R_z(\theta) P \implies \begin{bmatrix} a \\ -a \\ a \\ \end{bmatrix}$$

It may not seem quite apparent by the numbers as to what exactly is causing the gimbal lock, but the results of the transformations should give you some insight to what is going on. It might be easier to visualize than just by looking at the math. So I provided a link to a good video below. Now if you are interested in proofs then you have plenty of work ahead of you for there are also some other factors that cause this to happen such as the relationships of the $\cos(\theta)$ between two vectors being equal to the dot product between those vectors divided by their magnitudes.

$$\cos(\theta) = \frac{ V_1 \cdot V_2}{ \lvert V_1 \rvert \lvert V_2 \rvert } $$

Other contributing factors are the rules of calculus on the trigonometric functions especially the $\sin$ and $\cos$ functions.

$$(\sin{x})' = \cos{x}$$

$$(\cos{x})' = -\sin{x}$$

$$\int \sin{ax} \space dx = -\frac{1}{a}\cos{ax} + C$$

$$\int \cos{ax} \space dx = \frac{1}{a}\sin{ax} + C$$

Another interesting fact that I think that may lead to the reasoning of Gimbal Lock is quite interesting but that is a topic for another day as that in itself would merit its own page, but do forgive me if the math formatting isn't perfect I am new to this particular stack exchange site and I'm learning the math tags and formatting as I go.

Here is an excellent video illustrating Gimbal Lock: Youtube : Gimbal Lock


Many answers have been given that are mathematically very sound and correct. Here is another way, using only high school mathematics:

Suppose, we use the ZXY Euler rotations: first about the Z-axis (yaw), then about the X-axis (roll) and finally about the y-axis(pitch).

$R_z(\psi) = \left(\begin{array}{ccc} \cos \left(\psi \right) & -\sin \left(\psi \right) & 0\\ \sin \left(\psi \right) & \cos \left(\psi \right) & 0\\ 0 & 0 & 1 \end{array}\right)$

$R_x(\phi) = \left(\begin{array}{ccc} 1 & 0 & 0\\ 0 & \cos \left(\phi \right) & -\sin \left(\phi \right)\\ 0 & \sin \left(\phi \right) & \cos \left(\phi \right) \end{array}\right)$

$R_y(\theta) = \left(\begin{array}{ccc} \cos \left(\theta \right) & 0 & \sin \left(\theta \right)\\ 0 & 1 & 0\\ -\sin \left(\theta \right) & 0 & \cos \left(\theta \right) \end{array}\right)$

The rotation matrix in ZXY configuration would be:

$R = R_z(\psi)R_x(\phi)R_y(\theta)$

$R = \left(\begin{array}{ccc} \cos \left(\psi \right)\,\cos \left(\theta \right)-\sin \left(\phi \right)\,\sin \left(\psi \right)\,\sin \left(\theta \right) & -\cos \left(\phi \right)\,\sin \left(\psi \right) & \cos \left(\psi \right)\,\sin \left(\theta \right)+\cos \left(\theta \right)\,\sin \left(\phi \right)\,\sin \left(\psi \right)\\ \cos \left(\theta \right)\,\sin \left(\psi \right)+\cos \left(\psi \right)\,\sin \left(\phi \right)\,\sin \left(\theta \right) & \cos \left(\phi \right)\,\cos \left(\psi \right) & \sin \left(\psi \right)\,\sin \left(\theta \right)-\cos \left(\psi \right)\,\cos \left(\theta \right)\,\sin \left(\phi \right)\\ -\cos \left(\phi \right)\,\sin \left(\theta \right) & \sin \left(\phi \right) & \cos \left(\phi \right)\,\cos \left(\theta \right) \end{array}\right)$

Now, let's replace $\phi =\frac{\pi }{2}$

You will get R as:

$R = \left(\begin{array}{ccc} \cos \left(\psi \right)\,\cos \left(\theta \right)-\sin \left(\psi \right)\,\sin \left(\theta \right) & 0 & \cos \left(\psi \right)\,\sin \left(\theta \right)+\cos \left(\theta \right)\,\sin \left(\psi \right)\\ \cos \left(\psi \right)\,\sin \left(\theta \right)+\cos \left(\theta \right)\,\sin \left(\psi \right) & 0 & \sin \left(\psi \right)\,\sin \left(\theta \right)-\cos \left(\psi \right)\,\cos \left(\theta \right)\\ 0 & 1 & 0 \end{array}\right)$

Using trigonometric identities, we get:

$R = \left(\begin{array}{ccc} \cos \left(\psi +\theta \right) & 0 & \sin \left(\psi +\theta \right)\\ \sin \left(\psi +\theta \right) & 0 & -\cos \left(\psi +\theta \right)\\ 0 & 1 & 0 \end{array}\right)$

There is infinite $\psi$ and $\theta$ that create the same rotation matrix.

For $\phi =-\frac{\pi }{2}$:

$R = \left(\begin{array}{ccc} \cos \left(\psi \right)\,\cos \left(\theta \right)+\sin \left(\psi \right)\,\sin \left(\theta \right) & 0 & \cos \left(\psi \right)\,\sin \left(\theta \right)-\cos \left(\theta \right)\,\sin \left(\psi \right)\\ \cos \left(\theta \right)\,\sin \left(\psi \right)-\cos \left(\psi \right)\,\sin \left(\theta \right) & 0 & \cos \left(\psi \right)\,\cos \left(\theta \right)+\sin \left(\psi \right)\,\sin \left(\theta \right)\\ 0 & -1 & 0 \end{array}\right)$

$R = \left(\begin{array}{ccc} \cos \left(\psi -\theta \right) & 0 & -\sin \left(\psi -\theta \right)\\ \sin \left(\psi -\theta \right) & 0 & \cos \left(\psi -\theta \right)\\ 0 & -1 & 0 \end{array}\right)$

You can use Symbolic math tools to create these simplifications. I have used MATLAB:

syms psi

R_z = [ cos(psi), -sin(psi), 0; 
        sin(psi),  cos(psi), 0;
        0       ,  0       , 1
syms phi 

R_x = [ 1, 0,           0; 
        0, cos(phi), -sin(phi);
        0, sin(phi),  cos(phi)

syms theta 

R_y = [  cos(theta), 0, sin(theta); 
         0         , 1, 0;
        -sin(theta), 0, cos(theta)
R = R_z*R_x*R_y;
R_new_1_expr = subs(R, phi, pi/2)
R_new_1_simplified = simplify(R_new_1_expr);

$\left(\begin{array}{ccc} \cos \left(\psi +\theta \right) & 0 & \sin \left(\psi +\theta \right)\\ \sin \left(\psi +\theta \right) & 0 & -\cos \left(\psi +\theta \right)\\ 0 & 1 & 0 \end{array}\right)$

  • $\begingroup$ I've found this explanation the easiest to understand. It's really interesting and revealing that with ϕ = - π/ 2 no matter what ψ angle will be used for Z rotation Y basis (which is typically affected by Z rotation) is already defined as (0,0,1) and won't be affected by Z rotation. And therefore there is a lock rotating around Y axis. $\endgroup$ Commented Oct 22, 2023 at 11:01

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .