Axis-Angle and Quaternions
Every 3D rotation, however it was built, is one turn by some angle about some axis. A unit quaternion stores that axis and angle in four numbers, , and then rotating a vector, combining two turns and blending turns all become arithmetic on those four numbers, with no gimbal lock anywhere.
What the symbols mean
- Axis
A unit vector along the axis of the turn.
- Angle
How far to turn, counterclockwise looking down n toward the origin.
- Vector
The vector being turned.
What the symbols mean
- Scalar part
cos of half the angle: 1 for no turn, 0 for a half turn.
- Axis
The unit axis, scaled by sin of half the angle to make (x, y, z).
What the symbols mean
- Vector part
The (x, y, z) of the first quaternion.
What the symbols mean
- Conjugate
The same axis with the opposite turn: q's inverse when q is a unit quaternion.
Explanation
An axis-angle rotation is the simplest description of a turn there is: pick a line through the center (the axis) and say how far to spin around it (the angle). The surprise is that every rotation is one of these, even one built from a messy chain of yaws, pitches and rolls. A quaternion is a way to write that axis and angle as four numbers, : the first number is the "how much" part, and the other three, , are the axis scaled down. The half angles look strange at first, and we'll see exactly why they're there. Everything else is a few multiplications and additions; bear with it.
Euler's rotation theorem. Any rotation of 3D space about the origin leaves some line fixed and turns everything around that line. Here's why. A rotation matrix has perpendicular unit columns, so , and it doesn't mirror, so . Then
using . But flipping every sign of a matrix flips its determinant, . So , which forces . That means for some nonzero : an eigenvector with eigenvalue (Eigenvectors). That vector is the axis. The angle comes from the trace, .
Rodrigues' formula: turning about any axis. Given a unit axis and an angle , where does a vector go? Split into the part along the axis and the part across it:
The part along the axis doesn't move at all. The part across it spins in the plane perpendicular to , and in that plane it's just the 2D rotation. All we need is a second direction in the plane, a quarter turn ahead of , and is exactly that: perpendicular to both and , and the same length as (The Cross Product). So becomes , the same "cos times the old direction plus sin times the quarter-turned one" as in 2D. Add the parallel part back:
and collect the two terms to get Rodrigues' formula. There's an animation further down showing the two parts: one sitting still on the axis, the other sweeping a circle.
Quaternions. In 2D, a unit complex number is a rotation. In 1843 William Rowan Hamilton found the 3D version: numbers with three imaginary units that obey
From those rules, , and , but : order matters, just like it does for rotations. We write with , and multiplying two quaternions out term by term gives the product formula above. Notice both vector products show up: for two pure vectors, .
To rotate by the unit quaternion , write as the quaternion and sandwich it: . Multiply that out and you get Rodrigues' formula with angle . That's why the half angle: acts on twice, once from each side, and each side contributes half the turn. (In 2D a single multiplication was enough. In 3D the one-sided product isn't even a pure vector anymore; the sandwich is what cleans it up.) With the vector part of , engines use this faster form of the same thing:
Combining turns is multiplying. Turn by , then by : , because . So the combined rotation is , with the first turn on the right, the same order rule as matrices.
Two quaternions for every rotation. Put in place of in : the two minus signs cancel, so and turn everything identically. Through the angle, is a turn by : one extra full circle, which lands in the same place. So turning by gives , not , and the quaternion only comes back to where it started after . This is called the double cover. Hart, Francis and Kauffman have a lovely way to see it: tie a belt to a book and turn the book one full turn, and the belt gets a twist you can't undo without turning the book back. Turn it twice and, surprisingly, you can untangle the belt while the book stays still. The quaternion keeps track of that twist. In animation it matters for one practical reason: when you blend, you have to choose between and , and the wrong one turns the long way around (Slerp).
Reading a quaternion. Going back: and . A near means a small turn; means a half turn.
Why games and robots store quaternions. Four numbers instead of a matrix's nine. Combining two rotations costs 16 multiplications instead of 27. After thousands of combinations rounding errors creep in, and a quaternion is fixed by dividing by its length, while a drifting matrix has to be re-squared column by column. There's no gimbal lock: the four numbers change smoothly through every orientation. And quaternions blend beautifully, which is the next lesson. The product in code:
type Quat = [number, number, number, number]; // (w, x, y, z)
function mul([a, b, c, d]: Quat, [e, f, g, h]: Quat): Quat {
return [a*e - b*f - c*g - d*h, a*f + b*e + c*h - d*g, a*g - b*h + c*e + d*f, a*h + b*g - c*f + d*e];
}
A common mistake. Building the quaternion with the full angle, . It's tempting because the 2D version uses the full angle. In 3D the sandwich uses twice, so that quaternion turns things by . A turn about is ; the quaternion is a turn. And the axis must be a unit vector: if isn't unit length, the sandwich stretches everything by as well as turning it.
See it
Worked examples
Turn by about the axis , using Rodrigues' formula.
- 1
Collect the trig values: , , so .
Do the same rotation with a quaternion: write for about and compute for .
- 1
Half the angle is , so .
Practice
Type your answer and check it. Solve them all to complete the lesson.
Write the unit quaternion for a turn about the -axis.
A unit quaternion is . Through what angle does it rotate, in degrees?
Use Rodrigues' formula to turn by about the unit axis . Enter .
Multiply the quaternions , written as .
A quaternion arrives from a physics engine as . Rewrite it with (the same rotation) and give its rotation angle in degrees, between and .
A satellite's camera points straight down, along . Its attitude controller applies the unit quaternion . Using , where does the camera point now?
The matrix is a rotation: its columns are perpendicular unit vectors and . Find its axis (an eigenvector with eigenvalue 1) and its angle (from ), then write it as a unit quaternion with .
Which quaternion describes exactly the same rotation as ?
The hardest one here. Get it, and you've got this lesson.
A robot wrist turns about the -axis and then about the -axis (both fixed world axes). Find the single unit quaternion , with , for the combined rotation.
Where it's used
Every major game engine, 3D modeling tool and robotics library stores orientations as unit quaternions: character joints, cameras, physics bodies. Spacecraft attitude control does too, because a gimbal lock in orbit is not an option and the four numbers integrate cleanly from gyroscope readings. Phones fuse their gyroscope, accelerometer and compass readings into a quaternion many times a second to know which way the screen faces. When you drag a rotation gizmo in a 3D editor, the tool turns your mouse motion into an axis and an angle, builds a quaternion, and multiplies it onto the object's current one.