Step 1 of 5 · Reading · ~1 min
Read
Game Loop
Transforms & Math
Every game engine needs 2D/3D math primitives. Even a 2D game needs:
- vec2 / vec3: positions, velocities, sizes.
- mat3 / mat4: rotation, scale, translation composed into one matrix.
Why a matrix (not a 2x2)?
A pure rotation in 2D fits in a 2x2 matrix:
R(t) = | cos(t) -sin(t) |
| sin(t) cos(t) |
But translation does not fit in a 2x2 — adding a vector is not a matrix multiply. The fix is homogeneous coordinates: extend (x,y) to (x,y,1) and use a 3x3 matrix:
M = | sx*cos(t) -sy*sin(t) tx |
| sx*sin(t) sy*cos(t) ty |
| 0 0 1 |
Now translation, rotation, and scale all live in one matrix. Composing transforms is just matrix multiplication.
Scene graph
Entities have a Transform component with a local matrix. The world transform is computed by walking from the root and multiplying:
child.world = parent.world * child.local
Real engines cache world and only re-compute on dirty changes — entity moved, parent rotated, etc.
Coordinate conventions
- 2D: y-up (math/Bevy/Unity 2D) vs y-down (screen/SDL). Pick one and stick to it.
- 3D: left-handed (Unity, DirectX) vs right-handed (OpenGL, glTF). Differs by which way +z points relative to a screen.
- Units: meters are standard. Unreal famously uses centimeters.
The Practice problem
You'll implement a tiny stack-machine that applies TRANSLATE, ROTATE (degrees), SCALE to a single point. The same algebra scales to a full transform stack walked by a renderer.
Discussion
Ask a question, share an insight, or help someone who’s stuck.
Sign in to post a comment or reply.
Loading…