For unit quaternions, how do you rotate a vector, compose two rotations in the right order, and why is the conjugate the inverse?
answer
- half the angle in the quaternion
- the sandwich product
- product order is application order reversed
- q times its conjugate
basics
~20 sA unit quaternion q = (cos(a/2), sin(a/2) n) rotates a vector by the sandwich q v q*; the product q2 q1 applies q1 first; and since q q* = 1, the conjugate (w, -x, -y, -z) is the inverse.
solid answer
~50 sWrite `q = (w, x, y, z)` with `w` the scalar part. A rotation by angle `a` about unit axis `n` is `q = (cos(a/2), sin(a/2)*n)`, and a vector rotates as the sandwich `v' = q v q*`, treating `v` as `(0, v)`. Because of the sandwich, `q2 q1` applied to `v` gives `q2 (q1 v q1*) q2*`, so the right-hand factor acts first; the Hamilton product is not commutative, so the order matters. For an orientation that maps body to world, `q r` rotates about the body's own axes and `r q` about world axes. The conjugate `q* = (w, -x, -y, -z)` satisfies `q q* = (w² + x² + y² + z², 0)`, which is `(1, 0)` for a unit quaternion, so it is the inverse; in general the inverse is `q*/|q|²`. Repeated products drift off unit length, so renormalise.
go deeper
Recall the half-angle form, that a vector rotates by the sandwich q v q*, and that the conjugate of a unit quaternion is its inverse.
Explain from the sandwich why q2 q1 applies q1 first, and when multiplying on the right rotates about local axes rather than world axes.
Diagnose controls that feel wrong once an object is banked by the side of multiplication, and keep long update chains unit length so the conjugate stays the inverse.
Fix one component order and one product convention for the codebase, document the local-versus-world rule, and test conversions at the boundaries where data arrives in another layout.
## A quaternion as a rotation A **quaternion** is a four-number value written here as `q = (w, x, y, z)`, with `w` the **scalar part** and `(x, y, z)` the **vector part**. Component order varies between codebases (some store the scalar last), so this explanation fixes scalar-first and the **Hamilton** product convention throughout. A **unit quaternion**, one with `w² + x² + y² + z² = 1`, encodes a rotation: - rotation by angle `a` about a unit axis `n` is `q = (cos(a/2), sin(a/2)*n)`; - the identity is `(1, 0, 0, 0)`; - converting back: `a = 2*acos(w)` and `n = (x, y, z) / sin(a/2)`, with the axis undefined when the angle is zero. The angle is halved because the vector is multiplied by `q` on both sides, and each side contributes half of the turn. ## The product, and why order matters The Hamilton product of `q1 = (w1, v1)` and `q2 = (w2, v2)` is `q1 q2 = (w1*w2 - dot(v1, v2), w1*v2 + w2*v1 + cross(v1, v2))` It costs 16 multiplications and 12 additions. The `cross` term flips sign when the factors swap, so `q1 q2` and `q2 q1` differ unless the axes are parallel, just as turning 90 degrees about up and then 90 degrees about right ends somewhere different from the reverse order. ## Rotating a vector To rotate `v`, embed it as the pure quaternion `(0, v)` and form the **sandwich** `q (0, v) q*`; the scalar part of the result is zero and the vector part is the rotated vector. Expanding it gives a cheaper form with `u` the vector part of `q`: 1. `t = 2*cross(u, v)` 2. `v' = v + w*t + cross(u, t)` Composition follows directly from the sandwich: `q2 (q1 v q1*) q2* = (q2 q1) v (q2 q1)*`, because the conjugate of a product is the product of the conjugates in reverse order. So **the product `q2 q1` applies `q1` first**, the same right-to-left reading that column-vector matrices use. ## The conjugate, the inverse, and drift The **conjugate** negates the vector part: `q* = (w, -x, -y, -z)`. Multiplying gives `q q* = (w² + x² + y² + z², 0)`, the squared length as a pure scalar. For a unit quaternion that is `(1, 0)`, the identity, so: - the inverse of a **unit** quaternion is its conjugate, which costs three negations; - the inverse of any non-zero quaternion is `q* / |q|²`; - the conjugate means "the same angle about the reversed axis", which is the rotation undone. Each product rounds, so a long chain of updates drifts off unit length and the sandwich starts to scale vectors as well as rotate them. The fix is cheap: divide by the length every so often, which is far simpler than re-orthonormalising a drifting 3x3 matrix. ## Local versus world composition If an orientation `q` maps an object's local axes to world axes, the side you multiply on picks the frame of a new rotation `r`: - `q' = q r` turns the object about its **own** axes, such as a ship rolling about its own nose; - `q' = r q` turns it about the **world** axes, such as a ship yawing about world up whatever its bank; - the conjugation `r q r*` is something else again: it re-expresses rotation `q` with its axis rotated by `r`. Mixing these up is the classic bug where controls feel right when level and wrong once banked. ## Converting to a matrix A matrix is cheaper when one rotation is applied to many vectors (nine multiplications per vector against about eighteen for the expanded sandwich), and many pipelines consume matrices. For the conventions above, with column vectors: ``` // unit quaternion q = (w, x, y, z); column vectors, v' = M * v M = | 1 - 2(y*y + z*z) 2(x*y - w*z) 2(x*z + w*y) | | 2(x*y + w*z) 1 - 2(x*x + z*z) 2(y*z - w*x) | | 2(x*z - w*y) 2(y*z + w*x) 1 - 2(x*x + y*y) | ``` Checking one case: a rotation by `a` about the z axis is `q = (cos(a/2), 0, 0, sin(a/2))`, and the top-left 2x2 block becomes `cos a, -sin a, sin a, cos a`, the familiar planar rotation. Under the row-vector convention the same rotation is the transpose of this matrix.
- Why does a long chain of quaternion updates need renormalising, and what happens if it is skipped?Every product rounds, so the length slowly drifts from 1. The sandwich `q v q*` then scales vectors by the squared length as well as rotating them, and the conjugate is no longer the inverse. Dividing by the length occasionally restores both; it is a single square root, much cheaper than re-orthonormalising a matrix.
- When is it worth converting a quaternion to a matrix before use?When the same rotation transforms many vectors, such as every vertex of a mesh or every point in a batch, because a matrix applies in nine multiplications per vector. It is also needed when the rotation joins translation and scale in one transform. Quaternions stay the better form for composing, inverting and interpolating orientations.
- Quaternion multiplication is commutative, so the order does not matter.
- The product q1 q2 applies q1 first when using the sandwich q v q*.
- The inverse of a unit quaternion is its negation, -q.
- A unit quaternion stores the full rotation angle, not half of it.
- Quaternions never need renormalising because they cannot drift.