Complex numbers had a trick — Hamilton wanted a bigger one
Multiplying a 2D point by e^(iθ) rotates it by θ — complex numbers give 2D rotation for free. William Rowan Hamilton spent over a decade in the 1830s and 40s trying to find a three-dimensional number system that did the same trick for 3D. The breakthrough, carved into a Dublin bridge in 1843, was to stop looking for three imaginary units and use four real numbers instead: i² = j² = k² = ijk = −1. The resulting quaternions ℍ turned out to be exactly the right shape for representing 3D orientation — no singularities, smooth interpolation, and only four numbers to store instead of the nine entries of a rotation matrix.
A quaternion is written q = w + xi + yj + zk, or as a pair (w, v) where w is the scalar part and v = (x, y, z) is the vector part. Multiplication is non-commutative — pq ≠ qp in general — which is not a defect, it mirrors the physical fact that rotating around X then Y gives a different result than Y then X.
The rotation sandwich
To rotate a vector v by angle θ around a unit axis n̂, build a unit quaternion from half the angle:
q = cos(θ/2) + n̂ · sin(θ/2) // embed v as a pure quaternion (0, v), then sandwich it: v' = q · (0, v) · q⁻¹ // v' is also a pure quaternion — its vector part is the rotated v. // composing q1 then q2: apply q2·q1, right to left
The half-angle is not an arbitrary convention — it falls directly out of the algebra, because the sandwich applies the rotation twice (once from each side of v). It also explains a subtlety worth knowing: unit quaternions live on the 3-sphere S³ in ℝ⁴, which double-covers the group of 3D rotations SO(3). A quaternion q and its negative −q produce exactly the same rotation, so a full 360° turn of the object corresponds to the quaternion tracing only half of its great circle.
Gimbal lock: the problem quaternions were built to avoid
Euler angles represent an orientation as three sequential rotations about coordinate axes — yaw, pitch, roll. It reads naturally to humans, but it has a structural flaw: whenever the middle rotation reaches ±90°, the first and third axes swing into alignment with each other. Two of the three rotational degrees of freedom collapse into one, and an entire direction of motion becomes unreachable without a discontinuous jump. This is gimbal lock — the same failure that famously worried the Apollo guidance computer team, and the same failure that makes a rigged character's shoulder joint suddenly flip when animated past a pole.
No choice of rotation order removes gimbal lock — it just relocates the singular orientation. Quaternions sidestep the whole issue because they parametrise SO(3) with a smooth, singularity-free four-dimensional embedding (modulo the harmless q / −q double cover), so there is no orientation at which the representation itself breaks down.
SLERP: interpolating along the sphere, not through it
Blending two orientations naively — linearly interpolating the four numbers of q₀ and q₁ — drifts the result off the unit sphere and has to be renormalised, which produces uneven, "wobbly" angular speed. SLERP (spherical linear interpolation) instead walks along the great-circle arc between q₀ and q₁ at constant angular velocity:
Ω = arccos(q0 · q1) // half the angle between the two rotations slerp(q0, q1, t) = sin((1−t)Ω)/sin(Ω) · q0 + sin(tΩ)/sin(Ω) · q1 // if Ω ≈ 0, use NLERP instead to avoid a division by ~0: nlerp(q0, q1, t) = normalise((1−t)·q0 + t·q1)
SLERP is what makes camera pans, character animation blending and spacecraft attitude manoeuvres look smooth rather than jerky. Because it costs a couple of trig calls per frame, engines fall back to the cheaper NLERP when the two orientations are already close together, where the visual difference is negligible.
How the simulation here compares them
The simulation on this site drives two wireframe cubes from the same yaw/pitch/roll sliders — one accumulates the rotation as Euler angles, the other converts each step into a quaternion and applies the sandwich formula. Drag the pitch slider toward 90° and the Euler cube visibly locks: yaw and roll start rotating it around the same axis. The quaternion cube keeps tracking smoothly through the same orientation with no special case at all. Toggling SLERP shows the same contrast for interpolation between two arbitrary orientations, rather than sliders held at constant values.
Frequently asked questions
Why can't Euler angles just be fixed instead of switching to quaternions?
Gimbal lock is not a bug in a particular implementation, it is a structural property of representing 3D orientation as three sequential axis rotations: whenever the middle rotation reaches 90 degrees, two of the three rotation axes become parallel and a degree of freedom is lost. No choice of rotation order removes the singularity, it only moves it to a different orientation. Quaternions avoid it entirely because they parametrise rotations with four numbers on a sphere, with no axis-alignment case to trigger.
Why do quaternions use half the rotation angle, θ/2, instead of θ?
The rotation is applied as a sandwich product q·v·q⁻¹, and each multiplication by q contributes a rotation of θ/2 in the underlying algebra, so the two multiplications combine to a full rotation of θ. This also explains the double cover: q and −q both produce the sandwich product for the same rotation, so a full 360° turn of the physical object corresponds to only a 180° traversal of the quaternion around its sphere.
When should I use SLERP instead of just averaging two quaternions?
A naive linear blend of two unit quaternions drifts off the unit sphere and has to be renormalised, which produces uneven angular speed — the interpolated rotation slows down near the endpoints and speeds up in the middle. SLERP moves along the great-circle arc between the two orientations at constant angular velocity, which is what smooth camera pans and animation blending need. For very small angles between the quaternions, a normalised linear blend (NLERP) is a cheaper and visually indistinguishable substitute.
Try it live
Everything above runs in your browser — open Quaternion Rotation and drag the pitch slider toward 90° to watch the Euler cube lock up while the quaternion cube keeps tracking smoothly. Nothing is installed, nothing is uploaded, the whole model lives in one tab.
▶ Open Quaternion Rotation simulation