Gimbal Lock & Quaternions

Set the three angles. The gimbal turns, the rings move, and the panel reads out how much of the three-axis freedom is left — then pushes yaw and roll together and apart so you can see which of the two pushes has stopped doing anything.

Loading interactive simulation...

Lesson

The theory — Gimbal Lock & Quaternions

A gimbal is three rings, one inside the next, each free to turn about its own axis. Three rings, three angles, and any orientation you like — until two of the rings line up. Gimbal lock is that moment: the outer and inner rings share a plane, their two angles do the same job, and one whole direction of turn becomes unreachable whichever way the rings are set. A quaternion is the other way to write a rotation, four numbers rather than three, and it has no rings to line up.

What each symbol means

ψ, θ, φ
yaw, pitch and roll — the three ring angles, applied in that order about z, then the new y, then the new x
q
the quaternion (w, x, y, z): w = cos(α/2) for a single turn of α, and the rest sin(α/2) times the axis it turns about
|cos θ|
the freedom left: the size of the determinant of the map from ring rates to how the body turns, 1 when the rings are square and 0 when two are parallel

How to read what you see

The page opens at yaw 30°, pitch 60°, roll 20°. The quaternion row reads 0.8463, 0.0178, 0.5145, 0.1369, and the row below turns that into the one thing it really is: a single turn of 64.4° about an axis close to y. Freedom reads 50.0%, which is cos 60° exactly, and the two probe rows read 5.2° and 19.3° — both still moving, but no longer by the same amount. Drag pitch to 90° and the first of those two falls to 0.0°.

Assumes
That the three turns happen in this order and about these axes. Change the convention to z-y-z and the fold moves to a middle angle of 0° instead of 90°, though it does not go away. Every three-angle scheme has a locked configuration somewhere, which is a theorem about three-dimensional rotation rather than a shortcoming of one convention.
Breaks when
It does not break; it is the point. Apollo's guidance platform carried three gimbals rather than four, so the crew had a region of attitudes to steer around rather than through, and the alternative was a heavier fourth ring. What breaks is the idea that more precision helps: at 90° the third freedom is gone rather than small, and no number of decimal places in yaw or roll will recover it.

Gimbal lock is a slope, and everyone tells it as a cliff 🖖

Drag pitch up from level and watch the freedom row. It is |cos θ|: 86.6% at 30°, 50.0% at 60°, 17.4% at 80°, and 1.7% at 89°. Nothing happens suddenly to that number at 90°; the last degree costs almost nothing, because there was almost nothing left to lose. A platform tracking at 80° of pitch gets a sixth of the turn it got level for the same movement of the rings, and that is the part the word "lock" hides. The figure is the size of the determinant of the map from ring rates to how the body actually turns — the volume of turn per unit of ring movement — so it is already small long before the rings are parallel.

Turn all the way round and the quaternion says you have not 🖖

A quaternion carries cos(α/2) for a turn of α, so it counts half-turns. Rotate 360° about z and it reads −1, 0, 0, 0, not the 1, 0, 0, 0 it started at. Only after 720° does it come home. Both of those describe the same orientation, because q and −q point at the same body — so the quaternion is keeping a record of something the object itself has forgotten. This is not an accident of notation. Interpolation has to be told about it: the shortest-arc rule flips the sign when two quaternions face opposite ways, and without that line a half-second turn takes the long way round.

The long way round is perfectly smooth 🖖

Interpolating Euler angles is usually blamed for making animation wobble, and mostly it does not. Measured from yaw 79.9°, pitch 85.5°, roll 153.5° to yaw 155.5°, pitch 75.0°, roll −139.8°, the two orientations are 16.7° apart and walking the three angles linearly travels 368.1° to get there — twenty-two times too far, at a rate that varies by half a percent from start to finish. Nothing about it looks wrong while it is happening. Over 4,000 random pairs the Euler path was never once shorter than the direct turn, and averaged 1.7 times its length.

Problem solved in full

  1. What is left to control at 90° of pitch 6 steps

    Set pitch to 90° and yaw to 20°, with roll at 0°. Show that adding 10° to yaw and 10° to roll leaves the body exactly where it was, and say what the orientation does still depend on.

    Open this problem in the tool

    1. The rotation is three turns in order: yaw about z, then pitch about the new y, then roll about the new x. Written out, R = R_z(ψ)·R_y(θ)·R_x(φ).

    2. Put θ = 90°. The middle turn carries the x axis onto −z and the z axis onto x, so the first turn and the last one are now being applied about the same line in space. Whatever ψ does, φ can undo.

    3. So the product collapses to a single turn that depends on φ − ψ and on nothing else. Two different pairs with the same difference are the same orientation, not merely a similar one.

    4. Check it. Starting from ψ = 20°, φ = 0°, the difference is −20°. Move to ψ = 30°, φ = 10° and the difference is still −20°.

    5. So those two sets of angles are not merely close. They name the same orientation, and the gap between them is zero — the angle row does not move at all.

    6. Now break the difference instead. ψ = 30°, φ = −10° gives −40°, and the body swings the full 20.0000°.

    Answer

    Adding 10° to both leaves the body exactly where it was — the two orientations differ by 0.0000°, and both are the same single turn of 91.728° about (−0.1711, 0.9703, 0.1711). Adding to one and subtracting from the other moves it the full 20.0000°. What survives at θ = +90° is φ − ψ, one number where there were two, and at θ = −90° it is φ + ψ instead. The third degree of freedom has stopped existing rather than become hard to reach, and no amount of precision in ψ or φ will bring it back.