Vector math hero image

Vector math basics for games programming

If you’re making your foray into games programming, vectors are an important concept that you’ll have to understand and work with. If you’ve been reading build-your-own-game tutorials and getting confused by all the vector math that’s been going on, this article is probably a good one to read.

What are vectors?

Remember these diagrams you had to draw in secondary school?

Vector addition
Vector addition
Vector subtraction
Vector subtraction

Well, these are vectors. They represent quantities in space. This means that they are like numbers, but they also have a direction. This makes them effective mathematical concepts for dealing with objects that are moving in 2-dimensional (2D) or 3-dimensional (3D) spaces.

The Cartesian coordinate system

In a programming context, vectors almost always exist inside a Cartesian coordinate system. This is a system of measuring space that uses 2 or 3 number lines to quantify 2D or 3D spaces, respectively.

A number line looks like this:

1D Vectors
Numeric operations, as movement on 1-dimensional space.

It is infinite in both directions, and every number is equally spaced from another. On a number line, you can identify a spot on the line by specifying a number.

While this may not be very useful, if you put 2 number lines together, you get a Cartesian coordinate system that can be used to represent 2D space. In such a system, we call the horizontal line the x-axis, and the vertical line the y-axis.

2D Coordinates
The horizontal line is the x-axis, while the vertical one is called the y-axis.

Adding a third number line (z-axis), as shown below, will give us a 3D coordinate system that can quantify 3D space.

Z-Axis
This is called the z-axis. No surprises there.

Article continues after the advertisement:


Vectors in coordinate systems

In a coordinate system, vectors are represented by a set of numbers. Vectors in a 2D coordinate system contain 2 numbers, while vectors in a 3D coordinate system contain 3 numbers. No surprises here. Each number in a vector represents the length of the vector along a given axis.

We are using 2D vectors here because they are easier to draw.

When dealing with vectors in Cartesian coordinates, you don’t need vector diagrams to add or subtract vectors. Simply sum up the numbers on each axis individually:

[ -2.5 1 ] + [ 2 1.5 ] = [ -2.5 + 2 1 + 1.5 ] = [ -0.5 2.5 ] [ -2.5 1 ] [ 2 1.5 ] = [ -2.5 – 2 1 – 1.5 ] = [ -4.5 -0.5 ]

Vectors can also be multiplied or divided; but unlike addition and subtraction, they can only be multiplied by a single number, or a scalar.

[ 4.5 3.5 ] × 2 = [ 4.5 × 2 3.5 × 2 ] = [ 9 7 ] [ 7 5 ] ÷ 2 = [ 7 ÷ 2 5 ÷ 2 ] = [ 3.5 2.5 ]

Multiplying or dividing a vector changes its length without changing its direction. The vector gets longer or shorter, but still points where it used to point.

Vector lengths

To get the length of a vector, we can apply the Pythagoras Theorem. Notice that in a 2D vector, x and y form a right-angled triangle.

Vector length using Pythagoras Theorem
Read more about the Pythagoras Theorem.

This means that you can get the vector’s length using the following formula:

length = x2 + y2

Pythagoras Theorem works for 3D vectors too:

length = x2 + y2 + z2

But there are handy shortcuts that you can use in modern game engines to find vector lengths, so you won’t have to apply the formula yourself. In Unity3D, you can simply use Vector2.magnitude or Vector3.magnitude properties. These will apply the Pythagoras theorem formula on your vectors to get you the length.

Vector2 v = new Vector(3,5);
float dist = v.magnitude;
Vector3 v = new Vector(3,5,2);
float dist = v.magnitude; 

Article continues after the advertisement:


Leave a Reply

Your email address will not be published. Required fields are marked *

Note: You can use Markdown to format your comments.

For security, use of Google's reCAPTCHA service is required which is subject to the Google Privacy Policy and Terms of Use.

I agree to these terms.

This site uses Akismet to reduce spam. Learn how your comment data is processed.