Quaternion Rotation, Explained
After reading this you will be able to write down the unit quaternion for any axis-angle rotation, rotate a vector by hand with the sandwich product, and explain why slerp between two quaternions gives a smooth, gimbal-free turn.
What a quaternion rotation is
A quaternion is a four-number object q = (w, x, y, z), one real part and three imaginary parts. For rotation you only ever use quaternions of length one, called unit quaternions. Every unit quaternion encodes exactly one rotation of 3D space, and it does so without ever picking a set of axes to spin around in sequence. That last property is the reason games and spacecraft use them.
Here is the hook. Point an axis straight up the y-axis, \hat{a} = (0, 1, 0), and rotate a cube by 90^\circ. The quaternion that does this is q = (\cos 45^\circ, 0, \sin 45^\circ, 0) \approx (0.7071, 0, 0.7071, 0). The front face of the cube swings to the right. Notice there are no three angles to set, no order to argue about. One axis, one angle, four numbers.
The tool draws the cube, spins it, and prints these four numbers so you can watch the digits change as you drag the axis and the angle.
When to use quaternions, and when not to
Use quaternions when you need to compose many rotations, interpolate between orientations, or avoid the failure mode called gimbal lock. Gimbal lock happens with Euler angles: when the middle rotation reaches 90^\circ, two of your three axes line up and you lose a degree of freedom. A quaternion has no axes to collapse, so it never locks.
Do not reach for quaternions when a single fixed 2D rotation is all you need, or when a human has to read and type the orientation by hand. Nobody thinks in quaternion coordinates. For authoring, Euler angles like "pitch 30, yaw 45" are friendlier. The standard pattern is to let people edit Euler angles, then convert to a quaternion for storage and math.
A rotation matrix also avoids gimbal lock, but it carries nine numbers with six constraints between them. A quaternion carries four numbers with one constraint (unit length). It is cheaper to store, cheaper to renormalize, and much cheaper to interpolate.
The formula and why the half-angle appears
The rotation by angle \theta about a unit axis \hat{a} = (a_x, a_y, a_z) is the quaternion below.
The real part w = \cos(\theta/2) measures how little you turn: w = 1 means no rotation. The vector part points along the axis and grows with the angle. The half-angle looks strange until you see how a quaternion actually moves a vector.
To rotate a 3D vector v, you write it as a quaternion with zero real part and apply the sandwich product.
Here q^{-1} is the inverse, which for a unit quaternion is just the conjugate (w, -x, -y, -z). The vector q hits from the left and q^{-1} hits from the right. Because the rotation is applied twice, once on each side, each factor only needs to carry half the angle. That is where \theta/2 comes from. It is not a convention; it falls out of the two-sided product.
One consequence: q and -q give the same rotation, because both minus signs cancel in the sandwich. The set of unit quaternions is a 4D sphere, and it covers each 3D rotation twice. This double cover matters when you interpolate, as you will see.
A worked example reproducing the demo
A 90-degree turn about the vertical axis
Take the demo defaults: axis \hat{a} = (0, 1, 0) and angle \theta = 90^\circ. Work out the quaternion, then rotate one corner of the cube.
- Half the angle: \theta/2 = 45^\circ, so \cos 45^\circ = 0.7071 and \sin 45^\circ = 0.7071.
- Build the quaternion: q = (0.7071,\ 0 \cdot 0.7071,\ 1 \cdot 0.7071,\ 0 \cdot 0.7071) = (0.7071, 0, 0.7071, 0).
- Check the length: 0.7071^2 + 0.7071^2 = 0.5 + 0.5 = 1. It is a unit quaternion.
- Rotate the corner v = (1, 0, 0). A rotation of 90^\circ about the y-axis sends the x-axis to the negative z-axis, so the answer should be (0, 0, -1).
- Confirm with the sandwich q v q^{-1}: the algebra gives v' = (0, 0, -1), matching the geometric expectation.
So the corner that started at (1,0,0) ends at (0,0,-1). That is a quarter turn, and the four printed numbers are 0.7071, 0, 0.7071, 0.
The chart below shows how the real part w = \cos(\theta/2) shrinks as you crank the angle from 0^\circ to 360^\circ. At 360^\circ the real part is -1, not +1: a full turn lands on -q, the other copy of the identity.
Reading the four numbers
Once you can decode the printed quaternion, the tool stops being mysterious. Split the four numbers into the real part and the vector part.
- Real part w
- Equals \cos(\theta/2). Read the angle off it: \theta = 2\arccos(w). If w = 0.7071, then \theta = 2 \times 45^\circ = 90^\circ.
- Vector part (x, y, z)
- Points along the rotation axis, scaled by \sin(\theta/2). Divide by that factor to recover the unit axis. For (0, 0.7071, 0) divide by 0.7071 to get axis (0,1,0).
- Length
- Always \sqrt{w^2 + x^2 + y^2 + z^2} = 1 for a valid rotation. If it drifts from 1 after many multiplications, renormalize.
The animation slerps from the identity (1,0,0,0) to your target. Slerp (spherical linear interpolation) walks the great circle of the quaternion sphere at constant angular speed. The formula for interpolation parameter t from 0 to 1 is below.
Here \Omega is the angle between the two quaternions on the sphere, found from their dot product: \cos\Omega = q_0 \cdot q_1. At t = 0 you get q_0; at t = 1 you get q_1; in between the cube turns at a steady rate.
Common mistakes
Three errors account for most quaternion bugs.
Forgetting to normalize the axis. The half-angle formula assumes \hat{a} has length one. If you feed in (0, 2, 0) instead of (0, 1, 0), the resulting quaternion is not unit length and the rotation is wrong. Divide the axis by its length first.
The second mistake is slerping without checking the sign. Because q and -q are the same rotation, two orientations that look close can have a negative dot product. If q_0 \cdot q_1 \lt 0, flip one of them to -q_1 before you slerp. Skip this and the cube takes the long way around, spinning nearly 360^\circ to reach an orientation 10^\circ away.
The third is confusing the half-angle. People plug the full angle into \cos and get w = \cos 90^\circ = 0 for a quarter turn, which is actually a half turn. Always halve first: w = \cos 45^\circ = 0.7071.
The table shows the correct quaternion for a few angles about the y-axis, so you can spot-check your own arithmetic.
| angle | theta/2 | w | y component | rotation of (1,0,0) |
|---|---|---|---|---|
| 0 deg | 0 deg | 1 | 0 | (1, 0, 0) |
| 90 deg | 45 deg | 0.7071 | 0.7071 | (0, 0, -1) |
| 180 deg | 90 deg | 0 | 1 | (-1, 0, 0) |
| 270 deg | 135 deg | -0.7071 | 0.7071 | (0, 0, 1) |
| 360 deg | 180 deg | -1 | 0 | (1, 0, 0) |
What this toy does and does not show
The visualizer is honest about one thing and quiet about another. It shows correctly that a quaternion is a real orientation and that slerp is smooth. It does not show floating-point drift, which in real code slowly pulls the quaternion off unit length after thousands of multiplications, forcing a periodic renormalize. It also hides the fact that composing rotations means multiplying quaternions, which is not commutative: turning right then up differs from up then right. The tool animates a single rotation from the identity, so you never see that non-commuting behavior directly. Keep both caveats in mind before you trust a mental model built only from this page.
Related tools
To see a 4D object cast a 3D shadow the way a quaternion lives in 4D, spin the Tesseract projection. For the flat cousin of rotation, drag the matrix in the Linear transformation playground and watch a 2D grid rotate and shear. The Platonic solids explorer spins the same kind of 3D shapes and checks Euler's formula on each. If rotating circles interest you, the Fourier epicycles chain redraws any shape from stacked rotations in the plane.
Frequently asked questions
Why do quaternions avoid gimbal lock but Euler angles do not?
Euler angles apply three rotations in sequence around fixed axes. When the middle angle reaches 90^\circ, the first and third axes align and you lose a degree of freedom. A quaternion stores one axis and one angle with no ordering, so there is no configuration where axes collapse.
Why does a full 360-degree turn give -1 instead of coming back to 1?
The real part is \cos(\theta/2), so at \theta = 360^\circ you get \cos 180^\circ = -1. Since q and -q describe the same rotation, the cube looks identical. You need 720^\circ to return the quaternion itself to (1,0,0,0). This is the double cover.
How do I get the angle back out of a quaternion?
Use \theta = 2\arccos(w), where w is the real part. For w = 0.7071 this gives 2 \times 45^\circ = 90^\circ. Recover the axis by dividing the vector part by \sin(\theta/2).
Is slerp always the shortest rotation between two orientations?
Yes, as long as you fix the sign first. Check the dot product q_0 \cdot q_1; if it is negative, replace q_1 with -q_1 so you interpolate along the short arc. Without that check slerp can take the long way around.
Do I have to normalize after every multiplication?
Not every one, but periodically. Each multiplication introduces tiny floating-point error, and after a few thousand the length can drift from 1 by a fraction of a percent. Renormalizing (dividing by the length) once per frame keeps rotations clean.