4. Quaternions

The mathematically elegant way to rotate in 3D — no gimbal lock, smooth interpolation, constant speed

Euler angles are intuitive but break at the poles. Rotation matrices work but are hard to interpolate smoothly. Quaternions solve both problems with four numbers and a bit of 19th-century algebra.

Every modern game engine, every animation system, and every spacecraft attitude controller uses quaternions. They're not as scary as their reputation suggests.

What's Wrong with Matrices for Animation?

Rotation matrices are great for applying rotation. But they're awkward for blending between two orientations:

  • You can't just average two rotation matrices — the result isn't a valid rotation
  • Interpolating Euler angles produces weird arcs and sudden flips
  • Gimbal lock makes some paths impossible with Euler angles

What we need is a representation where you can smoothly slide from one orientation to another at constant angular speed. That's exactly what quaternions provide.

What Is a Quaternion?

A quaternion is four numbers: one real part and three imaginary parts.

Quaternion form

q = w + xi + yj + zk

Or simply: q = (w, x, y, z) where w is the scalar part and (x, y, z) is the vector part.

The imaginary units i, j, k follow Hamilton's rules:

i² = j² = k² = ijk = -1
ij = k,  jk = i,  ki = j
ji = -k, kj = -i, ik = -j

These look arbitrary, but they encode exactly the right behavior for 3D rotation.

Quaternions as Rotations

A rotation of angle θ around a unit axis (ax, ay, az) is represented by:

Axis-angle to quaternion

q = (cos(θ/2), sin(θ/2)*ax, sin(θ/2)*ay, sin(θ/2)*az)

The half-angle θ/2 is not a typo — it's fundamental. It's why quaternions use 4D to represent 3D rotation: the "double cover" of the rotation group means every rotation has exactly two quaternion representations (q and -q produce the same rotation).

Applying a Quaternion Rotation

To rotate a 3D point p = (px, py, pz), embed it as a pure quaternion p' = (0, px, py, pz), then compute:

p_rotated = q × p' × q*

where q* is the conjugate (w, -x, -y, -z). The result's vector part gives the rotated point.

Quaternion Multiplication

Multiplying two quaternions a and b:

w = a.w*b.w - a.x*b.x - a.y*b.y - a.z*b.z
x = a.w*b.x + a.x*b.w + a.y*b.z - a.z*b.y
y = a.w*b.y - a.x*b.z + a.y*b.w + a.z*b.x
z = a.w*b.z + a.x*b.y - a.y*b.x + a.z*b.w

This is 16 multiplications and 12 additions. Composing two rotations is just one quaternion multiply — cleaner and cheaper than multiplying two 3x3 matrices (27 multiplications).

Why Quaternions Beat Euler Angles

The comparison

Property Euler Angles Quaternions
Storage3 floats4 floats
Gimbal lockYesNo
CompositionConvert to matrix firstMultiply directly
Smooth interpolationNo (weird arcs)Yes (SLERP)
RenormalizationN/ACheap (divide by length)
IntuitiveVeryLess so

SLERP: Spherical Linear Interpolation

The killer feature of quaternions is SLERP — smoothly blending between two orientations at constant angular velocity.

Given quaternions q0 and q1 and a parameter t from 0 to 1:

Ω = arccos(q0 · q1)
slerp(q0, q1, t) = q0 * sin((1-t)Ω) / sin(Ω) + q1 * sin(tΩ) / sin(Ω)

Why SLERP works

Unit quaternions live on the surface of a 4D sphere. SLERP traces the great circle between two points on that sphere — the shortest path. This produces constant angular velocity with no wobble, no sudden jumps, and no gimbal lock. It's why every animation system uses it.

The Dot Product Check

Before SLERP, check the dot product of q0 and q1. If it's negative, negate one quaternion — this ensures you take the short path around the sphere instead of the long way around.

if (q0 · q1 < 0) {
    q1 = -q1
}
Interactive

SLERP vs linear interpolation

Left: SLERP (constant speed, smooth arc). Right: naive Euler lerp (variable speed, wobble).

Converting Quaternion to Matrix

To send a rotation to the GPU (or to use it with the rest of your transform pipeline), convert the quaternion to a 3x3 rotation matrix:

Given q = (w, x, y, z):

| 1-2(y²+z²)   2(xy-wz)    2(xz+wy)  |
| 2(xy+wz)    1-2(x²+z²)   2(yz-wx)  |
| 2(xz-wy)    2(yz+wx)    1-2(x²+y²) |

This is cheaper than constructing the matrix from Euler angles (no trig calls — just multiplications and additions since w, x, y, z already encode the sine and cosine via the half-angle).

Quaternion to/from Euler Angles

Sometimes you need Euler angles for UI (humans think in degrees, not quaternions). The conversion:

Euler → Quaternion:

qx = (sin(pitch/2), 0, 0, cos(pitch/2))
qy = (0, sin(yaw/2), 0, cos(yaw/2))
qz = (0, 0, sin(roll/2), cos(roll/2))
q = qz × qx × qy   // depends on your convention

Quaternion → Euler is more complex and involves atan2 — and it can still produce gimbal lock in the extracted angles. But the internal representation stays smooth.

Practical Tips

  1. Always normalize

    After many multiplications, floating-point drift makes quaternions non-unit. Renormalize periodically: q = q / |q|. This is just dividing by sqrt(w²+x²+y²+z²).

  2. Use SLERP for animation, quaternion multiply for composition

    SLERP blends between two orientations. Quaternion multiplication applies one rotation on top of another. Don't confuse them.

  3. Store orientation as a quaternion, display as Euler angles

    Keep the canonical orientation as a quaternion internally. Convert to Euler only for debugging or UI display.

  4. q and -q are the same rotation

    This is the double-cover property. It rarely causes bugs, but be aware of it when comparing quaternions or computing SLERP.

Interactive

Quaternion rotation explorer

The Full Picture

At this point you have the complete rotation toolkit:

  1. Sin/cos formulas — simple, direct, good for understanding what rotation is
  2. Matrices — composable, efficient for batch transforms, the GPU's native format
  3. Quaternions — compact, interpolatable, gimbal-lock-free, the standard for storing orientation

Real engines use all three: quaternions for storing and blending orientations, matrices for the GPU transform pipeline, and Euler angles for human-facing UI.

Key Takeaways

What to remember

  • A quaternion (w, x, y, z) represents a rotation of 2*arccos(w) around the axis (x, y, z)/sin(arccos(w)).
  • SLERP interpolates between two orientations along the shortest path at constant speed.
  • Quaternion multiplication composes rotations — cheaper than matrix multiply.
  • Quaternions have no gimbal lock because they don't decompose rotation into three sequential axis rotations.
  • Convert to a matrix when you need to send the rotation to the GPU.