Separating Axis Theorem — Definition, Formula & Example
The Separating Axis Theorem says that two convex shapes do not touch if and only if you can find a direction (an axis) along which their "shadows" do not overlap. Game engines and robots use it to detect collisions between polygons.
Let and be disjoint, nonempty, closed convex subsets of , with at least one of them compact. Then there exist a nonzero vector and a real number such that for all and . The hyperplane separates the sets, and is a separating axis. For two convex polygons in the plane, it is enough to test the axes normal to their edges.
Key Formula
Where:
- = Candidate axis direction (it does not need to be a unit vector to test for overlap)
- = Position vectors of the vertices of polygon P
- = Projection interval of polygon P onto the axis
- = The two convex shapes being tested
How It Works
Pick a candidate axis . Project every vertex of each shape onto it by computing dot products, and record each shape's minimum and maximum. If the two intervals do not overlap, you have found a separating axis and the shapes are disjoint. If the intervals overlap on every edge normal of both polygons, the polygons intersect. In 3D you must also test the cross products of edge pairs, one edge from each shape.
Worked Example
Problem: In a 2D game, measured in feet, a square crate has vertices , , , . A triangular ramp has vertices , , . Do they collide?
Step 1: Test the x- and y-axes. These are the square's edge normals, and they are also the normals of the triangle's vertical and horizontal edges. On both axes, the square spans 0 to 2 and the triangle spans 1 to 4. The intervals overlap, so neither axis separates the shapes.
Step 2: Test the last edge normal. The triangle's slanted edge runs from to , with direction , so a normal is . Project each vertex by computing .
Step 3: Since , the intervals are disjoint, so is a separating axis. To convert the gap to feet, divide by the length of , which is .
Answer: The shapes do not collide. Their projections overlap on both coordinate axes, but the diagonal axis separates them by about 0.71 feet.
Why It Matters
Physics engines in video games use this test every frame to detect collisions between boxes and polygons. Robot motion-planning software uses it the same way. In its general form, the hyperplane separation theorem is the foundation of duality in convex optimization and of support vector machines in machine learning.
Common Mistakes
Mistake: Applying the test to concave shapes, such as a C-shaped wall.
Correction: The theorem requires convexity. A small square tucked inside the notch of a C-shape has no separating axis, even though the two shapes never touch. Split concave shapes into convex pieces and test each pair.
