JOVANA
Library Glossary Getting Started Three Levels Fields How it works Mission
Join the mission
All guides

Euler Angles, Gimbal Lock, and Quaternions

The friendly three-angle way to name a rotation, the trap it falls into, and the four-number fix engineers actually ship.

Three turns name any orientation

Suppose a drone is hovering and you want to describe which way it is pointing. Its position tells you where it is; its orientation tells you how it is twisted in space. The most intuitive way to name that twist is to break it into three familiar turns: roll (tilt left or right), pitch (nose up or down), and yaw (spin like a compass needle). These three numbers are the Euler angles, and almost everyone reaches for them first because they map onto words a human can picture.

The key idea is that the three turns are sequential: you apply them one after another, each about an axis. Starting from a reference coordinate frame (say, the world's), you first yaw, then pitch, then roll, and where the object lands is its final orientation. Compose those three in order and you have a complete description that is equivalent to a full rotation matrix — just stored as three friendly angles instead of nine numbers.

The trap: gimbal lock

Euler angles are lovely to read, but they hide a sharp edge. Picture three nested rings, each free to spin about one axis — the mechanical gimbals that once stabilized ship compasses and spacecraft instruments. As long as the rings stay at angles to one another, you can reach any orientation. But tip the middle ring 90 degrees and two of the rings line up. Now they spin about the same axis, and one of your three independent turns has quietly stopped doing anything new. This collapse is gimbal lock.

Concretely, in a ZYX (yaw-pitch-roll) convention, pitch the nose straight up to 90 degrees and the yaw axis and roll axis now point the same way. Yaw and roll become indistinguishable — you have lost a degree of freedom in your description, even though the physical drone can still rotate freely in every direction. The object never gets stuck; only your three-angle naming scheme does. The Apollo missions carried a real gimbal-lock concern on their inertial guidance platforms for exactly this reason.

One axis, one angle, no lock

Here is the escape hatch. A deep result, Euler's rotation theorem, says any orientation you can reach by three turns can also be reached by a single turn about one cleverly chosen axis. That gives the axis-angle representation: name a unit vector (the axis to spin about) and one angle (how far to spin). Instead of three sequential turns that can collapse onto each other, it is one honest rotation — and there is no middle ring to line up, so gimbal lock simply cannot happen.

The representation engineers actually ship takes that axis-and-angle idea and packs it into four numbers that compose and interpolate beautifully: the unit quaternion. A quaternion bundles one scalar together with a three-number vector part; for a rotation by angle θ about unit axis (x, y, z), you store the cosine of the half-angle and the axis scaled by the sine of the half-angle. The half-angle looks odd at first, but it is exactly what makes two rotations combine by simple multiplication.

axis-angle:  unit axis n = (x, y, z),  angle theta

unit quaternion q = ( w, vx, vy, vz )
   w  = cos(theta/2)
   vx = x * sin(theta/2)
   vy = y * sin(theta/2)
   vz = z * sin(theta/2)

always keep ||q|| = sqrt(w^2 + vx^2 + vy^2 + vz^2) = 1  (a UNIT quaternion)
q and -q name the SAME rotation
From an axis and angle to the four quaternion numbers. Keeping the length at 1 is what makes it a valid rotation.

Two payoffs make quaternions the default inside robots, game engines, and the IMU code that fuses gyroscope readings. First, there is no gimbal lock — every orientation is represented just as smoothly as any other. Second, you can blend two orientations along the shortest path with a clean operation called spherical linear interpolation (slerp), giving the steady, even camera sweeps and joint motions that Euler angles cannot guarantee. Internally these are all rotations living in the same group, SO(3); the quaternion is just a compact, well-behaved coordinate for them.

Rule of thumb: read in Euler, compute in quaternions

None of these representations is wrong; they are good at different jobs. The practical workflow most robotics teams follow is a clean division of labor: use the friendly one where humans look, and the robust one where the math runs.

  1. Show orientation to a person — a dashboard, a log, a control slider — in Euler angles, because roll, pitch, and yaw are easy to picture and reason about.
  2. The moment you need to store, compose, or interpolate a rotation in code, convert to a unit quaternion and do all the real work there, far from any gimbal-lock singularity.
  3. After each multiplication, re-normalize the quaternion back to length 1 so accumulated floating-point error never drifts it away from a valid rotation.
  4. Convert back to Euler angles only at the very end, for display — and remember that q and -q are the same rotation, so don't be alarmed when the signs flip.