Skip to content

Vectors — Points, Directions, and Movement

Every object in a game world has a position, a direction, and often a velocity. All three are vectors. This chapter introduces the vector as both a geometric arrow and an algebraic list of numbers, then builds up the operations you will use constantly: addition, scalar multiplication, magnitude, and normalization. By the end, you will know how to move a character across a map, compute how far away an enemy is, and separate "which direction" from "how fast" — all with straightforward vector arithmetic.

Three Steps East, Four Steps North

Imagine a top-down adventure game. Your character stands at the center of a grid. You tap a destination: three tiles east and four tiles north. The game needs to figure out two things. First, which direction should the character walk? Second, how far is the destination?

You already have all the information you need. The instruction "three east, four north" encodes both a direction (northeast) and a distance (not three, not four, but something we will calculate shortly). That instruction — with its magnitude and direction bundled together — is a vector.

We can write it as a pair of numbers:

The first number is the east–west component (positive means east), and the second is the north–south component (positive means north). Together, they tell the character exactly where to go relative to its current position. A 3D game adds a third component for up–down:

Now the character moves three units east, four units north, and two units up — maybe climbing a staircase, launching into the air, or rising through water. The idea is the same; we just track one more axis.

What Is a Vector?

A vector is a mathematical object that has both a magnitude (length) and a direction. Geometrically, think of it as an arrow: the arrow's length is the magnitude, and the way it points is the direction.[^1]

The crucial insight — one that trips up many newcomers — is that the arrow's starting position does not define the vector. Two arrows are the same vector as long as they have the same length and point in the same direction, no matter where they are drawn.[^2] A vector describes a displacement ("go here from wherever you are"), not a fixed point in space.

Algebraically, a vector in two dimensions is an ordered pair of real numbers, and in three dimensions an ordered triple:[^1]

Each number is a component. The components tell you how far the vector stretches along each axis. You will also see angle-bracket notation in textbooks — it means exactly the same thing.

INFO

Points vs. vectors. A point marks a location. A vector describes a displacement — "go 3 right and 4 up." In code they often share the same data structure, but conceptually they are different. A point is a place on the map. A vector is a step you could take from any place on the map. As the Georgia Tech Interactive Linear Algebra textbook puts it, "an arrow is determined by its length and its direction, not by its location."[^2]

In game engines, positions are points, velocities are vectors, and forces are vectors. When you add a velocity vector to a position point, you get a new position point — the object has moved.

Standard Basis Vectors

The coordinate axes have their own special unit vectors, used as building blocks. In 2D:

In 3D, a third joins them:

Any vector can be expressed as a combination of these basis vectors. For example:

This says: "go 3 units in the x-direction and 4 in the y-direction." You will encounter this notation in physics textbooks and some game-engine APIs. These basis vectors will take on a much deeper significance when we reach matrices and linear transformations — every column of a transformation matrix tells you where one of these basis vectors lands.

Vector Addition — Combining Movements

Suppose your character is at position and you apply the movement vector . Where does the character end up? Add the components:

The character lands at . That is vector addition: add corresponding components, one axis at a time.[^3] The rule extends to 3D without any surprises — just add the three pairs of components.

Geometrically, vector addition follows the tip-to-tail rule. Place the tail of the second arrow at the tip of the first. The sum is a new arrow that goes from the tail of the first arrow to the tip of the second:[^2]

(0,0) ---[v=(3,4)]---> (3,4)
                          |
                     [w=(2,1)]
                          |
                          v
(0,0) ................> (5,5)   ← v + w (the shortcut)

The solid arrows show two successive movements. Vector carries you from the origin to . Vector carries you from there to . The dotted arrow — the sum — goes directly from start to finish: the shortcut is geometrically identical to the two-step journey.

There is also the parallelogram rule: place both vectors at the same tail and complete the parallelogram. The diagonal of the parallelogram is the sum. Both methods give the same answer; use whichever picture helps you think.

The Game Loop Pattern

In a game, vector addition runs every frame. The core update loop looks like this:

ts
// Every frame:
position = position + velocity * deltaTime;

If a spaceship is at position with velocity meters per second, and the frame lasts of a second:

Sixty times per second, that tiny addition nudges every object in the world forward. It is the heartbeat of any simulation, and it is nothing more than vector addition (combined with scalar multiplication, which we cover next).

TIP

Vector addition is commutative: . The order does not matter. Whether you go east then north or north then east, you end up in the same place. It is also associative: , which means you can combine forces or movements in any grouping without affecting the result.

Scalar Multiplication — Speeding Up and Slowing Down

A scalar is an ordinary number — no direction, just a magnitude. Think temperature, distance, price. When you multiply a vector by a scalar, you multiply every component by that number:[^4]

Geometrically, scalar multiplication rescales the arrow's length without rotating it (unless the scalar is negative, in which case the direction flips).[^4]

  • Multiply by : the arrow doubles. The character moves twice as far in the same direction.
  • Multiply by : the arrow halves. Half speed.
  • Multiply by : the arrow reverses. The character runs the opposite way.

This is exactly what happens in the game-loop snippet above. velocity * deltaTime is scalar multiplication — it scales the velocity vector by the fraction of a second that has elapsed, producing a tiny displacement appropriate for one frame.

Game Examples

Speed boost. A power-up that doubles the player's speed? Scale the velocity:

ts
if (hasSpeedBoost) {
  velocity = velocity * 2;
}

Direction stays the same. Only the magnitude changes. The beauty of scalar multiplication is that it separates how fast from which way.

Friction. A simple friction model multiplies velocity by a scalar slightly less than 1 each frame, draining speed regardless of direction:

ts
velocity = velocity * 0.95; // lose 5% of speed per frame

No angle checks needed. The direction is untouched; only the length shrinks.

Slow motion. When a game enters bullet-time mode, every velocity vector in the scene gets multiplied by the same slow-motion factor. One scalar applied uniformly to every vector in the simulation — clean and consistent.

Magnitude — How Far?

The magnitude (also called the length or norm) of a vector tells you how long the arrow is. It is written . For a 2D vector , the formula comes straight from the Pythagorean theorem — the components are the legs of a right triangle, and the vector is the hypotenuse:[^1]

Remember the character who walked three east and four north? The magnitude of is:

The straight-line distance is 5 tiles. A classic 3-4-5 right triangle.

In three dimensions, the formula extends by adding one more squared term — applying the Pythagorean theorem twice, once in the horizontal plane and once in the vertical:[^5]

For : .

Practical Example: Range Checks

A staple of game AI is checking whether an enemy is within attack range. If the player is at position and the enemy is at position , the distance is the magnitude of the difference vector:

If the player is at and the enemy is at :

If the attack range is 6, the enemy is within range . If the range is 4, it is not .

TIP

Computing a square root is relatively expensive. Many game engines offer a sqrMagnitude or distanceSquared function. Since you only need to compare distances, and since implies for positive values, compare squared magnitudes instead:

ts
const distSq = (e.x - p.x) ** 2 + (e.y - p.y) ** 2;
if (distSq <= range * range) {
  // Enemy in range — attack!
}

This avoids the square root entirely and is a standard game-development optimization.

Unit Vectors and Normalization — Which Direction?

Sometimes you care about direction but not distance. "The camera should face toward the player" does not specify how far away the player is — it only specifies a direction. For this you need a unit vector: a vector with magnitude exactly 1.[^5]

Any nonzero vector can be converted to a unit vector by dividing it by its own magnitude. This operation is called normalization:[^5]

The hat notation (, "v-hat") signals a unit vector. Let us normalize :

The result points in the same direction as but has length exactly 1. Verify: . ✓

Separating Direction from Speed

One of the most common patterns in game code is storing direction and speed separately:

ts
const direction: Vec2 = normalize(target - position); // unit vector
const velocity: Vec2  = direction * speed;             // scaled by speed

With this pattern, changing speed does not touch the direction, and pointing the character a different way does not affect its speed. The two concerns are cleanly decoupled.

Without normalization, subtle bugs creep in. Imagine a click-to-move game. The raw difference vector to a faraway target is much longer than the vector to a nearby one. A naive velocity = target - position would make the character sprint toward distant clicks and crawl toward close ones — not what you want. Normalizing first ensures constant speed.

Here is where normalization appears in practice:

  • Camera and character facing — the forward direction must always be unit-length
  • Surface normals — which way a polygon faces (essential for lighting; Chapter 3)
  • Projectile direction — fire in this direction at this speed
  • AI steering — move toward the target at a fixed speed per frame

WARNING

Never normalize the zero vector . Dividing by zero yields NaN (Not a Number), which silently corrupts every calculation it touches. Before normalizing, always check that the magnitude is greater than zero (or greater than a small epsilon like 0.0001 to avoid floating-point near-zero issues).

Putting It Together — Launching a Projectile

Let us use every concept from this chapter in one worked example. You want to fire a projectile from the player's position toward a target.

Given:

  • Player position:
  • Target position:
  • Projectile speed: units per second

Step 1 — find the displacement vector.

Step 2 — measure the distance.

Step 3 — normalize to get the launch direction.

Step 4 — scale by speed to get the velocity vector.

Step 5 — update position each frame.

Four operations — subtraction, magnitude, normalization, scalar multiplication — and a projectile flies across a 3D world at exactly the right speed and angle.

Adding Gravity

Real projectiles curve downward. Each frame, add a downward acceleration vector to the velocity:

ts
velocity = velocity + (0, -9.8, 0) * deltaTime;
position = position + velocity * deltaTime;

The x and z components carry the projectile forward; the y component pulls it down. The launch calculation above is unchanged — gravity is simply an additional vector added on top.

Vectors in Code

Most game engines and math libraries represent vectors as simple structs or classes. Here is a minimal 2D implementation to make the operations concrete:

ts
class Vec2 {
  constructor(public x: number, public y: number) {}

  add(other: Vec2): Vec2 {
    return new Vec2(this.x + other.x, this.y + other.y);
  }

  scale(k: number): Vec2 {
    return new Vec2(this.x * k, this.y * k);
  }

  magnitude(): number {
    return Math.sqrt(this.x ** 2 + this.y ** 2);
  }

  normalize(): Vec2 {
    const mag = this.magnitude();
    if (mag === 0) throw new Error("Cannot normalize the zero vector");
    return new Vec2(this.x / mag, this.y / mag);
  }

  subtract(other: Vec2): Vec2 {
    return new Vec2(this.x - other.x, this.y - other.y);
  }
}

Every operation from this chapter maps to a few lines of arithmetic. Engines like Unity expose the same API on their Vector3 type — v.magnitude, v.normalized, v + w. Now you know what each of those properties and operators actually computes.

Chapter Recap

ConceptFormulaUse in games
Vector or Position, velocity, direction
AdditionUpdate position, combine forces
Scalar multiplyScale speed, apply friction
Magnitude (2D)Distance, range checks
Magnitude (3D)Distance in 3D space
NormalizeExtract pure direction

Key ideas to keep in mind:

  • A vector describes a displacement, not a fixed location. Two arrows are the same vector if they share length and direction, regardless of where they are drawn.
  • Vector addition chains movements. Scalar multiplication rescales without rotating.
  • Magnitude tells you "how far." Normalization extracts "which way."
  • Normalize before multiplying by speed — that is the idiomatic pattern for movement in games.

The next chapter introduces the dot product — an operation that takes two vectors and returns a single number measuring how much they agree in direction. It is the key to lighting calculations, line-of-sight checks, and a surprising number of other game systems. It builds directly on everything here.

References

[^1]: "Euclidean vector." Wikipedia. https://en.wikipedia.org/wiki/Euclidean_vector

[^2]: Margalit, D. and Rabinoff, J. "Vectors." Interactive Linear Algebra, Georgia Institute of Technology. https://textbooks.math.gatech.edu/ila/vectors.html

[^3]: Burzynski, J. "Addition, Subtraction, and Scalar Multiplication of Vectors." Mathematics for Game Developers, LibreTexts. https://math.libretexts.org/Bookshelves/Applied_Mathematics/Mathematics_for_Game_Developers_(Burzynski)/02:_Vectors_In_Two_Dimensions/2.02:_Addition_Subtraction_and_Scalar_Multiplication_of_Vectors

[^4]: "Scalar multiplication." Wikipedia. https://en.wikipedia.org/wiki/Scalar_multiplication

[^5]: "Vectors in Three Dimensions." OpenStax Calculus Volume 3, Section 2.2. https://openstax.org/books/calculus-volume-3/pages/2-2-vectors-in-three-dimensions