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

Intermediate

Collision & Geometry

Distance, intersection, and spatial tests that power hit detection, triggers, and gameplay logic.

Formula

AABB Intersection

A fast overlap test for axis-aligned rectangles/boxes.

Intuition

Two boxes overlap only if they overlap on every axis. If there is a gap on any axis, they do not collide.

Equation
overlap=(Amin.x≤Bmax.x∧Amax.x≥Bmin.x)∧…\text{overlap} = (A_{min}.x \le B_{max}.x \land A_{max}.x \ge B_{min}.x) \land \ldots

Variables

Amin,AmaxA_{min}, A_{max}
Minimum and maximum corners of box A.
Bmin,BmaxB_{min}, B_{max}
Minimum and maximum corners of box B.

Explanation

AABB stands for Axis-Aligned Bounding Box. Because edges are aligned to axes, overlap tests are cheap and branch-friendly. They are commonly used as broad-phase collision checks before more precise tests.

Where it shows up in games

Platformer collision, trigger volumes, UI hit testing, broad-phase physics, tile/entity overlap checks.

Implementation

JavaScript & C++
JavaScriptjavascript
function aabbIntersects(a, b) {
  return (
    a.min.x <= b.max.x && a.max.x >= b.min.x &&
    a.min.y <= b.max.y && a.max.y >= b.min.y
  );
}

const player = { min: { x: 0, y: 0 }, max: { x: 1, y: 2 } };
const coin = { min: { x: 0.8, y: 0.5 }, max: { x: 1.2, y: 1 } };
console.log(aabbIntersects(player, coin)); // true
C++cpp
struct Vec2 { float x, y; };
struct AABB { Vec2 min; Vec2 max; };

bool intersects(const AABB& a, const AABB& b) {
    return (a.min.x <= b.max.x && a.max.x >= b.min.x) &&
           (a.min.y <= b.max.y && a.max.y >= b.min.y);
}

Formula

Circle/Sphere Overlap

Tests overlap by comparing center distance to combined radii.

Intuition

Two circles overlap if the distance between their centers is less than or equal to the sum of their radii.

Equation
∥c⃗2−c⃗1∥2≤(r1+r2)2\lVert \vec{c}_2 - \vec{c}_1 \rVert^2 \le (r_1 + r_2)^2

Variables

c⃗1,c⃗2\vec{c}_1, \vec{c}_2
Centers of the circles/spheres.
r1,r2r_1, r_2
Radii.

Explanation

This is one of the cheapest and most useful collision tests. The squared form avoids a square root. It's ideal for projectiles, pickups, proximity checks, and simple character interactions.

Where it shows up in games

Projectile hits, pickups, aggro radius, explosion radius, simple physics proxies.

Implementation

JavaScript & C++
JavaScriptjavascript
function circlesOverlap(a, b) {
  const dx = b.x - a.x;
  const dy = b.y - a.y;
  const r = a.r + b.r;
  return dx * dx + dy * dy <= r * r;
}

console.log(circlesOverlap({ x: 0, y: 0, r: 1 }, { x: 1.5, y: 0, r: 1 })); // true
C++cpp
struct Circle { float x, y, r; };

bool overlaps(const Circle& a, const Circle& b) {
    float dx = b.x - a.x;
    float dy = b.y - a.y;
    float r = a.r + b.r;
    return dx * dx + dy * dy <= r * r;
}