TExcelTAD
Home › Math for Games › AABB

AABB

An axis-aligned bounding box (AABB) is a box whose edges are parallel to the world axes. It is stored as two corners, min and max (or as a center and half-size). AABBs are the workhorse of collision detection: tile-based 2D games use them for everything, and 3D engines wrap every object in one for a cheap first test before any expensive one (the broad phase).

Overlap test

Two AABBs overlap when they overlap on every axis. On one axis, two intervals [aMin, aMax] and [bMin, bMax] overlap when aMin <= bMax and bMin <= aMax. If any axis has a gap, the boxes are apart.

cpp · try it yourself
#include <iostream>

struct Vec2 { double x, y; };
struct AABB { Vec2 min, max; };

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

bool contains(const AABB& box, Vec2 p) {
    return p.x >= box.min.x && p.x <= box.max.x && p.y >= box.min.y && p.y <= box.max.y;
}

int main() {
    std::cout << std::boolalpha;
    AABB player{{2, 0}, {3, 2}};
    AABB crate{{2.5, 1}, {4, 3}};
    AABB coin{{5, 0}, {5.5, 0.5}};
    AABB wall{{3, -5}, {4, 5}};
    std::cout << "player vs crate: " << overlaps(player, crate) << "\n";
    std::cout << "player vs coin:  " << overlaps(player, coin) << "\n";
    std::cout << "player vs wall:  " << overlaps(player, wall) << "  (touching edges count)\n";
    std::cout << "point (2.5, 1) in player: " << contains(player, {2.5, 1}) << "\n";
}
player vs crate: true
player vs coin:  false
player vs wall:  true  (touching edges count)
point (2.5, 1) in player: true

Whether touching edges count as a collision (<= vs <) is a design decision. For tile maps, < is usually right, so a player standing exactly on a tile edge doesn't collide with the tile next to it.

Resolving a collision: minimum translation

Knowing that boxes overlap is half the job; a platformer also has to push the player out. Compute the overlap depth on each axis and push out along the axis with the smaller depth. That gives the shortest way out, the minimum translation vector, and it is why a player landing on a platform is pushed up rather than sideways.

cpp · try it yourself
#include <iostream>
#include <iomanip>
#include <algorithm>

struct Vec2 { double x, y; };
struct AABB { Vec2 min, max; };

// Returns how far to move `a` to separate it from `b` (zero if they don't overlap).
Vec2 separation(const AABB& a, const AABB& b) {
    double ox = std::min(a.max.x, b.max.x) - std::max(a.min.x, b.min.x);
    double oy = std::min(a.max.y, b.max.y) - std::max(a.min.y, b.min.y);
    if (ox <= 0 || oy <= 0) return {0, 0};
    double acx = (a.min.x + a.max.x) / 2, bcx = (b.min.x + b.max.x) / 2;
    double acy = (a.min.y + a.max.y) / 2, bcy = (b.min.y + b.max.y) / 2;
    if (ox < oy) return {acx < bcx ? -ox : ox, 0};
    return {0, acy < bcy ? -oy : oy};
}

int main() {
    std::cout << std::fixed << std::setprecision(2);
    AABB ground{{0, 0}, {10, 1}};
    AABB landing{{4, 0.8}, {5, 2.8}};      // fell 0.2 into the ground
    AABB sideHit{{9.7, 1.5}, {10.7, 3.5}};
    AABB wall{{10, 1}, {11, 5}};

    Vec2 a = separation(landing, ground), b = separation(sideHit, wall);
    std::cout << "landing pushed by (" << a.x << ", " << a.y << ")\n";
    std::cout << "side hit pushed by (" << b.x << ", " << b.y << ")\n";
}
landing pushed by (0.00, 0.20)
side hit pushed by (-0.70, 0.00)

Note: Resolving against several boxes one at a time can snag on the seams between neighboring tiles. Platformers usually move and resolve one axis at a time (move x, resolve x; move y, resolve y), which avoids catching on flat floors made of many tiles.

Bounding a rotated or moving object

To put a box around a rotated object, transform its eight local corners and take the min and max of each axis. There is a faster trick for a box under a matrix (taking absolute values of the rotation part), but the corners version shows the idea. The result is larger than the object when it's rotated: an AABB is a conservative bound, and the precise test comes afterwards.

cpp · try it yourself
#include <iostream>
#include <iomanip>
#include <cmath>
#include <algorithm>
#include <numbers>
#include <limits>

struct Vec2 { double x, y; };

int main() {
    std::cout << std::fixed << std::setprecision(3);
    const double a = 30 * std::numbers::pi / 180, c = std::cos(a), s = std::sin(a);
    Vec2 center{10, 5}, half{2, 1};                     // a 4 x 2 crate rotated 30 degrees
    Vec2 mn{std::numeric_limits<double>::max(), std::numeric_limits<double>::max()};
    Vec2 mx{-mn.x, -mn.y};
    for (double sx : {-1.0, 1.0}) for (double sy : {-1.0, 1.0}) {
        double lx = sx * half.x, ly = sy * half.y;
        Vec2 w{center.x + lx * c - ly * s, center.y + lx * s + ly * c};
        mn = {std::min(mn.x, w.x), std::min(mn.y, w.y)};
        mx = {std::max(mx.x, w.x), std::max(mx.y, w.y)};
    }
    std::cout << "AABB min (" << mn.x << ", " << mn.y << ") max (" << mx.x << ", " << mx.y << ")\n";
    std::cout << "box area 8.000, AABB area " << (mx.x - mn.x) * (mx.y - mn.y) << "\n";

    // The fast version: half-extents of the AABB = |R| * half
    double hx = std::fabs(c) * half.x + std::fabs(s) * half.y;
    double hy = std::fabs(s) * half.x + std::fabs(c) * half.y;
    std::cout << "fast half-extents (" << hx << ", " << hy << ")\n";
}
AABB min (7.768, 3.134) max (12.232, 6.866)
box area 8.000, AABB area 16.660
fast half-extents (2.232, 1.866)

AABBs in the engines

Unity exposes Bounds (Renderer.bounds, Collider.bounds, Bounds.Intersects), Unreal FBox (GetComponentsBoundingBox, FBox::Intersect), Godot AABB / Rect2 (intersects, has_point), Three.js Box3 (setFromObject, intersectsBox).

Test yourself

When do two AABBs overlap?

  1. When their centers are within 1 unit
  2. When their intervals overlap on every axis
  3. When they overlap on any axis
  4. When their areas are equal

Boxes overlap by 0.2 on y and 0.9 on x. Along which axis do you push them apart?

  1. x, by 0.9
  2. y, by 0.2
  3. Both axes
  4. Neither

What happens to an object's AABB when you rotate the object 45°?

  1. It rotates too
  2. It usually grows to enclose the rotated shape
  3. It shrinks
  4. It becomes a sphere