Quaternions
Geometry & Trigonometry
Chapter 7 · Quaternions
Chapter 6 verified a real, permanent loss of a degree of freedom whenever an orientation is built from three sequential Euler-angle rotations at pitch=90°. Quaternions are the standard practical fix used throughout real games, robotics, and computer-vision code — and this chapter treats them as a genuinely usable tool built from four numbers you can compute and check by hand, not a black box to be imported and trusted blindly.
What a Quaternion Actually Is
A unit quaternion representing a rotation of angle θ around a unit axis (aₓ,a_y,a_z) is four numbers: q = (cos(θ/2), aₓ·sin(θ/2), a_y·sin(θ/2), a_z·sin(θ/2)). The first component is often called w; the other three, (x,y,z), encode the rotation axis scaled by sin(θ/2). Notice the half-angle — a genuinely easy-to-forget detail that trips up a first implementation.
90° rotation around the z-axis: q = (0.7071, 0, 0, 0.7071) — cosine and sine of the half-angle, 45°. Rotating the vector (1,0,0) using the standard formula v' = q·v·q⁻¹ (treating v as a "pure" quaternion (0,vₓ,v_y,v_z)) gives exactly (0, 1, 0) — matching the Rz(90°) matrix rotation from Chapter 5 precisely.
Composing Rotations: Quaternion Multiplication Reproduces the Matrix Result Exactly
Quaternions compose the same way rotation matrices do: multiplying two quaternions gives the quaternion for the combined rotation. The real test is whether this actually reproduces Chapter 6's own matrix results, number for number.
Rx(90°) and Ry(90°), then rotating (0,0,1): applying Rx then Ry gives (0, −1, 0); applying Ry then Rx gives (1, 0, 0) — exactly Chapter 6's own verified matrix results, to the same precision. Quaternions aren't a different kind of rotation from matrices; they're a different encoding of the identical underlying rotations, verified to agree completely.
What Quaternions Actually Fix — and an Honest Limit
It's tempting to say "quaternions eliminate gimbal lock." That's almost right, and the precise version matters.
(yaw=30°, pitch=90°, roll=10°) and for (yaw=40°, pitch=90°, roll=20°) — the same yaw−roll=20° pair Chapter 6 verified collapses to one matrix — produces, verified directly, the same quaternion in both cases (matching to floating-point precision). Converting either one back into Euler angles for display recovers the identical (yaw=180°, pitch=90°, roll=180°) — the individual yaw and roll values are gone, exactly as they were for the matrix version. This isn't a shortcoming of quaternions specifically: at that exact orientation, the original (yaw,roll) distinction was never real information the orientation itself carried. Quaternions fix the numerical behavior of composing and interpolating rotations; they don't — and can't — restore information that a particular Euler-angle decomposition never uniquely had in the first place.
A Second Real Advantage: Cheap Renormalization
Chapter 5 verified that repeatedly composing rotation matrices drifts away from a true rotation, needing an involved re-orthonormalization to fix. A unit quaternion has exactly one constraint to maintain: its magnitude must stay 1.
200,000 times (mirroring Chapter 5's own matrix experiment): the resulting quaternion's magnitude drifts to 1.000000000009458, instead of exactly 1, with a maximum component difference of ≈9.34×10⁻¹² from a fresh direct computation. Fixing this needs only one operation — dividing all four components by the quaternion's own magnitude — after which the difference from the direct computation shrinks to ≈1.88×10⁻¹⁴, roughly 500× better, from a single square root and four divisions. Correcting a drifted rotation matrix instead requires re-orthonormalizing an entire 3×3 matrix (typically via Gram-Schmidt across all three rows) — genuinely more arithmetic for a comparable fix.
Where This Connects
| This chapter's finding | What it sets up |
|---|---|
| Quaternion multiplication verified to exactly match matrix composition | Confirms Chapter 8's coordinate transformations can freely mix quaternion-based and matrix-based rotation representations, since they compute identical results |
| Quaternions stay numerically smooth through what would be a gimbal-lock orientation | The practical reason virtually every animation system interpolates camera/character orientation with quaternions (spherical linear interpolation, "SLERP") rather than interpolating Euler angles directly — a natural extension beyond this course's own scope |
| Cheap renormalization vs. expensive matrix re-orthonormalization | A concrete, quantified reason real engines default to quaternions for any orientation that's updated incrementally over many frames |
Hands-On Exercises
Using this chapter's own quaternion-construction formula, build the quaternion for a 180° rotation around the y-axis, and use it to rotate the point (0,0,1). Confirm your result matches what you'd expect from the Ry(180°) rotation matrix directly.
A developer claims "since I switched my engine's orientation representation to quaternions, gimbal lock is completely impossible in my game now, even in the UI that displays the camera's yaw/pitch/roll to the player." Using this chapter's own verified findings, explain what part of this claim is correct and what part is not.
📄 View solutionUsing this chapter's own verified drift-and-renormalization numbers, explain why a game engine might choose to renormalize an object's orientation quaternion every single frame, even though the drift after just one frame's worth of composition is far too small to be visible.
📄 View solutionChapter 7 Quick Reference
- Quaternion from axis-angle:
q = (cos(θ/2), aₓsin(θ/2), a_ysin(θ/2), a_zsin(θ/2))— note the half-angle - Verified: quaternion rotation of
(1,0,0)by90°aroundzmatches theRz(90°)matrix result exactly,(0,1,0) - Verified: quaternion multiplication exactly reproduces Chapter 6's own matrix non-commutativity results —
(0,−1,0)and(1,0,0)for the two rotation orders - Quaternions fix the numerical behavior of composing/interpolating rotations near a gimbal-lock orientation — verified, they don't restore Euler-angle information that was never uniquely recoverable there in the first place (both
(30°,10°)and(40°,20°)yaw/roll pairs collapse to the identical quaternion) - Verified: quaternion drift after 200,000 compositions (magnitude
1.000000000009458) is fixed to≈1.88×10⁻¹⁴error by one cheap renormalization — versus a full matrix re-orthonormalization needed for the equivalent matrix drift - Next chapter: Coordinate systems and transformations — world/local/camera/screen space, building on this chapter's own rotation machinery