Introduction

Almost every number a graphics card works with describes either where something is or which way something points: the corner of a triangle, the direction of the sun, the way a surface faces. The maths for both is the same, and it’s surprisingly gentle. This post, the first in the Maths for Graphics series, covers:

  • Coordinates: describing a position with a few numbers
  • Vectors: the same numbers thought of as a movement or direction
  • The four things you’ll do with vectors constantly: add, subtract, scale and measure them
  • Unit vectors: arrows exactly 1 long, which graphics uses everywhere for directions

You don’t need any maths beyond adding and multiplying. The series can be read on its own, or alongside Graphics Fundamentals, which uses all of this.

Coordinates: Where Is It?

Picture a sheet of graph paper with two lines drawn through the middle: one going across, called the x axis, and one going up, called the y axis. The point where they cross is the origin. Any point on the paper can be described by two numbers: how far across from the origin (x) and how far up (y). We write them in brackets, x first: (3, 2) means 3 across and 2 up. Negative numbers go the other way: (−1, −2) is 1 to the left and 2 down.

Those two numbers are the point’s coordinates. Drag the orange point and watch them change.

A point and its coordinates

3D works exactly the same way with one more axis, z, for forwards and backwards, so a point needs three numbers: (x, y, z). Every corner of every triangle in a game is stored like this (Vertices, Triangles and Meshes).

Vectors: Which Way, and How Far?

Now look at the dashed arrow from the origin to the point. It describes a movement: “go 3 across and 2 up”. An arrow like this is called a vector, and it’s written with exactly the same numbers: (3, 2).

The difference between a point and a vector is only in how you think about them:

  • A point is a place: “the tree is at (3, 2)”.
  • A vector is a movement or direction: “walk 3 across and 2 up”, from wherever you are. You can slide the arrow anywhere on the paper and it’s still the same vector.

Graphics uses vectors for directions (which way the light shines, which way a surface faces), for movements (how far a character walks this frame), and for the difference between two points (the arrow from the camera to an object).

Doing Things with Vectors

Choose an operation below and drag the tips of the arrows a (orange) and b (purple).

Adding, subtracting, scaling and measuring vectors

Adding

To add two vectors, add their matching numbers: (3, 1) + (1, 2.5) = (4, 3.5). In pictures, it means doing one movement and then the other: put the tail of b on the tip of a, and the sum goes from the start to wherever you end up. Moving an object is exactly this: its new position = its old position + the movement.

Subtracting

b − a is the arrow that goes from a to b. That’s one of the most useful facts in graphics. The direction from a surface to a light is “light position − surface position”. The arrow from the camera to an object is “object − camera”. One edge of a triangle is “one corner − another corner”.

Scaling

Multiplying a vector by a single number (graphics programmers call a single number a scalar) multiplies each of its numbers: 2 × (3, 1) = (6, 2). The arrow keeps its direction but gets longer or shorter. A negative number turns it round.

Length

How long is the arrow (3, 4)? It’s the long side of a right-angled triangle whose other sides are 3 and 4, so Pythagoras’ theorem gives the answer: square the numbers, add them, and take the square root. √(3² + 4²) = √(9 + 16) = √25 = 5. In 3D, just add the third number in too: length = √(x² + y² + z²). The distance between two points is the length of the vector between them.

Unit vectors

A unit vector is one whose length is exactly 1. To turn any vector into one, divide it by its own length; this is called normalising it, and in shader code it’s the normalize function. Choose Length, and unit vector in the demo: however you drag a, its unit vector always ends on the dashed circle of radius 1.

Why bother? Because often only the direction matters, not the length. If every direction has length 1, maths that compares directions becomes much simpler, as the next post shows. All the lighting directions in Basic Lighting, and every normal (Normal Mapping), are unit vectors.

Under the hood

Graphics cards are built around vectors. A shading language has vector types (vec2, vec3, vec4 in GLSL), and adding two vec4s is written as a single +. Much hardware historically worked on four numbers at once for exactly this reason. Modern graphics cards mostly work one number at a time but on many pixels in parallel (How GPUs Run Shaders), and the compiler splits vector maths up for you. Square roots are relatively slow, so the hardware has a fast instruction for “1 ÷ square root”, which is exactly what normalize needs: multiply the vector by 1 ÷ its length.

Where You’ll Use This

  • Every vertex is a point: (x, y, z) (Vertices, Triangles and Meshes).
  • Moving and placing objects is adding vectors; transformation matrices package it up with rotating and scaling.
  • Directions for lighting, the camera and surfaces are unit vectors made by subtracting points and normalising (Basic Lighting, Cameras).
  • Distances (how far is the player from the enemy? which object is nearest?) are vector lengths.
  • Physics, animation and AI in games all use the same vectors for positions, velocities and forces.

Questions You Might Have

Does the order of adding vectors matter?

No: a + b gives the same answer as b + a, just like adding numbers. Walking 3 right then 2 up ends in the same place as 2 up then 3 right. Subtracting is different: a − b points the opposite way to b − a.

Summary

  • Coordinates describe a position: (x, y) in 2D, (x, y, z) in 3D.
  • A vector is the same numbers as a movement or direction; it can sit anywhere.
  • Add and subtract number by number; b − a is the arrow from a to b.
  • Scaling multiplies every number; length comes from Pythagoras: √(x² + y² + z²).
  • A unit vector has length 1; dividing by the length (normalising) makes one.

Further Reading