Intuition
Two boxes overlap only if they overlap on every axis. If there is a gap on any axis, they do not collide.
Variables
- Minimum and maximum corners of box A.
- 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.
- Use AABB as a cheap early-out before expensive polygon/pixel-perfect collision.
- AABBs can be loose for rotated objects, causing false positives. Use OBB/SAT or narrow-phase tests when needed.
- Be explicit about whether bounds are inclusive and what coordinate system/units you use.
Implementation
JavaScript & C++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)); // truestruct 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);
}