TExcelTAD
Home › Math for Games › Vectors

Vectors

A vector is a list of numbers that together describe one thing: a position, a direction, a velocity, a color. In games almost all of them have two components (x, y) for 2D games or three (x, y, z) for 3D games.

The same type is used for two different ideas, and keeping them apart in your head prevents a lot of bugs:

Meaning Example "Moves" when you translate the object?
Point (a location) the player stands at (4, 0, -2) yes
Direction / offset (an arrow) the player walks along (1, 0, 0) at 3 m/s no

An arrow has no fixed starting place: the offset (1, 0, 0) means "one meter to the right" wherever you are. Later, homogeneous coordinates will give points and directions different behavior under matrices. For now they share one struct.

A Vec2 and a Vec3

Here is a small vector type with the operators you need most. Adding vectors adds component by component; multiplying by a number (a scalar) scales every component.

cpp · try it yourself
#include <iostream>
#include <iomanip>

struct Vec3 {
    double x = 0, y = 0, z = 0;
};

Vec3 operator+(Vec3 a, Vec3 b) { return {a.x + b.x, a.y + b.y, a.z + b.z}; }
Vec3 operator-(Vec3 a, Vec3 b) { return {a.x - b.x, a.y - b.y, a.z - b.z}; }
Vec3 operator*(Vec3 v, double s) { return {v.x * s, v.y * s, v.z * s}; }
Vec3 operator*(double s, Vec3 v) { return v * s; }
Vec3 operator-(Vec3 v) { return {-v.x, -v.y, -v.z}; }

std::ostream& operator<<(std::ostream& os, Vec3 v) {
    return os << "(" << v.x << ", " << v.y << ", " << v.z << ")";
}

int main() {
    std::cout << std::fixed << std::setprecision(2);
    Vec3 a{1, 2, 3}, b{4, 0, -1};
    std::cout << "a + b  = " << a + b << "\n";
    std::cout << "b - a  = " << b - a << "\n";
    std::cout << "a * 2  = " << a * 2 << "\n";
    std::cout << "0.5 * b = " << 0.5 * b << "\n";
    std::cout << "-a     = " << -a << "\n";
}
a + b  = (5.00, 2.00, 2.00)
b - a  = (3.00, -2.00, -4.00)
a * 2  = (2.00, 4.00, 6.00)
0.5 * b = (2.00, 0.00, -0.50)
-a     = (-1.00, -2.00, -3.00)

Subtraction gives "from A to B"

The most useful habit in game math: to - from is the arrow from from to to. Want the direction from the enemy to the player? player - enemy. Want to know how far the bullet must still travel? target - bullet.

cpp · try it yourself
#include <iostream>
#include <iomanip>

struct Vec2 { double x, y; };
Vec2 operator-(Vec2 a, Vec2 b) { return {a.x - b.x, a.y - b.y}; }
Vec2 operator+(Vec2 a, Vec2 b) { return {a.x + b.x, a.y + b.y}; }

int main() {
    std::cout << std::fixed << std::setprecision(1);
    Vec2 enemy{2, 1}, player{7, 4};
    Vec2 toPlayer = player - enemy;       // arrow from enemy to player
    std::cout << "enemy -> player: (" << toPlayer.x << ", " << toPlayer.y << ")\n";

    // Following the arrow from the enemy lands exactly on the player.
    Vec2 arrive = enemy + toPlayer;
    std::cout << "enemy + arrow:   (" << arrive.x << ", " << arrive.y << ")\n";

    // The opposite arrow points back.
    Vec2 back = enemy - player;
    std::cout << "player -> enemy: (" << back.x << ", " << back.y << ")\n";
}
enemy -> player: (5.0, 3.0)
enemy + arrow:   (7.0, 4.0)
player -> enemy: (-5.0, -3.0)

Moving things: velocity and acceleration

A velocity is a direction vector measured in units per second. Multiply it by the frame time dt and add it to the position. An acceleration (gravity, thrust) changes the velocity the same way. This tiny loop is a projectile:

cpp · try it yourself
#include <iostream>
#include <iomanip>

struct Vec2 { double x, y; };
Vec2 operator+(Vec2 a, Vec2 b) { return {a.x + b.x, a.y + b.y}; }
Vec2 operator*(Vec2 v, double s) { return {v.x * s, v.y * s}; }

int main() {
    std::cout << std::fixed << std::setprecision(3);
    Vec2 pos{0, 0};
    Vec2 vel{4, 6};                 // m/s: forward and up
    const Vec2 gravity{0, -9.8};    // m/s^2
    const double dt = 0.2;

    for (int step = 1; pos.y >= 0; step++) {
        vel = vel + gravity * dt;   // velocity changes first...
        pos = pos + vel * dt;       // ...then position (semi-implicit Euler)
        std::cout << "t=" << step * dt << "  pos=(" << pos.x << ", " << pos.y << ")\n";
    }
}
t=0.200  pos=(0.800, 0.808)
t=0.400  pos=(1.600, 1.224)
t=0.600  pos=(2.400, 1.248)
t=0.800  pos=(3.200, 0.880)
t=1.000  pos=(4.000, 0.120)
t=1.200  pos=(4.800, -1.032)

The ball rises, slows, and comes back down below y = 0, where the loop stops. The game-physics course explains why the order of the two update lines matters; for now notice that both are just "vector times number, plus vector".

Operations at a glance

Operation Code Meaning
a + b {a.x+b.x, a.y+b.y} follow a, then b
b - a {b.x-a.x, b.y-a.y} arrow from a to b
v * s {v.x*s, v.y*s} same direction, s times as long (negative s flips it)
-v {-v.x, -v.y} the opposite direction
point + direction a new point
point − point a direction
point + point usually a bug (except when averaging)

Tip: Use double while learning so rounding never distracts you. Engines use float (32-bit) for speed and memory, which has about 7 significant digits. That is the reason big open worlds shift the origin when the player travels far.

Test yourself

An enemy is at e and the player at p. Which vector points from the enemy toward the player?

  1. e - p
  2. p - e
  3. p + e
  4. e * p

What is (2, -1, 4) * 3?

  1. (6, -3, 12)
  2. (5, 2, 7)
  3. (6, 3, 12)
  4. (2, -1, 12)

Which combination is usually a bug?

  1. point + direction
  2. point − point
  3. point + point
  4. direction * scalar