3. Rotation Matrices
Package rotation, scaling, and translation into one composable transform
In the previous article we rotated 3D points using sin and cos directly. It works — but every rotation is a separate formula, composing them is fragile, and there's no single object that represents "the current orientation." Matrices fix all of this.
A rotation matrix is a 3x3 (or 4x4) grid of numbers that, when multiplied with a point, rotates it. The beautiful part: to combine two rotations, you just multiply their matrices. The result is another matrix — one that does both rotations at once.
Matrix-Vector Multiplication
Before we get to rotation, let's make sure the core operation is clear. Multiplying a 3x3 matrix by a 3D vector works like this:
Matrix × vector
| a b c | | x | | a*x + b*y + c*z |
| d e f | × | y | = | d*x + e*y + f*z |
| g h i | | z | | g*x + h*y + i*z |
Each row of the matrix "picks out" a weighted combination of x, y, and z. That's all matrix multiplication is — weighted sums.
In code, this is just nine multiplications and six additions:
rx = m[0]*x + m[1]*y + m[2]*z
ry = m[3]*x + m[4]*y + m[5]*z
rz = m[6]*x + m[7]*y + m[8]*z
The Three Rotation Matrices
The sin/cos formulas from the previous article translate directly into matrices:
Rotation around X (pitch):
| 1 0 0 |
Rx = | 0 cos(θ) -sin(θ) |
| 0 sin(θ) cos(θ) |
Rotation around Y (yaw):
| cos(θ) 0 sin(θ) |
Ry = | 0 1 0 |
| -sin(θ) 0 cos(θ) |
Rotation around Z (roll):
| cos(θ) -sin(θ) 0 |
Rz = | sin(θ) cos(θ) 0 |
| 0 0 1 |
Notice the pattern: the row and column corresponding to the rotation axis are just the identity (1 on the diagonal, 0 elsewhere). The other four entries hold the sin/cos values.
Composing Rotations
Here's where matrices earn their keep. To rotate first by yaw, then by pitch:
M = Rx × Ry
Now M is a single matrix that does both rotations. Apply it to any number of points with one matrix-vector multiply each. Need to add roll? Just multiply again:
M = Rz × Rx × Ry
Matrix multiplication is not commutative
Rx × Ry ≠ Ry × Rx — the order you multiply matters because the order
you rotate matters. This isn't a quirk of matrices; it reflects the physical reality
that 3D rotations don't commute.
Matrix × Matrix
To multiply two 3x3 matrices, each entry in the result is a dot product of a row from the left matrix with a column from the right:
C[i][j] = A[i][0]*B[0][j] + A[i][1]*B[1][j] + A[i][2]*B[2][j]
This gives you 9 entries, each requiring 3 multiplications — 27 multiplications total. It sounds like a lot but modern CPUs do this in nanoseconds.
The 4x4 Matrix and Translation
A 3x3 matrix can rotate and scale, but it can't translate (move) a point — because multiplying the zero vector always gives zero. To include translation, we upgrade to 4x4 matrices and homogeneous coordinates.
Add a fourth coordinate w = 1 to every point: (x, y, z, 1). Now we can sneak translation into the last column:
| 1 0 0 tx | | x | | x + tx |
| 0 1 0 ty | × | y | = | y + ty |
| 0 0 1 tz | | z | | z + tz |
| 0 0 0 1 | | 1 | | 1 |
The rotation goes in the top-left 3x3 block; the translation goes in the right column. This is a model matrix (or transform matrix), and it's how every 3D engine represents an object's position and orientation as a single matrix.
Camera Transform
Instead of moving the camera, we move the entire world in the opposite direction. If the camera is at position (cx, cy, cz) and has some orientation, the view matrix is:
- Translate by
(-cx, -cy, -cz)— shift everything so the camera is at the origin - Rotate by the inverse of the camera's orientation — align the world with the screen axes
V = R_inverse × T(-cx, -cy, -cz)
For a pure rotation matrix, the inverse is just the transpose (flip rows and columns). This is one of the elegant properties of rotation matrices: they're orthogonal, meaning R^T × R = I.
Euler Angles and Their Problems
Specifying rotation as three angles — pitch, yaw, roll — is called using Euler angles. It's intuitive but has a nasty trap: gimbal lock.
Gimbal lock
When one rotation axis aligns with another (e.g., pitch reaches ±90°), you lose a degree of freedom. Two of the three axes end up doing the same thing, and certain orientations become unreachable without discontinuous jumps.
This isn't a bug in your code — it's a mathematical property of decomposing rotation into three sequential axis rotations. The fix requires a different representation entirely: quaternions.
Scaling
A uniform scale matrix is trivial:
| s 0 0 |
| 0 s 0 |
| 0 0 s |
Non-uniform scaling (different values per axis) works the same way. You can compose it with rotation: M = R × S. Order matters here too — scaling then rotating gives a different result than rotating then scaling (the latter shears the object if scales aren't uniform).
Putting It Together: The MVP Pipeline
3D engines typically chain three matrices:
- Model (M) — positions and orients an object in the world
- View (V) — transforms from world space to camera space
- Projection (P) — applies the perspective divide
The combined matrix P × V × M transforms a vertex directly from object space to screen space. This is called the MVP matrix, and every vertex shader in every game computes it.
Key Takeaways
What to remember
- A rotation matrix packages sin/cos rotation into a 3x3 grid of numbers.
- Composing rotations = multiplying matrices. One matrix, any number of rotations.
- 4x4 matrices add translation, giving you a full rigid-body transform.
- Rotation matrices are orthogonal: the inverse is the transpose.
- Euler angles suffer from gimbal lock — a fundamental limitation that quaternions solve.