Skip to content
GameDev Formulas
Math & physics for real games
Back to topics

All levels

Vectors & Movement

The foundation of positions, directions, velocity, steering, and spatial reasoning in games.

Formula

Vector Magnitude (Length)

Measures how long a vector is — used for distance, speed, and normalization.

Intuition

A vector like (x, y) forms a right triangle from the origin. Its length is the hypotenuse.

Equation
∥v⃗∥=vx2+vy2+vz2\lVert \vec{v} \rVert = \sqrt{v_x^2 + v_y^2 + v_z^2}

Variables

v⃗\vec{v}
The vector whose length you want.
vx,vy,vzv_x, v_y, v_z
Components of the vector along each axis.

Explanation

Magnitude tells you the size of a vector regardless of direction. In games, you use it to compute distance between points, determine speed from a velocity vector, or check whether an object is within range. For 2D games, drop the z term.

Where it shows up in games

Distance checks, speed calculation, range triggers, camera zoom constraints, and normalizing vectors before applying direction.

Implementation

JavaScript & C++
JavaScriptjavascript
function magnitude(v) {
  return Math.sqrt(v.x * v.x + v.y * v.y + v.z * v.z);
}

function distance(a, b) {
  const dx = b.x - a.x;
  const dy = b.y - a.y;
  const dz = b.z - a.z;
  return Math.sqrt(dx * dx + dy * dy + dz * dz);
}

// Example: check if enemy is in attack range
const enemy = { x: 10, y: 0, z: 3 };
const player = { x: 7, y: 0, z: 1 };
const attackRange = 5;

if (distance(player, enemy) <= attackRange) {
  console.log("Enemy can attack");
}
C++cpp
#include <cmath>
#include <iostream>

struct Vec3 {
    float x, y, z;
};

float magnitude(const Vec3& v) {
    return std::sqrt(v.x * v.x + v.y * v.y + v.z * v.z);
}

float distance(const Vec3& a, const Vec3& b) {
    float dx = b.x - a.x;
    float dy = b.y - a.y;
    float dz = b.z - a.z;
    return std::sqrt(dx * dx + dy * dy + dz * dz);
}

int main() {
    Vec3 player{7.0f, 0.0f, 1.0f};
    Vec3 enemy{10.0f, 0.0f, 3.0f};
    float attackRange = 5.0f;

    if (distance(player, enemy) <= attackRange) {
        std::cout << "Enemy can attack\n";
    }
}

Formula

Vector Normalization

Converts a vector to length 1 while keeping its direction.

Intuition

If you divide a vector by its own length, you scale it down to a unit direction arrow.

Equation
v^=v⃗∥v⃗∥,∥v⃗∥≠0\hat{v} = \frac{\vec{v}}{\lVert \vec{v} \rVert}, \quad \lVert \vec{v} \rVert \neq 0

Variables

v⃗\vec{v}
Original vector.
∥v⃗∥\lVert \vec{v} \rVert
Magnitude of the vector.
v^\hat{v}
Unit vector (length 1) in the same direction.

Explanation

Normalization separates direction from magnitude. This is essential when you want consistent movement speed, aiming directions, surface normals, or lighting calculations. After normalizing, you can multiply by any speed or force magnitude.

Where it shows up in games

Character movement direction, projectile aiming, camera forward vectors, surface normals, and AI steering.

Implementation

JavaScript & C++
JavaScriptjavascript
function normalize(v) {
  const len = Math.hypot(v.x, v.y, v.z);
  if (len === 0) return { x: 0, y: 0, z: 0 }; // Safe fallback
  return { x: v.x / len, y: v.y / len, z: v.z / len };
}

// Move player toward target at constant speed
const player = { x: 0, y: 0, z: 0 };
const target = { x: 3, y: 0, z: 4 };
const speed = 5; // units per second
const dt = 0.016;

const dir = normalize({
  x: target.x - player.x,
  y: target.y - player.y,
  z: target.z - player.z,
});

player.x += dir.x * speed * dt;
player.y += dir.y * speed * dt;
player.z += dir.z * speed * dt;
C++cpp
#include <cmath>

struct Vec3 {
    float x, y, z;
};

Vec3 normalize(const Vec3& v) {
    float len = std::sqrt(v.x * v.x + v.y * v.y + v.z * v.z);
    if (len == 0.0f) return {0.0f, 0.0f, 0.0f};
    return {v.x / len, v.y / len, v.z / len};
}

// Usage: constant-speed movement
Vec3 moveToward(Vec3 player, Vec3 target, float speed, float dt) {
    Vec3 dir{target.x - player.x, target.y - player.y, target.z - player.z};
    dir = normalize(dir);
    player.x += dir.x * speed * dt;
    player.y += dir.y * speed * dt;
    player.z += dir.z * speed * dt;
    return player;
}

Formula

Dot Product

Measures how much two vectors point in the same direction.

Intuition

The dot product is positive when vectors face roughly the same way, zero when perpendicular, and negative when opposite.

Equation
a⃗⋅b⃗=axbx+ayby+azbz=∥a⃗∥∥b⃗∥cos⁡(θ)\vec{a} \cdot \vec{b} = a_x b_x + a_y b_y + a_z b_z = \lVert\vec{a}\rVert \lVert\vec{b}\rVert \cos(\theta)

Variables

a⃗,b⃗\vec{a}, \vec{b}
Two vectors being compared.
θ\theta
Angle between the vectors.

Explanation

The dot product is a compact way to test alignment. It appears everywhere: field-of-view checks, lighting intensity, projection, backface detection, and deciding whether an enemy is in front of or behind the player.

Where it shows up in games

FOV cones, light shading (Lambert), checking if target is ahead, projecting movement onto slopes, and angle thresholds.

Implementation

JavaScript & C++
JavaScriptjavascript
function dot(a, b) {
  return a.x * b.x + a.y * b.y + a.z * b.z;
}

function isInFront(playerForward, toTarget) {
  // Both vectors should be normalized for a clean angle test
  return dot(playerForward, toTarget) > 0;
}

function isInFOV(playerForward, toTargetDir, fovDegrees) {
  const cosHalfFov = Math.cos((fovDegrees / 2) * (Math.PI / 180));
  return dot(playerForward, toTargetDir) >= cosHalfFov;
}

console.log(isInFOV({ x: 0, y: 0, z: 1 }, { x: 0, y: 0, z: 1 }, 90)); // true
C++cpp
#include <cmath>

struct Vec3 { float x, y, z; };

float dot(const Vec3& a, const Vec3& b) {
    return a.x * b.x + a.y * b.y + a.z * b.z;
}

bool isInFOV(const Vec3& forward, const Vec3& targetDir, float fovDegrees) {
    float cosHalfFov = std::cos((fovDegrees / 2.0f) * 3.14159265f / 180.0f);
    return dot(forward, targetDir) >= cosHalfFov;
}