Introduction#
A mesh is built once, in its own coordinates, but a game shows it in many places: a hundred trees, each in a different spot, turned a different way and a slightly different size. Instead of changing every vertex by hand, graphics programs describe each change as a transformation matrix: a small grid of numbers that moves, rotates and scales every vertex in one go. This post covers:
- What a matrix does to a point, and how to read a matrix just by looking at it
- The three basic transformations: scale, rotate and translate (move)
- Why 3D graphics uses 4×4 matrices, even though space only has three dimensions
- Why the order you combine transformations in changes the result
To keep the pictures simple, the demos are in 2D, but everything here works the same way in 3D with one more row and column.
A Matrix Moves Points#
(Coordinates and Vectors gently introduces this section.) A point in 2D is a pair of numbers, (x, y): how far it is along the x axis (horizontal) and the y axis (vertical). The axes cross at the origin, the point (0, 0).
A matrix is a grid of numbers. A 2D transformation matrix has two rows and two columns (2×2), and “multiplying” a point by it follows a fixed recipe that gives a new point. Writing a·x to mean “a times x”, the recipe is:
Each new number is a mix of the old ones: the new x is a lots of the old x plus b lots of the old y, and the new y is c lots of x plus d lots of y. But there’s a much more useful way to think about it.
The columns are where the axes go#
Put the point (1, 0), one step along the x axis, through the rule above, and you get (a, c): the first column of the matrix. Put in (0, 1), one step up the y axis, and you get (b, d): the second column. So:
- The first column says where the x axis ends up
- The second column says where the y axis ends up
Every other point comes along for the ride, because (2, 3) just means “2 steps along x plus 3 steps along y”, and those steps follow the axes. This is the most useful fact about transformation matrices: you can read what a matrix does from its columns.
Try It#
The demo below builds a matrix from sliders: move (translate), rotate and scale. The house is drawn with the matrix applied, the red and green arrows show where its x and y axes have gone, and the matrix is shown underneath in the same colours. Then switch to Drag the axes and move the arrow tips to make any matrix, including ones the sliders can’t, like a slant (a shear) or a mirror image.
A 2D transformation and its matrix
Some things to try:
- Rotate only. As the angle grows, the red arrow’s tip travels round a circle and the green one stays at right angles to it. Both keep their length, so the house turns without changing shape. (In trigonometry terms, the columns are (cos θ, sin θ) and (−sin θ, cos θ) for an angle θ; Angles, Sine and Cosine explains them.)
- Scale x to 2. The first column doubles and the house stretches sideways. Scale to a negative number and the house flips over: a mirror image.
- In Drag the axes mode, drag the green tip sideways without moving the red one. That’s a shear. Drag one tip on top of the other and the house is squashed flat: no 2D picture is left to undo, so this matrix can’t be reversed.
Moving Needs an Extra Dimension#
The matrix in the demo is 3×3, not 2×2, because a 2×2 matrix can’t move anything: whatever its numbers, (0, 0) always comes out as (0, 0). Rotation and scaling keep the origin fixed, but moving (translation) doesn’t.
The trick is to give every point an extra coordinate, always set to 1: (x, y) becomes (x, y, 1). These are called homogeneous coordinates. Now a 3×3 matrix can add a fixed amount to each coordinate, using its third column:
The third column is exactly how far the origin moves. 3D graphics does the same thing one size up: points are (x, y, z, 1), and transformations are 4×4 matrices, with the x, y and z axes in the first three columns and the translation in the fourth.
A nice side effect: set the extra coordinate to 0 instead of 1 and translation has no effect. That’s just right for a direction, like “the way this surface faces”, which should turn with the object but not move with it. The extra coordinate returns, doing something much cleverer, for perspective in part 5.
Combining Transformations#
To scale, rotate and move a mesh, you could apply three matrices to every vertex in turn. But multiplying two matrices gives one matrix that does both jobs, so games multiply all of an object’s transformations together once and apply that single matrix to every vertex. A whole chain costs the same per vertex as one.
But the order matters. In the demo below, both houses get the same two steps: rotate by 45°, and move 3 to the right. It plays once by itself; press Play to watch again.
Same two steps, different order
Rotation and scaling always happen around the origin. If the object has already been moved away from the origin, rotating swings it around in a big arc instead of turning it on the spot. That’s why the usual order for placing an object is scale, then rotate, then translate.
When matrices are written as a multiplication, the one nearest the point happens first. So “scale, then rotate, then translate” is written:
Here p is a point, p′ (say “p dash”) is where it ends up, and the formula reads right to left: S happens first. If that feels backwards, you’re not alone, and some game engines (the software games are built on, such as Unity or Unreal) write it the other way round. That’s the topic of part 8, Matrix Conventions.
Multiplying a 4×4 matrix by a point (x, y, z, 1) is 16 multiplications and 12 additions. Graphics cards are built for exactly this: each of their thousands of small processors does a multiplication and an addition in a single step, all at once, so transforming millions of vertices every frame is routine. Combining matrices is usually done once per object on the main processor (the CPU), and the result is handed to the vertex shader as a uniform: a value that’s the same for every vertex in a draw call.
Questions You Might Have#
Do I have to build these matrices by hand?
No. Maths libraries (glMatrix for WebGL, GLM, DirectXMath, or the one in your game engine) have functions like translate, rotate and perspective that build them for you. Knowing what’s inside is what helps when something ends up in the wrong place.
Summary#
- A matrix transforms points; each new coordinate is a mix of the old ones.
- The columns of a matrix are where the axes end up, so you can read a matrix by looking at it.
- Homogeneous coordinates add a 1 to every point, so one matrix can also translate. That’s why 3D uses 4×4 matrices.
- Matrices combine by multiplying, and order matters: usually scale, then rotate, then translate.
Next: coordinate spaces and the transform chain, where a series of these matrices carries every vertex from its model all the way to the screen.
Further Reading#
- 3Blue1Brown, Essence of Linear Algebra: beautifully animated videos on what matrices really do. Chapters 3 and 4 match this post.
- LearnOpenGL, Transformations: the same ideas in 3D, with the code to use them in shaders.
- WebGL2 Fundamentals, 2D Matrices: building up 2D transformations step by step.
- Wikipedia, Transformation matrix: a reference for rotation, shear, reflection and more.