AABB Collision Detection

axis aligned bounding box collision

Axis aligned bounding box collision means two rectangles collide only when their x ranges and y ranges both overlap. A gap on either axis rules out a hit, so the check needs no rotation maths and works only on unrotated boxes.

MDN’s 2D collision detection guide calls this one of the simpler forms of collision detection. It is one technique inside our wider 2d collision detection tutorial, and it is a good early exercise in 2d game programming for beginners. The steps below build the check from the edges up, so you can see why each comparison is there.

What you need before you start

  • Rectangles that never rotate. MDN defines this case as two rectangles that are axis aligned, meaning no rotation. Their sides stay parallel to the x and y axes. A box aligned with the coordinate axes is the simplest bounding volume, as a write-up on AABB versus OBB puts it.
  • A hitbox instead of the image. A sprite is the picture you draw. The hitbox is a simple generic shape that covers the entity, so it does not need to follow the artwork pixel for pixel. A bounding box is that covering rectangle, and it is axis-aligned when its sides line up with the axes.
  • Position and size per object. Store each rectangle as its top-left corner plus a width and a height, which is how a rectangle-intersection discussion defines the bounds. Your sprite’s width and height are the natural starting size for the hitbox. MDN’s demo sets the width and height of its boxes to 20px.

Building the overlap check

The check answers one question: is there a gap between the rectangles on any of the four sides? MDN’s example works by ensuring there is no gap on any side. The steps below split that into x and y tests.

Step 1: Give every object a box

Add x, y, width and height to anything that can collide, such as the player, an obstacle or a pickup. Keep these numbers separate from the sprite’s draw position if the hitbox should be smaller or larger than the artwork. Only the box takes part in the collision test.

Loose brushstrokes and textured paint surround the red, yellow and dark-outlined rectangles.

Step 2: Name the four edges

Each box has a left edge at x, a right edge at x plus width, a top edge at y and a bottom edge at y plus height. Writing these as local variables, or as getters on your object, makes the comparisons that follow easy to read and to check against a sketch.

Step 3: Test the x axis on its own

Project both boxes onto the horizontal axis and each one becomes a range. The ranges overlap when the left edge of box a is less than the right edge of box b, and the right edge of box a is greater than the left edge of box b. MDN’s 3D guide describes the same idea as one test per axis, using the boxes’ boundaries. If this test fails, the boxes are separated on x and you can stop. A box that fails on any single axis is not colliding, as the AABB explainer notes.

Working out the x and y ranges on paper first makes each comparison easier to trust before you type it.

Step 4: Test the y axis the same way

Repeat the comparison with the top and bottom edges. The top of box a must be above the bottom of box b, and the bottom of box a must be below the top of box b. Treat this as a separate question from the x test. Two boxes can share a column and still be far apart vertically, and the other way round.

Step 5: Combine both axes and return the result

Rectangles intersect only when both axis tests pass. The same rectangle-intersection thread states that if all the conditions are true, the rectangles intersect. That gives four comparisons joined by AND. Here is the whole check in JavaScript:

function overlaps(a, b) {
  return a.x < b.x + b.width &&
       a.x + a.width > b.x &&
       a.y < b.y + b.height &&
       a.y + a.height > b.y;
}

This is the form MDN uses, with a first line of this.position.x < other.position.x + other.width followed by the other three comparisons. Call it with your player and each obstacle. A true result means the hitboxes overlap, and your game decides what happens next.

Stock illustration of JavaScript in an editor. The overlap function above is short enough to read in one glance.

Step 6: Decide whether touching edges collide

The four comparisons above are strict, using only less-than and greater-than. With strict comparisons, two boxes whose edges meet exactly are not colliding, because there is no overlapping area. MDN’s 3D guide writes its condition with less-than-or-equal and greater-than-or-equal, so boxes that merely touch do count there.

White strips run across and between the dark rectangular blocks at varying angles.

Neither convention is wrong, so pick one and use it everywhere. Choose strict comparisons when objects are placed flush against each other, such as tiles in a grid or a character resting on a floor, and you do not want contact alone to register as a hit. Choose inclusive comparisons when contact should trigger something, such as a pickup that should fire on touch.

Where AABB stops being enough

AABB fails for rotated sprites because the box cannot turn with the sprite. The constraint exists for performance reasons: the overlap of non-rotated boxes can be checked with logical comparisons alone, while rotated boxes would need trigonometry. If you rotate the sprite, its hitbox still aligns with the axes, and the check no longer describes the shape on screen. The method is, in the words of the AABB explainer, less flexible when objects rotate.

You have three practical options.

  • Enlarge the box. MDN suggests modifying the dimensions so the box still wraps the object after rotation. The check stays cheap, but the hitbox is looser than the sprite. This suits fast action games where a slightly generous hitbox is acceptable.
  • Switch shape. MDN’s guide notes that spheres are invariant to rotation, and the flat counterpart is a circle. If the object is roughly round, circle collision detection avoids the rotation problem entirely.
  • Use an oriented box. An oriented bounding box rotates with its object and fits more tightly than an AABB. Testing two rectangles with the separating axis method needs four axes. Reach for this only when a loose hitbox visibly hurts gameplay.

For anything that stays upright, such as platformer tiles, walls and most pickups, AABB remains the sensible default. A top-down collision write-up calls AABBs very useful for simple collision detection and 2D physics.

Checkpoint: what each result means

Situation Result Planning use
Gap on any one side No collision Return false as soon as one axis test fails.
Ranges overlap on both x and y Collision Trigger your response, such as damage, pickup or blocking.
Edges meet exactly No collision with strict comparisons, collision with inclusive ones Choose the rule once and apply it to every check.
Sprite is rotated Box no longer matches the shape Enlarge the box, use a circle, or move to an oriented box.

These outcomes follow MDN’s planar and volumetric collision guides.

Frequently asked questions

Does the hitbox have to match the sprite exactly?

No. MDN describes a hitbox as a simple generic shape covering the entity, so it is not pixel perfect. Size it from the sprite’s width and height, then adjust it until the game feels fair.

What should happen once an overlap is detected?

Detection only reports that a collision exists. To separate the boxes, a top-down AABB write-up suggests moving them along the shortest intersection axis.

Do I need to test every object against every other object?

No. MDN notes that testing every entity with every other wastes cycles, and that games usually split collision into a broad phase and a narrow phase. Use a cheap broad check to limit the pairs, then run the AABB test on the remaining pairs as the narrow check.