Why quaternions? Gimbal lock, seen live
Roll, pitch and yaw are easy to read. But when the pitch reaches 90°, they break. Before we learn quaternions, we look at this problem with our own eyes, because it is the whole reason quaternions exist.
1.1Three angles are three rings
Our object has three axes of its own. The x axis points forward, along the white arrow on its top. The y axis points to its left, and the z axis points up. The same three letters are used for the world, which never moves.
With Euler angles we describe the orientation of the object with three turns, one after another:
- Yaw: a turn about z. The object turns left or right.
- Pitch: a turn about y. The front goes down or up.
- Roll: a turn about x. The object tilts sideways.
First yaw, then pitch about the new y, then roll about the newest x. This is called the ZYX order, a very common one.
A gimbal is the mechanical version of this idea. Each ring turns about one axis and carries the next ring. The outer ring is yaw, the middle ring is pitch, and the inner ring is roll. The object sits in the middle.
1.2What is lost at 90°
At pitch 90° or −90°, the roll axis is vertical, on the same line as the yaw axis. Two sliders now do the same thing, and there is one direction the object cannot start turning in. That’s gimbal lock.
You might ask: the object is still somewhere, so what’s the problem? The problem starts when the object moves, and it starts long before 90°. Let’s see it with one simple case.
1.3Near 90°, the rings have to spin faster and faster
Keep the roll at zero, and tilt the object by a pitch angle θ. Now we want the object to turn about its own z axis, the line straight out of its top face. We call the speed of this turn \(\omega_z\) (omega z), in degrees per second. Only the yaw ring can give a turn in this direction.
But the yaw ring turns about the vertical line, and the object’s z axis is tilted away from that line by the angle θ. So only a part of the yaw ring’s speed reaches the object’s z axis. That part is \(\cos\theta\), the same as the shadow of a tilted stick. (The rest of its speed goes into the object’s x axis, and the roll ring has to turn the other way to cancel it.)
ψ̇ (psi with a dot) is the speed of the yaw ring, in degrees per second. θ is the pitch angle.
So the more we pitch, the faster the yaw ring has to spin to give the same turn:
| pitch θ | 0° | 60° | 80° | 89° | 89.9° | 90° |
| yaw ring speed | 1 × | 2 × | 5.8 × | 57 × | 573 × | impossible |
At exactly 90° we divide by zero: no ring speed can produce that turn. That is the lost direction. And close to 90° a tiny error in the turn speed is multiplied by 50 or 500, so yaw and roll jump around wildly. A camera looking straight up, a plane in a vertical climb or a phone pointing at the sky reaches this angle often.
For the curious: the full formula, with roll
With roll included, the idea is the same and only the formula is longer. The object’s turn is the sum of the three ring speeds, each about its own ring axis. Here \(\omega_x, \omega_y, \omega_z\) are the turn speeds about the object’s own x, y and z axes, and φ (roll), θ (pitch), ψ (yaw) are the three angles. Solving for the ring speeds gives:
You don’t need to remember it. Just see the cos θ at the bottom and the tan θ: both become infinitely large at 90°. The full derivation is in the sources below.
1.4What we want instead
We want a way to store the orientation that has no special angle where it breaks. We also want it cheap to compute, and easy to combine and blend. A quaternion does all of this with four numbers:
- No gimbal lock. There is no division by cos θ anywhere.
- Cheap. Updating it for a spinning object uses only multiplications, additions and one square root. No sin or cos.
- Easy to combine. Two rotations together are one multiplication.
- Easy to blend. We can move smoothly from one orientation to another, which is what animation and camera moves need.
We still use Euler angles to show the result to a human. Inside the program, we keep a quaternion. This is what game engines, robots, spacecraft and phones do. That’s the plan for the rest of this tutorial.
✓Check yourself
At which pitch angle does ZYX gimbal lock happen?
At pitch = +90° or −90°. Then the roll axis lines up with the yaw axis.
Is the object itself stuck when the gimbal is locked?
No. The object can still be in any orientation. What is lost is one direction of small movement from that position, and the angle math divides by zero.
+Sources and further reading
- H. A. Hashim, "Special Orthogonal Group SO(3), Euler Angles, Angle-axis, Rodriguez Vector and Unit-Quaternion: Overview, Mapping and Challenges", 2019
sections 4.3 and 4.4 derive the ring-speed formulas, and section 4.5 discusses the singularity at 90° - J. Diebel, "Representing Attitude: Euler Angles, Unit Quaternions, and Rotation Vectors", Stanford University, 2006
the same formulas for every Euler sequence. It uses the opposite, world-to-object direction, so its matrices are the transpose of ours