A moon mesh beside a planet at the world origin orbits it instead of spinning in place; how does transform multiplication order cause this?
answer
- matrix products do not commute
- which factor acts first
- rotation pivots about the current origin
- column vectors read right to left
basics
~20 sThe rotation was applied after the translation, so it pivots the already-moved moon about the world origin. With column vectors the model matrix must be T * R * S: scale and spin about its own centre, then move.
solid answer
~50 sWith column vectors (`p' = M * p`) the rightmost matrix acts first, so `M = T * R * S` scales in local space, rotates about the moon's own centre, then moves it into place. If the code builds `R * T`, the translation happens first and the rotation then swings the displaced moon around the world origin, where the planet sits: an orbit. Matrix multiplication does not commute, so the written order is the meaning. With row vectors (`p' = p * M`) the same intent is written `S * R * T`. To get both behaviours on purpose, compose `R_orbit * T(d) * R_spin * S` with the planet at the origin, and prefix the planet's own world matrix if it moves — the same rule a parent-child chain uses: `world = parentWorld * local`.
go deeper
Recall that matrix products do not commute and that a rotation turns points about the current origin, so translating before rotating produces an orbit.
Explain right-to-left reading under column vectors, why T * R * S spins in place, how row vectors write it as S * R * T, and where scale must go.
Diagnose from the symptom: spawn at the origin, draw local axes, check which side increments are multiplied on, and suspect two systems assuming different conventions.
Decide where the team's single TRS build function lives and whether components or matrices are the source of truth, so order mistakes cannot be written at call sites.
## Reading a product of transforms Every rotation matrix rotates about the **origin of whatever space the point is in at that moment**. That single fact explains the bug. Under the **column-vector convention** (`p' = M * p`), the product `A * B * C` applied to `p` is `A * (B * (C * p))`: `C` acts first, then `B`, then `A`. So the written order is the reverse of the order of application. The standard **model matrix** (local space to world space) is therefore: ``` M = T * R * S (column vectors: scale first, then rotate, then translate) ``` - `S` scales the mesh about its local origin, along its own axes. - `R` rotates it about its local origin, its centre, so it spins in place. - `T` moves the finished, spun object to its position in the world. ## The moon bug traced with numbers Take the planet at the world origin and the moon placed at `d = (10, 0, 0)`. Let `R` be a 90-degree turn about the vertical `y` axis, written as the matrix that maps `(x, y, z)` to `(z, y, -x)`. Follow two points of the moon: its centre, local `(0, 0, 0)`, and a point on its surface, local `(1, 0, 0)`. **Correct, `M = T * R`:** 1. Centre: `R` leaves `(0, 0, 0)` alone; `T` moves it to `(10, 0, 0)`. 2. Surface point: `R` turns `(1, 0, 0)` into `(0, 0, -1)`; `T` moves it to `(10, 0, -1)`. The centre stays put and the surface point has turned around it: a spin. **Buggy, `M = R * T`:** 1. Centre: `T` gives `(10, 0, 0)`; `R` turns that into `(0, 0, -10)`. 2. Surface point: `T` gives `(11, 0, 0)`; `R` turns that into `(0, 0, -11)`. The centre itself has moved a quarter of the way around the origin. As the angle grows each frame, the moon circles the planet: an orbit. ## Where scale belongs Scale goes on the right, nearest the vertex, so it acts in the mesh's own axes: - `T * R * S` stretches a non-uniformly scaled moon along its own axes and then spins the stretched shape rigidly. - `T * S * R` rotates first and then stretches along the **world** axes, so a non-uniform scale makes the shape appear to shear and wobble as it spins. - `S * T` (scale after translation) also scales the offset, so a moon scaled by 2 would sit at distance 20 instead of 10, even with a uniform scale. ## Row vectors mirror the written order | convention | how a point is transformed | spin-in-place model matrix | where translation lives | |---|---|---|---| | column vectors | `p' = M * p` | `T * R * S` | last column | | row vectors | `p' = p * M` | `S * R * T` | bottom row | Both describe the same operation — scale, then rotate, then translate. A row-vector matrix is the transpose of its column-vector twin, and `(T * R * S)` transposed is `S' * R' * T'`, which is why the order flips. A bug report saying "the order is reversed" is often two pieces of code assuming different conventions. ## Composing orbit and spin deliberately A real moon does both. With column vectors and the planet at the origin: ``` M_moon = R_orbit(a) * T(d) * R_spin(b) * S ``` Read right to left: scale the mesh, spin it about its own centre by angle `b`, push it out to distance `d`, then swing that whole arrangement about the planet by angle `a`. If the planet itself moves, prefix the planet's world matrix: `M_moon_world = M_planet * M_moon`. That is the general rule for a parent-child chain: `world = parentWorld * local` with column vectors, `local * parentWorld` with row vectors. When to recompute those products and in what order is the scene's bookkeeping; the multiplication rule is the math. ## A debugging checklist 1. Write down which convention the math library uses: is the vector on the right or the left? 2. Spawn the object at the world origin. If it spins correctly there but orbits when placed elsewhere, the rotation is fine and the order is wrong. 3. Draw the object's local axes as debug lines. A spin keeps the axes' origin fixed; an orbit moves it. 4. Check whether the code multiplies a new rotation onto the left or the right of an existing matrix: with column vectors, left means about the world origin, right means about the object's own origin.
- How do you rotate an object about an arbitrary pivot point p instead of its own origin?Move the pivot to the origin, rotate, and move it back. With column vectors that is T(p) * R * T(-p), applied on the left of the object's world matrix for a world-space pivot. The middle rotation then turns everything about p, because p sits at the origin while R acts.
- With column vectors, what is the difference between multiplying a new rotation on the left or on the right of an existing world matrix?On the left, R * M, the rotation acts last, in world space, about the world origin. On the right, M * R, it acts first, in the object's local space, about its own origin. If M contains non-uniform scale, the right-hand version rotates before the scale, which distorts the mesh; that is one reason engines keep rotation separate and rebuild the matrix.
- Why do many engines store position, rotation and scale separately and rebuild the matrix each frame?The components are easy to edit and animate, the build order T * R * S lives in one function so callers cannot get it wrong, and repeated matrix multiplication drift does not accumulate. The matrix is derived data, rebuilt when a component changes.
Spinning on the spot and then stepping onto your marked place leaves you on the mark, facing a new way. Stepping onto the mark first and then having the whole floor turned under you, like a turntable centred somewhere else, carries you around in a circle.
- Order does not matter as long as T, R and S are all in the product
- With column vectors, T * R * S translates first, then rotates, then scales
- An orbit-instead-of-spin bug means the rotation matrix is wrong
- Scale can sit anywhere in the product because it only multiplies
- A rotation matrix always turns about the object's own centre