Vectors and math
A 2D vector is a direction and a magnitude. Master addition, scalar multiplication, normalisation, and the dot product — the four operations that underpin game physics.
- Define a 2D vector in terms of direction and magnitude
- Perform vector addition, scalar multiplication, and normalisation
- Compute and interpret the dot product of two 2D vectors
- Use pygame.math.Vector2 for these operations in Python
Almost every physics calculation in a game — movement, collision response, aiming, steering — reduces to a handful of vector operations. Learning them once pays dividends across every project.
What is a vector?
A 2D vector is a pair (x, y) that encodes a direction and a
magnitude simultaneously. When a bullet travels at 300 pixels per second
toward the upper-right, its velocity is a vector: (212, -212) (roughly).
The direction is upper-right; the magnitude (sqrt(212² + 212²) ≈ 300) is the
speed.
Vectors differ from points: a point is a location; a vector is a displacement. Adding a velocity vector to a position point produces a new position point — that is movement.
The four core operations
Addition
A + B = (A.x + B.x, A.y + B.y)
Adding two vectors gives the combined displacement. If a spaceship has
velocity (3, 0) and you apply a thruster impulse of (1, -2), the new
velocity is (4, -2).
Scalar multiplication
s * A = (s*A.x, s*A.y)
Multiplying a vector by a scalar stretches or compresses it without changing
direction. Multiplying by a negative scalar reverses direction. This is how
you scale speed: if your normalised direction vector is (0.6, -0.8) and your
speed is 5, the velocity vector is 5 * (0.6, -0.8) = (3, -4).
Normalisation
A normalised (unit) vector has a magnitude of exactly 1. Divide each component by the vector's length:
length = sqrt(x² + y²)
normalised = (x / length, y / length)The result encodes direction only — magnitude is stripped. This is useful whenever you want to move "towards something" at a fixed speed: compute the direction vector, normalise it, then multiply by the desired speed.
Dot product
A · B = A.x*B.x + A.y*B.y
The dot product is a single number that measures alignment. When both vectors are normalised, the result lies between -1 and 1:
| Result | Meaning |
|---|---|
| 1 | Vectors point in the same direction |
| 0 | Vectors are perpendicular |
| -1 | Vectors point in opposite directions |
Games use this constantly: Is the enemy in front of or behind the player? Is a surface facing the light? Is the player moving toward or away from the goal? All answered with a dot product.
pygame.math.Vector2
Python's pygame library ships pygame.math.Vector2 — a two-component
vector class with operator overloading and built-in methods:
import pygame
a = pygame.math.Vector2(3, 4)
b = pygame.math.Vector2(1, -2)
print(a + b) # (4, 2)
print(a * 2) # (6, 8)
print(a.length()) # 5.0
print(a.normalize()) # (0.6, 0.8)
print(a.dot(b)) # 3*1 + 4*(-2) = -5Vector2 supports +=, -=, and *= in-place, which keeps per-frame
updates concise. It also exposes .angle_to(), .rotate(), and
.reflect() for more advanced needs.
Vector2.normalize() will raise a ValueError if called on the zero
vector (0, 0) — a vector with no direction cannot be normalised. Guard
with if v.length() > 0: v = v.normalize() wherever the input might be
zero.
Where to go next
Next: velocity and acceleration — applying Euler integration with delta time so objects move consistently regardless of frame rate.
Lab: Polish and persistence
Extend the tile platformer with jump and coin sound effects, looping background music, save-on-quit / load-on-start, and a settings scene for volume control.
Velocity and acceleration
Implement Euler integration so objects accelerate smoothly and move at the same speed on any machine — frame-rate independence in practice.