Sun, 06/20/2010 - 13:55 — golgobot
## Introduction

## The Basics

## The Algorithm

## Interactive Demo

Try playing around with the demo below to get a feel for the geometry. The color coding is the same as in the figures above. Just follow the direction in the right hand column.

Separation of Axis is an algorithm used for collision detection in some physics engines. The algorithm is very straightforward, and easy to understand geometrically. It also incurs a high computational cost , where and are the number of faces for each polygon, so it is best to perform this test only when absolutely necessary. Most physics engines have a few phases of collision detection. These phases might include some kind of spatial partitioning like quad-trees in 2-dimensions or oct-trees in 3-dimensions, or cheap bounding box or bounding sphere checks.

If you can draw a line between two convex polygons in 2-dimensions without that line intersecting one of the polygons, then they don't intersect. If no such line exists, then they do intersect. The image below shows an example of a non-intersecting polygon pair. The dotted line is the *separating line* and the green line is the *separating axis*

For two convex polygons, first choose a face. In the diagram below each is a vertex. The superscript identifies the shape, and the subscript is the vertex. So let's choose . The *separating axis*, , is the perpendicular of the face. Once we have our *separating axis*, we project each vertex of both polygons onto that *separating axis*. Notice that since and are perpendicular to the *separating axis* that we only need to choose one of those vertices to project. The bounds of each shape's projections form what are called *projection intervals* shown in red. The idea is that if these intervals do not overlap, then we know that these two polygons do not intersect. If, on the other hand, we iterate through each face of each polygon and perform this test and find that each and every set of projection interval overlap, then we know that these two polygons intersect. So back to the algorithm. For each vertex compute the length of the projection along the separating axis

where is the unit vector of . So is the distance along to . Using this distance is convenient because all we care about is whether or not these intervals overlap. For each shape, the minimum, , and maximum, , projection distances represent its projection interval. The intervals overlap if and . If they don't overlap, then we can exit the immediately because they don't intersect. If, on the other hand, each set of projection intervals overlap for each separating axis of both polygons, then there is a collision.

One thing I really like about Separation of Axis is that it provides a cheat to calculate the point of contact given that two polygons are intersecting. All you have to do is find the axis with the least *interval overlap*, shown in white in the figure below. Then just project the vertex of the shape that does not own that separating axis, onto the face that created that separating axis. So, in the figure below, the point of contact becomes