Skip to content

Matrices in computer graphics

Add one coordinate so that a move becomes a matrix, chain moves, turns and scalings into one product, and project a 3-D object onto a screen.

Helps to know firstMatrix multiplication

Helps to know first
This topic

First, the picture

A game moves thousands of corners every frame, all with matrices. Watch a cube turn, move away and land on a screen.

How games move 3-D objects

Turn, move, project: three 4 × 4 matrices, one product.

A wireframe cube with corners (±1, ±1, ±1), the eye at the origin, a screen 1 unit in front of it.

step
turn
distance
4
drawn size, near to far
–
0.00 / 16.80 s
Describe this picture

Two panels (stacked on a phone). On the left, a 3-D view with the eye at the origin, a screen 1 unit in front of it, and a wireframe cube with corners at (±1,±1,±1)(\pm1,\pm1,\pm1). On the right, two 4×44\times4 grids of cells: the matrix being applied now, and the product of all the steps so far. In three stages: the cube turns about the vertical axis by 53.13° under Arot\mathbf{A}_\text{rot}; it moves 4 units away under a move matrix with 4 in its last column; and it is projected onto the screen, every corner sliding along its ray to the eye, while Apersp\mathbf{A}_\text{persp}, whose last row copies zz into the last entry, joins the product. At the end a flat panel shows the drawing on the screen, a perspective drawing with its nearer edges larger. Readouts give the step, the distance, and the drawn sizes of the nearest and farthest corners. One control after the clip: a slider for the move distance from 2 to 10; the farther the cube, the smaller its drawing.

A move is not linear, in the plane

On A gallery of linear maps, sliding the plane by (1,0)(1,0) failed the test of linearity, because it moves the origin and every matrix keeps the origin in place. But games and drawing programs move objects all the time, and they want to do everything with matrices, because matrices chain by multiplication.

The trick is one extra coordinate. Write the point (x,y)(x,y) as xh=(x,y,1)\mathbf{x}_\text{h}=(x,y,1), its homogeneous coordinates. Now a move by (tx,ty)(t_x,t_y) is a 3×33\times3 matrix:

Amove(tx,ty)=[10tx01ty001],Amove[xy1]=[x+txy+ty1].\begin{aligned} \mathbf{A}_\text{move}(t_x,t_y)&=\begin{bmatrix}1&0&t_x\\0&1&t_y\\0&0&1\end{bmatrix},\\ \mathbf{A}_\text{move}\begin{bmatrix}x\\y\\1\end{bmatrix}&=\begin{bmatrix}x+t_x\\y+t_y\\1\end{bmatrix}. \end{aligned}

One dimension up, the move is a shear of space, and shears are linear. The plane z=1z=1 just slides along inside it. Turns and scalings go in the top left corner, with a 1 in the bottom right: the turn by θ\theta from A gallery of linear maps becomes [cos⁡θ−sin⁡θ0sin⁡θcos⁡θ0001]\begin{bmatrix}\cos\theta&-\sin\theta&0\\\sin\theta&\cos\theta&0\\0&0&1\end{bmatrix}, still written Arot(θ)\mathbf{A}_\text{rot}(\theta).

Chaining steps

Every step is now a matrix, so a chain of steps is one product, applied right to left as in Matrix multiplication. A turn about a point x0\mathbf{x}_0 other than the origin takes three steps: move x0\mathbf{x}_0 to the origin, turn, move back.

Apivot=Amove(x0) Arot(θ)×Amove(−x0).\begin{aligned} \mathbf{A}_\text{pivot}&=\mathbf{A}_\text{move}(\mathbf{x}_0)\,\mathbf{A}_\text{rot}(\theta)\\ &\quad\times\mathbf{A}_\text{move}(-\mathbf{x}_0). \end{aligned}

For a quarter turn about (2,1)(2,1):

Apivot=[0−1310−1001].\mathbf{A}_\text{pivot}=\begin{bmatrix}0&-1&3\\1&0&-1\\0&0&1\end{bmatrix}.

It sends (3,1)(3,1) to (2,2)(2,2), and it leaves (2,1)(2,1) where it is, as a turn about that point must.

Order matters

A quarter turn about (2, 1), against a move by (2, 1) and then a quarter turn.

The same house twice, with the corner (3, 1) marked.

0.00 / 11.80 s
Describe this picture

Two panels with the same small house, its corner (3,1)(3,1) marked with a dot (stacked on a phone). On the left the house turns a quarter turn about the point (2,1)(2,1), which is marked; the corner traces a dashed arc and lands at (2,2)(2,2). Then, on the right, the house is first moved by (2,1)(2,1), so the corner goes to (5,2)(5,2), and then turned a quarter turn about the origin; the corner traces a long arc and lands at (−2,5)(-2,5). A faint copy of the starting house stays in each panel. The two houses end in different places, and the end points are labelled. The clip runs for about 12 s. There is no control.

The order matters, as it did for the turn and the shear: “move, then turn” is not “turn, then move”.

Perspective

In 3-D a point is (x,y,z,1)(x,y,z,1) and every step is 4×44\times4. A camera at the origin looking along the zz axis draws a point at (x/z, y/z)(x/z,\ y/z) on a screen at distance 1: things twice as far look half as big. Division is not a matrix, but it can be left to the end. Use

Apersp=[1000010000100010],\mathbf{A}_\text{persp}=\begin{bmatrix}1&0&0&0\\0&1&0&0\\0&0&1&0\\0&0&1&0\end{bmatrix},

which sends (x,y,z,1)(x,y,z,1) to (x,y,z,z)(x,y,z,z): it copies the depth into the last entry. Then divide by that last entry. Graphics cards work this way: multiply, then divide once per corner.

A vector whose last entry is 0 is a direction, not a place. Moves don’t change it: Amove(5,5) (1,2,0)=(1,2,0)\mathbf{A}_\text{move}(5,5)\,(1,2,0)=(1,2,0). That is right, since moving an object doesn’t change which way it points.

Worked example

1. Turn about a point. With a quarter turn, θ=π/2\theta=\pi/2: Arot(π/2) Amove(−2,−1)=[0−1110−2001]\mathbf{A}_\text{rot}(\pi/2)\,\mathbf{A}_\text{move}(-2,-1)=\begin{bmatrix}0&-1&1\\1&0&-2\\0&0&1\end{bmatrix}; then Amove(2,1)\mathbf{A}_\text{move}(2,1) gives Apivot\mathbf{A}_\text{pivot} above. Check: Apivot(3,1,1)=(0−1+3,  3+0−1,  1)=(2,2,1)\mathbf{A}_\text{pivot}(3,1,1)=(0-1+3,\;3+0-1,\;1)=(2,2,1).

2. The other order. Move by (2,1)(2,1), then turn about the origin: [0−1−1102001]\begin{bmatrix}0&-1&-1\\1&0&2\\0&0&1\end{bmatrix}, which sends (3,1,1)(3,1,1) to (−2,5,1)(-2,5,1).

3. The cube, unturned. Moved 4 units away, its front face has corners (±1,±1,3)(\pm1,\pm1,3) and its back face (±1,±1,5)(\pm1,\pm1,5). On the screen they land at (±0.3333,±0.3333)(\pm0.3333,\pm0.3333) and (±0.2,±0.2)(\pm0.2,\pm0.2): two nested squares, the front one larger in the ratio 5/35/3.

4. The cube, turned. The turn by 53.13° (0.9273 rad, the 3-4-5 angle) followed by the move is one matrix:

[0.600.800100−0.800.640001].\begin{bmatrix}0.6&0&0.8&0\\0&1&0&0\\-0.8&0&0.6&4\\0&0&0&1\end{bmatrix}.

The corner (1,1,1)(1,1,1) goes to (1.4,  1,  3.8)(1.4,\;1,\;3.8) and appears on the screen at (1.4/3.8, 1/3.8)=(0.3684, 0.2632)(1.4/3.8,\ 1/3.8)=(0.3684,\ 0.2632). The corner (−1,1,1)(-1,1,1) goes to (0.2,  1,  5.4)(0.2,\;1,\;5.4) and appears at (0.0370, 0.1852)(0.0370,\ 0.1852).

Try these before you read the answers.

  • Is “turn, then move” the same as “move, then turn”? No.
  • Where does (1,1,3)(1,1,3) land on the screen at distance 1? (1/3, 1/3)(1/3,\ 1/3).
  • Move the cube twice as far away. How big is its drawing? About half as big.

In signal processing · the bilinear transform

IIR design by the bilinear transform, in the signal processing course, maps zz to (z−1)/(z+1)(z-1)/(z+1), up to a constant factor. That is a homogeneous-coordinate trick in one complex dimension: the matrix Abilin=[1−111]\mathbf{A}_\text{bilin}=\begin{bmatrix}1&-1\\1&1\end{bmatrix} acts on (z,1)(z,1), then divide the first entry by the second. For z=jz=j it gives (j−1, j+1)(j-1,\ j+1), and (j−1)/(j+1)=j(j-1)/(j+1)=j. Composing two such maps multiplies their matrices.

Where you’ll meet this

Every graphics card. A card draws a triangle by multiplying its corners by three matrices: the model’s place in the world, the camera’s view, and the perspective. A game multiplies the three once per object per frame, and then each corner costs one 4×44\times4 product.

Robot arms. Each joint of an arm turns and offsets the next link, one 4×44\times4 matrix per joint. The hand’s position is the product of the chain.

Augmented reality. To draw a virtual object on a live camera view, a phone estimates the camera’s 4×44\times4 matrix from the picture, many times a second.

Reference card

QuantityFormulaNotes
Homogeneous pointxh=(x,y,1)\mathbf{x}_\text{h}=(x,y,1)one extra coordinate
MoveAmove(tx,ty)\mathbf{A}_\text{move}(t_x,t_y), tx,tyt_x,t_y in the last columna shear one dimension up
TurnArot(θ)\mathbf{A}_\text{rot}(\theta) in the top-left corner1 in the bottom right
Chainproduct of the steps, right to leftorder matters
Turn about a pointAmove(x0)Arot(θ)Amove(−x0)\mathbf{A}_\text{move}(\mathbf{x}_0)\mathbf{A}_\text{rot}(\theta)\mathbf{A}_\text{move}(-\mathbf{x}_0)move there, turn, move back
Perspective(x,y,z,1)↦(x,y,z,z)(x,y,z,1)\mapsto(x,y,z,z), then dividescreen point (x/z, y/z)(x/z,\ y/z)
Directionlast entry 0moves leave it unchanged

Keep reading with a free account.

By creating an account you accept the terms of use.

Sign in

Already have an account? Use the service you signed up with.

Or keep reading: get new lessons by email

Are you 13 or over?

Accounts and email updates are for people aged 13 or over. We keep only your answer, never your date of birth.

End of the lesson

Where to go next.

Phasorium

Account

Sign inKeep your place on any device

Subjects

Site

Look