# The gimbal lock shows up in my quaternions

I suspect this is a bit basic for mathoverflow, seeing I'm still just an undergraduate

I've been playing around with quaternions as means to eliminate the gimbal lock. From what I understand, one place the gimbal lock occurs is when you rotate $\frac{\pi}{2}$ around the y-axis. If I create two rotation matrices, $R_{1}$ rotates first $\phi$ around x-axis and $\frac{\pi}{2}$ around the y-axis, while $R_{2}$ rotates first $\frac{\pi}{2}$ around the y-axis and then $\theta$ around the z-axis.

$$$R_{1} = R_{z}(0) R_{y}(\frac{\pi}{2}) R_{x}(\phi) \\ = \begin{bmatrix} 0 & 0 & 1 \\ 0 & 1 & 0 \\ -1 & 0 & 0 \end{bmatrix} \begin{bmatrix} 1 & 0 & 0 \\ 0 & \cos(\phi) & -\sin(\phi) \\ 0 & \sin(\phi) & \cos(\phi) \end{bmatrix} \\ = \begin{bmatrix} 0 & \sin(\phi) & \cos(\phi) \\ 0 & \cos(\phi) & -\sin(\phi) \\ -1 & 0 & 0 \end{bmatrix},$$$

$$$R_{2} = R_{z}(\theta) R_{y}(\frac{\pi}{2}) R_{x}(0) \\ = \begin{bmatrix} \cos(\theta) & -\sin(\theta) & 0 \\ \sin(\theta) & \cos(\theta) & 0 \\ 0 & 0 & 1 \\ \end{bmatrix} \begin{bmatrix} 0 & 0 & 1 \\ 0 & 1 & 0 \\ -1 & 0 & 0 \end{bmatrix} \\ = \begin{bmatrix} 0 & -\sin(\theta) & \cos(\theta) \\ 0 & \cos(\theta) & \sin(\theta) \\ -1 & 0 & 0 \end{bmatrix}.$$$

Since $R_{1} = R_{2}^{-1} \Rightarrow R_{1}(\theta) = R_{2}(-\theta)$, we've lost a degree of freedom. Which is what I expect.

From what I understand, if I perform the same rotations using quaternions, I should be avoiding the gimbal lock?

$Q_{1} = Q_{z}(0) \times Q_{y}(\frac{\pi}{2}) \times Q_{x}(\theta) = (1, 0, 0, 0) \times (\frac{1}{\sqrt(2)}, 0, \frac{1}{\sqrt(2)}, 0) \times (\cos\frac{\theta}{2}, \sin\frac{\theta}{2}, 0, 0)\\ = \frac{1}{\sqrt(2)}(\cos\frac{\theta}{2}, \sin\frac{\theta}{2}, \cos\frac{\theta}{2}, -\sin\frac{\theta}{2})$

$Q_{2} = Q_{z}(\phi) \times Q_{y}(\frac{\pi}{2}) \times Q_{x}(0) = (\cos\frac{\phi}{2}, 0, 0, \sin\frac{\phi}{2}) \times (\frac{1}{\sqrt(2)}, 0, \frac{1}{\sqrt(2)}, 0) \times (1, 0, 0, 0) \\ = \frac{1}{\sqrt(2)}(\cos\frac{\phi}{2}, -\sin\frac{\phi}{2}, \cos\frac{\phi}{2}, \sin\frac{\phi}{2})$

By setting $\phi = -\theta$, $Q_{2}$ becomes

$Q_{2} = \frac{1}{\sqrt(2)}(\cos\frac{-\theta}{2}, -\sin\frac{-\theta}{2}, \cos\frac{-\theta}{2}, \sin\frac{-\theta}{2})$ which due to trig properies becomes $Q_{2} = \frac{1}{\sqrt(2)}(\cos\frac{\theta}{2}, \sin\frac{\theta}{2}, \cos\frac{\theta}{2}, -\sin\frac{\theta}{2})$

Which means that $Q_{1}$ and $Q_{2}$ rotates around the same axis only in the oppsite direction, and we've lost a degree of freedom (??). Am I missing something fundamental?

-
While a lot of people will say to ask this on MSE, I would personally like to see the question answered here. – Steven Gubkin May 3 '12 at 19:12

There's no paradox here: you did the same calculation in two different ways and got the same answer, as you should. The issue is how to think about gimbal lock.

How should you represent a rotation in three dimensions? You can try using Euler angles to represent it using three rotation angles, but there's something fishy about this. That naturally parametrizes a three-dimensional torus, but the rotation group is not a torus (rather, it's a projective space). It doesn't even have a torus as a covering space, but rather a 3-sphere. So the problem is that the naive coordinates just don't give the right topology, and therefore something must go wrong in degenerate cases to fix the topology. Gimbal lock is essentially a name for what goes wrong.

When people say quaternions avoid gimbal lock, they mean the unit quaternions naturally form a 3-sphere, so there are no topology issues and they give a beautiful double cover of the rotation group (via a very simple map). Keeping track of a unit quaternion is fundamentally a more natural way to describe a rotation than keeping track of three Euler angles.

On the other hand, if you describe your quaternion via Euler angles, then gimbal lock shows up again, not in the quaternions themselves but in your coordinate system for them. That's what you are seeing in your calculations: you are doing a standard calculation to see the effects of gimbal lock, and then redoing the same calculation using quaternions.

Some explanations of gimbal lock don't distinguish clearly between the underlying geometry/topology and the choice of coordinates, which has always annoyed me, since that's essential for understanding what's going on mathematically.

-