Separating Axis Theorem for 2D Collisions

separating axis theorem

A rotated crate clips a wall and your bounding-box check says nothing happened. The separating axis theorem fixes that: two convex polygons are apart if any single axis shows a gap between their projections. They collide only when every tested axis overlaps.

This explainer builds the idea from projection pictures and one rotated polygon pair, and sits alongside our wider notes on 2d game programming for beginners. Circles have no edges to take normals from, so circle collision detection is handled separately.

What the separating axis theorem states

The separating axis theorem says that two closed convex objects are disjoint if a separating line exists onto which their projections are disjoint, as Patrick Bell’s SAT write-up puts it. Another common phrasing is that two convex shapes do not collide if and only if there is an axis on which their projections do not overlap, as this AABB versus OBB walkthrough states it. The Wikibooks game development guide describes the practical idea as creating virtual axes from your geometry and comparing the shapes along them.

Picture a lamp shining along the axis direction and casting each polygon’s shadow onto a line. If the two shadows leave a gap, you could slide a wall between the polygons, so they cannot be touching. If the shadows overlap, that one axis tells you nothing yet, and the next axis gets its turn.

Why it only works for convex shapes

A convex shape projects onto an axis as a single line segment, which is what makes comparing two intervals a reliable test. The theorem is stated for convex objects for that reason. Research on separating lines for pairwise disjoint compact convex sets in the plane, such as this arXiv preprint, is framed around convex polygons and separating lines too. For a concave sprite outline, a sensible approach is to treat it as several convex pieces and test each piece.

Open notebooks and a laptop on a shared table. Comparing each polygon’s edge list against the axis list is an easy review step for catching a missing axis.

Misconceptions that cause bugs

Most SAT bugs come from a handful of wrong assumptions, summarised below.

Common belief What holds Planning use
SAT works on any polygon outline. It is stated for convex objects, where each projection is one segment. Split concave shapes into convex pieces before testing.
There are endless axes to check. Unrestricted, the number of possible axes is infinite. For polygons, only directions perpendicular to an edge of either shape matter. Build the axis list from the edges of both polygons.
One overlapping axis means a hit. A collision needs overlap on every candidate axis. Report a hit only after the whole list has been checked.
One gap is not conclusive. If the segments on an axis do not overlap, neither do the objects. Return early on the first gap.
SAT only answers yes or no. Tracking the minimum overlap gives the push-out. Keep the smallest overlap and its axis while looping.

These points come from Patrick Bell’s SAT article and the AABB versus OBB walkthrough.

Which axes to test

For polygons you test one axis perpendicular to each edge, taken from both shapes. In the plane, per the same write-up, you only need to check each direction perpendicular to an edge. For a pair of rotated rectangles the candidate axes are the edge directions of both rectangles, as the OBB walkthrough notes. The number of axes to test is therefore the edge normals of the first polygon plus the edge normals of the second.

Stylized render of overlapping polygon planes; it is not a 2D collision diagram.

Skipping the second polygon’s normals is the classic mistake. A gap can be visible only along a direction that belongs to the other shape, so a test built from one polygon’s edges will report false collisions.

What each axis costs

A single axis test costs around 10–12 floating-point operations, according to the same OBB walkthrough. That makes SAT cheap per axis, and the early exit on the first gap means most non-colliding pairs finish after only a few axes. Run a quick broad-phase check first if you have many objects, so the full axis loop only runs on plausible pairs.

Projecting a polygon onto an axis

To project a polygon, take every vertex in world space, project it onto the axis, and keep the smallest and largest results. Those two values are the ends of the polygon’s segment. The Bell article describes looping through each vertex position in world space and projecting it onto the separating axis. The dot product does the projecting, as the OBB walkthrough lists: the dot product of a vertex position with the axis direction says where that vertex lands along the axis. Normalise the axis first if you want overlaps measured in real distance.

Generic code-editor view; no vector projection function is legible on screen.
  1. Pick an axis. Take an edge of either polygon and build a perpendicular direction.
  2. Project polygon A. Compute each vertex’s dot product with the axis and record the minimum and maximum.
  3. Project polygon B. Repeat for the second shape, giving the two segments for this axis.
  4. Compare the intervals. If they do not overlap, stop: the shapes are separated.

A worked pair: a rotated rectangle and a triangle

Take a rectangle turned at an awkward angle and a triangle approaching one of its corners. A plain axis-aligned box check would see the bounding boxes touching and report a hit, even though the shapes themselves have not met.

Start with one long edge of the rectangle and draw its perpendicular as the axis. Project the four rectangle corners and the three triangle corners onto it. The rectangle forms one segment; the triangle forms another. In this picture the triangle sits beyond the rectangle’s end, so the segments leave a gap and the test stops with no collision. Only the perpendicular of that one edge was needed to prove it.

Sketching the polygons and their projections by hand makes the interval gap easy to see before you write any code.

Now push the triangle until its tip enters the rectangle. Run the same loop. Every axis taken from the rectangle’s edges shows overlapping segments, and so does every axis taken from the triangle’s edges. With no gap on any candidate axis, the shapes are colliding. Only after the full list has been exhausted can you say so.

Getting the push-out distance

The push-out comes from the same loop. As you test each axis, keep track of the minimum overlap, as the Bell article advises. The axis with the smallest overlap is the penetration axis, which acts as the contact normal. The overlap along it is the penetration distance: how far along that axis you need to move the shapes apart. Together they form the minimum translation vector, the shortest shove that separates the polygons.

Remember which polygon the axis came from, and orient the vector so it pushes the right shape away from the other. What you then do with that vector, such as moving one body or splitting the correction, belongs to collision response 2d game logic.

Implementation pitfalls

  • Mixing coordinate spaces. Project world-space vertices. Using local-space points for one shape and world-space for the other gives meaningless intervals.
  • Quitting too soon on overlap. An overlapping axis is not a verdict. Only a gap ends the test early.
  • Forgetting to track the smallest overlap. If you need push-out, record the minimum and its axis during the loop rather than rerunning the test.
  • Feeding in concave outlines. The single-segment projection guarantee disappears, so decompose first.

Frequently asked questions

Question Answer
Can I stop at the first axis that shows a gap? Yes. A gap on any one axis means the objects do not overlap, so no further axes need testing.
Do rotated rectangles need a different method from other convex polygons? No. Rectangles are a special case where the candidate axes are the edges of both rectangles, so the same loop applies.
Is the axis test expensive? A single axis test costs around 10–12 floating-point operations, so even a full polygon pair stays inexpensive.
Why do I need a minimum overlap at all? It identifies the axis along which the shapes can be separated by the shortest move, which is the push-out direction.