As engine lead, which rotation representation would you standardise for authored data, runtime orientation and save files, and where should conversions between them happen?
answer
- one representation per job
- authoring angles, runtime quaternions
- matrices derived, not stored
- convert only at named boundaries
- component order and sign convention
basics
~20 sUsually unit quaternions for runtime orientation and saves, Euler angles with a documented order only where people author or clamp angles, and matrices derived on demand; conversions happen once, at named boundaries, never as a per-frame round trip.
solid answer
~50 sI would assign one representation per job rather than crown one. Runtime orientation is a unit quaternion: it composes and interpolates without gimbal lock, renormalises cheaply and does not drift into skew the way a repeatedly multiplied matrix does. People author and constrain rotations as angles, so editor fields and gameplay that clamps yaw and pitch, such as a first-person camera, keep angles as their source of truth, with the Euler order, units and handedness written down. Matrices are derived when the transform chain needs them, not stored as the orientation. Conversions happen at named boundaries (import, tool edit, save and load), never as a per-frame round trip, because Euler extraction is not unique and silently turns an authored 270 degrees into -90. The standard also fixes the quaternion component order and a sign convention for saved data, tested with asymmetric rotations.
code
pseudocode · 15 lines// Convention: right-handed, y up, column vectors, angles in radians.
// Euler order: yaw about y, then pitch about the new x, then roll about the new z.
function quat_from_axis_angle(axis, angle): // axis has unit length
s = sin(angle / 2)
return quat(w = cos(angle / 2), x = axis.x * s, y = axis.y * s, z = axis.z * s)
function quat_from_yaw_pitch_roll(yaw, pitch, roll): // called at the authoring boundary
qy = quat_from_axis_angle((0, 1, 0), yaw)
qx = quat_from_axis_angle((1, 0, 0), pitch)
qz = quat_from_axis_angle((0, 0, 1), roll)
return normalize(qy * qx * qz) // same rotation as R_y * R_x * R_z
function canonical_for_save(q): // q and -q are one rotation
if q.w < 0: return -q
return q // w == 0 needs a tie-break on x, y, zgo deeper
Recall the three representations, Euler angles, unit quaternions and rotation matrices, and the job each one is usually given in a game.
Explain why runtime orientation suits quaternions, why Euler extraction is not unique, and why repeatedly multiplied matrices drift from a pure rotation.
Describe the boundary bugs you have met, such as round-tripped editor angles, swapped component order or the wrong Euler order on import, and the asymmetric tests that catch them.
Weigh designer usability, runtime correctness and data stability, write a per-layer standard with named conversion points, and say when angle-driven gameplay should keep angles.
## The decision behind the question A game touches rotation in several places: designers type angles into an editor, gameplay code turns cameras and turrets, the transform chain multiplies matrices every frame, and save and level files keep orientations for years. Three common representations serve those jobs differently. A codebase that lets each system pick its own ends up converting back and forth, with a bug waiting at every crossing. The lead's job is not to crown one representation but to assign one **per job** and to name the **boundaries** where conversions are allowed. ## What each representation is good at | Representation | Size | Strengths | Weaknesses | |---|---|---|---| | **Euler angles** (yaw, pitch, roll in a stated order) | 3 numbers | readable, easy to clamp per axis, natural for authoring | gimbal lock, order-dependent, several angle sets for one orientation, poor interpolation | | **Unit quaternion** | 4 numbers | composes without gimbal lock, interpolates well, cheap to renormalise | unreadable to people; `q` and `-q` are the same rotation; component order varies between codebases | | **Rotation matrix** (3x3) | 9 numbers | rotates vectors directly, slots into the transform chain | drifts from orthonormal when multiplied repeatedly; element-wise interpolation does not give a rotation | ## A policy by layer A typical standard, which a team adapts rather than copies: - **Authoring.** Tools show angles in **degrees**, with the Euler **order** fixed and printed in the tool. The authored angles stay the source of truth for that field, so a designer's 270 degrees stays 270. - **Angle-driven gameplay.** A first-person camera or a turret that clamps yaw and pitch per axis keeps **two angles** as its state and builds a quaternion from them each frame. Extracting angles back out of a quaternion in order to clamp them is fragile near the limits. - **Runtime orientation.** Everything else stores a **unit quaternion**: composing, interpolating and renormalising are cheap, and nothing degenerates at a particular attitude. - **Transform chain.** **Matrices are derived** from position, rotation and scale when the chain needs them, not stored as the orientation. - **Save and level files.** Quaternions, or the authored angles for authored fields, with a stated component order and a canonical sign. ## The conversions that bite 1. **Euler extraction is not unique.** Converting yaw 270 to a quaternion and back typically returns yaw -90, because extraction returns angles in a fixed range. A pitch past 90 degrees comes back as a different triple: in yaw-pitch-roll order, an authored pitch of 100 returns as yaw 180, pitch 80, roll 180. An editor that round-trips on every refresh rewrites the designer's numbers. 2. **Euler order and handedness.** Yaw-pitch-roll and roll-pitch-yaw applied to the same three numbers are different rotations. An importer that assumes the wrong order produces objects that look right for single-axis rotations and wrong for combined ones. 3. **Component order.** Some code and file formats store `(x, y, z, w)` and others `(w, x, y, z)`. Reading the identity `(0, 0, 0, 1)` in the wrong order gives `w = 0` with the axis along z: a half turn about z. 4. **Units.** Degrees at the authoring boundary, radians inside the math; a missed conversion turns 90 degrees into 90 radians, about 5,157 degrees. ## Enforcing it - **Distinct types** for angles in degrees, angles in radians, quaternions and matrices, so a bare array of four floats never crosses an interface. - **Named conversion functions** at the import, tool and save boundaries, and no conversion in per-frame code without a comment saying why. - **Tests with asymmetric rotations** (unequal angles on all three axes), because a single-axis test passes with the wrong order or a transposed matrix. - **A written page** listing handedness, up axis, Euler order, units, component order and the save sign convention, owned by one person. ## When the default changes - A **2D game** usually needs a single angle and nothing more. - Strictly **angle-driven** gameplay may never touch quaternions outside the transform chain. - A game storing very many rotations may keep a **compressed quaternion** on disk: a storage format, not a new runtime representation. None of these choices is universally right. What separates a lead's answer from a list of facts is the reason for each layer's choice, and the boundaries where a conversion is allowed.
- Why not store orientations as rotation matrices, given that the renderer needs matrices anyway?A 3x3 rotation takes nine numbers against a quaternion's four, and after many multiplications rounding error leaves it slightly non-orthonormal, adding skew and scale that need a costlier re-orthonormalisation to repair. Interpolating matrices element by element does not give a rotation either. Deriving the matrix from the quaternion when the transform chain needs it is cheap and always yields a clean rotation.
- How do you keep saved rotations byte-stable when q and -q describe the same orientation?Pick a canonical sign at the save boundary, for example negate the quaternion when w is negative, with a tie-break on the next nonzero component when w is exactly zero, and renormalise before writing. Diffs of save and level files then show real changes only. Keep the canonical sign a storage rule: interpolation still needs its own dot-product check between consecutive rotations.
- What belongs in the written rotation standard besides the representation for each layer?Handedness and up axis, the Euler order and whether it is applied about fixed or rotating axes, degrees or radians at each boundary, the quaternion component order in memory and in files, the sign convention for saved quaternions, and the list of boundaries where conversion is allowed. Each item left unwritten is a silent mismatch at an importer or a save loader.
- Quaternions are always the right state, even for a camera that clamps yaw and pitch.
- Converting Euler angles to a quaternion and back returns the angles you started with.
- Quaternion component order is universal, so file formats need not state it.
- Storing rotation matrices is safer because the renderer uses matrices anyway.
- Choosing a rotation representation is a performance question, not a tooling and data question.