How do you integrate a rigid body's orientation quaternion from its angular velocity over one physics step, and why must you renormalise it?
answer
- angular velocity as a pure quaternion
- half the rate, multiplied onto q
- the side depends on the space
- additive step leaves the unit sphere
- exact step: axis-angle of omega times dt
basics
~20 sTreat world-space angular velocity as a pure quaternion, add 0.5*dt*(0, ω)*q to the orientation, then normalise. The additive step lengthens the quaternion, and a non-unit quaternion becomes a matrix that scales and skews the body.
solid answer
~40 sWith `q` mapping body space to world space and Hamilton products, `dq/dt = 0.5 * (0, ω) * q` for a world-space angular velocity `ω`, or `0.5 * q * (0, ω_body)` for a body-space one. A first-order step is `q = q + 0.5*dt*(0, ω)*q` followed by `q = normalize(q)`. The added term is perpendicular to `q` in four dimensions, so each step lengthens it by `sqrt(1 + (|ω|*dt/2)²)`; unnormalised, the usual quaternion-to-matrix formula yields a matrix that scales and skews the mesh. Normalising fixes length, not angle: the step turns by `2*atan(|ω|*dt/2)` instead of `|ω|*dt`. For fast spins, such as a wheel at speed, build the rotation of angle `|ω|*dt` about `ω/|ω|` and multiply it onto `q`, which is exact for an angular velocity held constant over the step.
code
pseudocode · 14 lines// q maps body space to world space; Hamilton product; w is a world-space angular velocity
// first-order step: cheap, fine while length(w) * dt is small
dq = quat(0, w.x, w.y, w.z) * q
q = q + dq * (0.5 * dt)
q = q / length(q)
// exact step for w held constant over dt
angle = length(w) * dt
if angle > 1e-6:
axis = w / length(w)
r = quat(cos(angle / 2), axis * sin(angle / 2))
q = r * q
q = q / length(q) // removes rounding drift onlygo deeper
Recall that orientation is advanced by multiplying a quaternion built from angular velocity onto the current one, and that the result must be normalised every step.
Derive half times the pure angular-velocity quaternion times q, explain the half-angle, and show why the additive step lengthens the quaternion.
Spot the symptoms of a missing normalise or a wrong multiplication side, and switch fast spinners to the exact axis-angle step to remove the under-rotation.
Fix one orientation convention for the codebase, quaternion order, side and space, so every system that integrates or reads orientation agrees.
## The quantities involved - **Orientation** `q`: a unit quaternion `(w, x, y, z)`, scalar first. The convention in this answer: `q` rotates vectors from **body space** to **world space**, and every product is a **Hamilton product**. - **Angular velocity** `ω`: a 3-vector whose direction is the spin axis and whose length is the spin rate in radians per second. The rigid-body model supplies it from angular momentum and the inertia tensor; this answer only advances orientation with it. - **Pure quaternion** `(0, ω)`: the vector written as a quaternion with a zero scalar part. ## Deriving the update A body spinning at a constant `ω` for a time `t` turns through the angle `|ω| * t` about the axis `ω / |ω|`. As a quaternion that rotation is `r(t) = (cos(|ω|*t/2), sin(|ω|*t/2) * ω/|ω|)`, and with `ω` in world space the orientation is `q(t) = r(t) * q0`. Differentiating at `t = 0` gives `dq/dt = 0.5 * (0, ω) * q` The **half** comes from the half-angle inside every rotation quaternion. When `ω` is given in body space the rotation is applied on the other side: `dq/dt = 0.5 * q * (0, ω_body)`. | Angular velocity given in | Derivative | Exact rotation applied | |---|---|---| | world space | `0.5 * (0, ω) * q` | `r * q` (on the left) | | body space | `0.5 * q * (0, ω_body)` | `q * r` (on the right) | A first-order step then reads: 1. `dq = (0, ω) * q`; 2. `q = q + 0.5 * dt * dq`; 3. `q = q / length(q)`. ## Why the length drifts and what it breaks Viewed as a 4-vector, `(0, ω) * q` is **perpendicular** to `q`: their dot product is the scalar part of `(0, ω) * q * conjugate(q) = (0, ω) * |q|²`, which is zero. Adding a perpendicular vector always lengthens `q`, here by a factor of `sqrt(1 + (|ω| * dt / 2)²)` per step, and rounding adds a little drift on top. A quaternion that is no longer unit length, fed to the usual unit-quaternion-to-matrix formula, gives a matrix that is no longer orthonormal: the mesh scales and skews, and any code that inverts a rotation by transposing it silently goes wrong. Renormalising after every step costs one square root and removes the problem. ## The exact update for fast spins Normalising fixes the length, not the angle. After normalising, the additive step has rotated the body by `2 * atan(|ω| * dt / 2)` instead of `|ω| * dt`. For slow bodies the difference is negligible. For fast ones it is not: in one scenario, a wheel at 86 rad/s on a 1/60 s step should turn about 1.43 rad per step but turns about 1.24 rad, roughly 13% short. The fix is the **exponential map**: build `r` for the angle `|ω| * dt` about the axis `ω / |ω|` directly and multiply it onto `q`. That is exact when `ω` is constant across the step, keeps unit length up to rounding, and costs a sine, a cosine and one product. Guard the zero-spin case, where the axis is undefined. ## Practical checklist - Pick one convention (body-to-world `q`, Hamilton product, scalar first) and match the side of the multiplication to the space `ω` lives in. - Renormalise after every additive update, not once per frame or once at load. - Switch to the axis-angle (exponential) form when `|ω| * dt` is large: spinning wheels, propellers, thrown tools. - Keep the factor of one half; dropping it makes the body spin twice as fast as its angular velocity says. - Do not integrate Euler angles instead: the mapping from angular velocity to angle rates becomes singular at gimbal lock, which a tumbling body eventually reaches. - If the square root matters, a quaternion already close to unit length can be corrected with `q = q * (3 - dot(q, q)) / 2`, one Newton step toward unit length. - In debug builds, assert that `abs(length(q) - 1)` stays below a small tolerance: a mesh that slowly grows or shears while it spins is the visible symptom of a skipped normalise.
- Where does the factor of one half come from?A rotation quaternion stores `cos(θ/2)` and `sin(θ/2)` times the axis, so it moves through quaternion space at half the rate the body turns. Differentiating `q(t) = (cos(|ω|t/2), sin(|ω|t/2) * ω/|ω|) * q0` at `t = 0` gives `0.5 * (0, ω) * q0`. Drop the half and the body spins twice as fast as its angular velocity says.
- Why not integrate a rotation matrix or Euler angles instead?A rotation matrix can be stepped with `R += dt * skew(ω) * R`, but it drifts off orthonormal and needs a costlier re-orthonormalisation over nine numbers. Euler angles are worse: their rates are not the components of `ω`, and the mapping between them becomes singular at gimbal lock, which a tumbling body eventually reaches. A quaternion needs four numbers and a single normalise.
- Add angular velocity times dt straight onto the quaternion's x, y and z.
- Normalising once at load time is enough; the update keeps unit length.
- The side the angular velocity is multiplied on does not matter.
- After normalising, the additive step is exact at any spin rate.
- Integrating Euler angles is equivalent and avoids normalising.