skip to content

Why must a rigid body's inertia tensor be rotated into world space each step, and how does skipping it make a thrown crate tumble wrongly?

level: seniorimportance: should knowfreq 45%

answer

  1. constant in one frame only
  2. torques arrive in world space
  3. rotate, scale, rotate back
  4. why cubes hide the bug

basics

~20 s

The inertia tensor is constant only in body space, but torques arrive in world space, so each step it must become R · I_body · Rᵀ (inverse likewise). Using the body tensor directly is right only while the crate is unrotated.

solid answer

~50 s

The inertia tensor relates angular velocity to angular momentum, `L = I * omega`, and it is constant only in the body's own frame, where it is usually diagonal. Torques and impulses arrive in world space, so with `R` mapping body vectors to world vectors (column vectors), the change of frame gives `I_world = R * I_body * transpose(R)` and `invI_world = R * invI_body * transpose(R)`, recomputed whenever the orientation changes. If code uses the body-space inverse with world-space torques, the response is right while the crate is unrotated and wrong after it turns, so it tumbles in ways its shape does not justify. The same happens with the transform written backwards or a world tensor cached at spawn. Spheres and uniform cubes hide the bug, because their tensor is a multiple of the identity, so I test with a body whose three principal moments differ.

code

pseudocode · 6 lines
pseudocode
R = rotationMatrix(orientation)              // body-to-world, column vectors
invInertiaWorld = R * invInertiaBody * transpose(R)

// angular impulse from impulse J applied at world point p
r = p - centerOfMassWorld
angularVelocity = angularVelocity + invInertiaWorld * cross(r, J)

go deeper

for a junior

Recall that the inertia tensor is rotational mass, a 3x3 matrix that is constant in the body's own frame and depends on shape and mass distribution.

for a middle

Explain body space versus world space and write the transform R · I · Rᵀ for a stated convention, including why the inverse transforms the same way.

for a senior

Diagnose tumbling that does not match a body's shape by checking the frame, the multiplication order, staleness and the centre used, and test with unequal principal moments.

for a principal

Decide where mass properties are computed and validated, in tools or at load, and what test bodies a physics test suite must contain so frame bugs cannot hide.

## What the inertia tensor is The **inertia tensor** `I` is the rotational counterpart of mass: it relates a body's **angular velocity** `omega` to its **angular momentum** `L = I * omega`. Unlike mass it is a 3x3 symmetric matrix, because a body resists spin differently about different axes: a long plank spins easily about its long axis and reluctantly end over end. It is defined about the **centre of mass** as `I = integral of density * (dot(x, x) * E - x * transpose(x)) dV`, where `x` is the position relative to the centre of mass and `E` the identity. For a solid box of mass `m` with full extents `a`, `b`, `c` along its local x, y and z axes: - `I_xx = m * (b² + c²) / 12` - `I_yy = m * (a² + c²) / 12` - `I_zz = m * (a² + b²) / 12` - all off-diagonal terms are zero, because the box's own axes are its **principal axes**. For a uniform cube of side `s` all three are `m * s² / 6`: the tensor is a multiple of the identity. ## Body space and world space Expressed in the body's own frame (**body space**), the tensor is **constant**: the mass does not move relative to the body. That is why it is computed once, usually diagonal. But torques, angular velocities and impulses are expressed in **world space**, and seen from the world the mass distribution turns with the body. This answer uses **column vectors** and a rotation matrix `R` that maps body-space vectors to world space. Then: 1. A world vector becomes a body vector by `transpose(R) * v`, because `R` is orthonormal and its inverse is its transpose. 2. In body space, `L_body = I_body * omega_body`. 3. Mapping both sides to world space: `L_world = R * I_body * transpose(R) * omega_world`. So **`I_world = R * I_body * transpose(R)`**, and because the inverse of that product is the product of the inverses in reverse order, **`invI_world = R * invI_body * transpose(R)`**. Simulations need the inverse far more often, since every update turns a torque or an angular impulse into an angular velocity change. ```pseudocode // once per step, after the orientation has changed R = rotationMatrix(body.orientation) // body-to-world, column vectors body.invInertiaWorld = R * body.invInertiaBody * transpose(R) // applying an impulse J at world point p r = p - body.centerOfMassWorld body.linearVelocity = body.linearVelocity + J * body.invMass body.angularVelocity = body.angularVelocity + body.invInertiaWorld * cross(r, J) ``` An equivalent form skips the matrix products: rotate the vector into body space, scale it by the diagonal body-space inverse, rotate it back. Which is cheaper depends on how many times per step the tensor is used. ## The thrown crate that tumbles wrongly The symptom: a long crate thrown with spin behaves plausibly for the first moment, then tumbles in ways its shape does not justify; it might whirl end over end as easily as about its long axis, or respond to the same torque differently as it turns. The cause is almost always one of these: | Bug | Why it looks the way it does | |---|---| | Using `invI_body` directly with world-space torques | Correct only while the orientation is the identity; wrong as soon as the crate rotates | | `transpose(R) * invI_body * R` | The transform for the opposite direction; again right only near the identity | | World tensor computed once at spawn and never updated | Correct at spawn, stale after the first rotation | | Tensor computed about the mesh pivot, not the centre of mass | Every off-centre response is skewed | | Non-principal tensor treated as diagonal | Off-diagonal coupling silently dropped | A useful diagnostic: **the bug is invisible on spheres and uniform cubes**. Their tensor is a multiple of the identity, so `R * k * E * transpose(R) = k * E` for every orientation. A test scene built only of cubes passes, and the first plank, barrel or vehicle exposes it. Test with a body whose three principal moments differ. ## Building tensors for real bodies - **Compound bodies** sum the parts' tensors after moving each one to the common centre of mass with the **parallel axis theorem**: `I = I_part + m_part * (dot(d, d) * E - d * transpose(d))`, where `d` is the offset from the combined centre of mass to the part's centre. - **Arbitrary meshes** produce a full symmetric tensor with off-diagonal terms. Engines usually diagonalise it once, storing the three principal moments and a fixed rotation from the body frame to the principal frame, so the per-step work stays diagonal. - **Storing inverses** (inverse mass, inverse inertia) makes every velocity update a multiplication and lets an immovable body store zeros. A good interview answer derives `R * I * transpose(R)` from the change of frame, says which convention it assumes, and names the cube test as the reason the bug survives.

  • How do you build the inertia tensor of a body made from several shapes?
    Compute each part's tensor about its own centre of mass, find the combined centre of mass, then shift each part's tensor to that point with the parallel axis theorem, `I_part + m_part * (dot(d, d) * E - d * transpose(d))`, and sum them. The result is usually not diagonal, so diagonalise it once and store the principal moments plus the rotation to the principal frame.
  • Is it cheaper to store the world-space inverse inertia or to recompute it on demand?
    Building it costs at most two 3x3 matrix products per body per step; applying it on demand costs a rotate, a diagonal scale and a rotate back for each vector. A solver that applies it many times per step, once per contact per iteration, benefits from caching the matrix; a body touched once or twice per step does not.

  • The inertia tensor is a constant, so it can be used in any frame.
  • World inertia is transpose(R) times I_body times R for a body-to-world R.
  • Testing with cubes proves the inertia code is correct.
  • The inertia tensor can be computed about the mesh pivot.
  • Inverting I_body is enough; the inverse does not need rotating.