Introduction to Vectors

Welcome to a new series on vector math! Vectors are essential to game programming for both 2D and 3D applications. Throughout this series, we’ll define what vectors are, how mathematical operators are applied to them, and examples of their geometrical representation. I’ll also include practical game-based scenarios. Let’s start with the data that defines a vector.

Data

Vectors represent a data model that consists of a direction and a magnitude (length). Imagine a player driving a vehicle in a game where we need to know its forward position in local space and the magnitude, or speed, in which we move it forward. I’m sure you’ve probably thought of another example already, like the physics of a platform that either increases or decreases gravity with a particular strength.

Geometric Interpretation

We can represent a vector geometrically using a direct line segment where the direction of the line describes the vector’s direction, and the length of the line describes the vector’s magnitude.

To draw a vector, we mark the tail and draw a line in the direction it will go, and end it by drawing an arrow at the head of the vector. This gives us both the direction and magnitude.

Notice that there’s no position data. Vectors do not describe their position in space. That means we can place them where ever we’d like to make it more convenient to reason about and compare them.

For example, we could place two vectors anywhere on a 2D coordinate space like this:

Generally, the most convenient place to position vectors when trying to solve problems is with the tail originating from the origin of the coordinate system.

Because vectors don’t carry position information, the data for a vector specifies the location of the “head” of the vector. Here’s a simple structure representation.

struct Vector3
{
    // Data
    float x;
    float y;
    float z;

    // Methods
    ...
}

Unit Vectors

You will often encounter what are called unit vectors. A unit vector is a vector that has a magnitude of 1, which will represent 1 unit of a working coordinate system.

Unit vectors can be used as a basis for deriving another vector with a modified magnitude. Let’s use a typical player movement example.

Say we want to move the player to the right on button press. We already know the direction is to the right, but we don’t have the speed. Using a right-directional unit vector, we can apply a magnitude to have both the direction and speed of player movement.

Unit vectors are so commonly used that most (if not all) game libraries will provide quick access to unit vectors for any direction. We’ll use Unreal Engine as an example. Here you can see the constants defined in the FVector class.

Notation

We need to have an easy way to identify when we are referring to vectors when writing them, and there are multiple ways to do so.

The most common way to notate a vector is by drawing an arrow above it like so:

You may also see it written as a half arrow or a straight line above or below the vector. I prefer the full arrow notation, so that’s what I’ll be using throughout this series.

To denote a unit vector, add a “^” above it, which we call a hat or cap. The following unit vector can be read as “a hat” or “a cap”.

To denote the magnitude of a vector, surround it in double vertical bars, like so. You may also encounter the use of a single vertical bar on each side, but this notation is uncommon.

Summary

We’re just scratching the surface of vector math with this introduction to the series. In future posts, we’ll dive into adding, subtracting, and multiplying vectors. I’ll also provide more common game-based examples to demonstrate how we practically apply these operations in game programming.

Want to connect? Follow me on Twitter and LinkedIn!

Take care.
Stay awesome.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s