Mathwords logoMathwords

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 AA and BB be disjoint, nonempty, closed convex subsets of Rn\mathbb{R}^n, with at least one of them compact. Then there exist a nonzero vector v\mathbf{v} and a real number cc such that v⋅x<c<v⋅y\mathbf{v}\cdot\mathbf{x} < c < \mathbf{v}\cdot\mathbf{y} for all x∈A\mathbf{x} \in A and y∈B\mathbf{y} \in B. The hyperplane v⋅z=c\mathbf{v}\cdot\mathbf{z} = c separates the sets, and v\mathbf{v} is a separating axis. For two convex polygons in the plane, it is enough to test the axes normal to their edges.

Key Formula

IP(v)=[min⁡iv⋅pi, max⁡iv⋅pi],separated if max⁡IA<min⁡IB  or  max⁡IB<min⁡IAI_P(\mathbf{v}) = \Big[\min_i \mathbf{v}\cdot\mathbf{p}_i,\ \max_i \mathbf{v}\cdot\mathbf{p}_i\Big], \qquad \text{separated if } \max I_A < \min I_B \ \text{ or } \ \max I_B < \min I_A
Where:
  • v\mathbf{v} = Candidate axis direction (it does not need to be a unit vector to test for overlap)
  • pi\mathbf{p}_i = Position vectors of the vertices of polygon P
  • IP(v)I_P(\mathbf{v}) = Projection interval of polygon P onto the axis
  • A,BA, B = The two convex shapes being tested

How It Works

Pick a candidate axis v\mathbf{v}. 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 (0,0)(0,0), (2,0)(2,0), (2,2)(2,2), (0,2)(0,2). A triangular ramp has vertices (4,1)(4,1), (1,4)(1,4), (4,4)(4,4). 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.
[0,2]∩[1,4]=[1,2]≠∅[0,2] \cap [1,4] = [1,2] \neq \varnothing
Step 2: Test the last edge normal. The triangle's slanted edge runs from (4,1)(4,1) to (1,4)(1,4), with direction (−3,3)(-3,3), so a normal is v=(1,1)\mathbf{v} = (1,1). Project each vertex by computing x+yx + y.
Square: 0,2,4,2⇒[0,4]Triangle: 5,5,8⇒[5,8]\text{Square: } 0, 2, 4, 2 \Rightarrow [0,4] \qquad \text{Triangle: } 5, 5, 8 \Rightarrow [5,8]
Step 3: Since 4<54 < 5, the intervals are disjoint, so (1,1)(1,1) is a separating axis. To convert the gap to feet, divide by the length of v\mathbf{v}, which is 2\sqrt{2}.
gap=5−42=12≈0.71 ft\text{gap} = \frac{5 - 4}{\sqrt{2}} = \frac{1}{\sqrt{2}} \approx 0.71 \text{ ft}
Answer: The shapes do not collide. Their projections overlap on both coordinate axes, but the diagonal axis (1,1)(1,1) 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.

Related Terms