Separating Axis Theorem
AABBs and circles are fast but limited: a rotated crate, a sloped platform or a triangular spaceship needs a test for convex polygons (in 3D, convex polyhedra and oriented boxes). The Separating Axis Theorem (SAT) gives one:
Two convex shapes do not overlap if and only if there is a line (an axis) onto which their projections do not overlap.
Picture shining a light from the side and looking at the shadows on a wall: if you can find a direction where the two shadows have a gap, the shapes are apart. For two convex polygons, you only need to try a finite set of axes: the normals of every edge of both polygons.
Projecting a polygon onto an axis
Projecting is the dot product again: dot(vertex, axis) for every vertex, keeping the minimum and maximum. That gives an interval, just like an AABB on one axis. (In fact the AABB test is SAT with only the x and y axes.)
#include <iostream>
#include <iomanip>
#include <vector>
#include <algorithm>
#include <cmath>
#include <limits>
struct Vec2 { double x, y; };
double dot(Vec2 a, Vec2 b) { return a.x * b.x + a.y * b.y; }
using Poly = std::vector<Vec2>; // convex, counter-clockwise
void project(const Poly& p, Vec2 axis, double& mn, double& mx) {
mn = std::numeric_limits<double>::max(); mx = -mn;
for (Vec2 v : p) { double d = dot(v, axis); mn = std::min(mn, d); mx = std::max(mx, d); }
}
bool overlapSAT(const Poly& a, const Poly& b) {
for (const Poly* shape : {&a, &b}) {
const Poly& p = *shape;
for (size_t i = 0; i < p.size(); i++) {
Vec2 e{p[(i + 1) % p.size()].x - p[i].x, p[(i + 1) % p.size()].y - p[i].y};
Vec2 axis{-e.y, e.x}; // edge normal (no need to normalize for a yes/no test)
double minA, maxA, minB, maxB;
project(a, axis, minA, maxA);
project(b, axis, minB, maxB);
if (maxA < minB || maxB < minA) return false; // found a separating axis
}
}
return true; // no gap on any axis
}
int main() {
std::cout << std::boolalpha;
Poly square{{0, 0}, {2, 0}, {2, 2}, {0, 2}};
Poly triangleNear{{1.5, 1.5}, {4, 2}, {2.5, 4}}; // pokes into the square's corner
Poly triangleGap{{3, 1.5}, {3, 3}, {1.5, 3}}; // AABBs overlap, shapes don't
Poly diamond{{3, 1}, {4, 0}, {5, 1}, {4, 2}};
std::cout << "square vs triangleNear: " << overlapSAT(square, triangleNear) << "\n";
std::cout << "square vs triangleGap: " << overlapSAT(square, triangleGap) << "\n";
std::cout << "square vs diamond: " << overlapSAT(square, diamond) << "\n";
}square vs triangleNear: true
square vs triangleGap: false
square vs diamond: false
The second pair is the interesting one: their bounding boxes overlap, so a broad-phase AABB test would report a possible collision, but SAT finds the gap along one of the triangle's edge normals.
Minimum translation vector
SAT can also tell you how to push the shapes apart. On each axis, compute the overlap amount; the axis with the smallest overlap (using normalized axes this time) is the direction of least penetration, and that overlap is the depth. Engines call the result the contact normal and penetration depth.
#include <iostream>
#include <iomanip>
#include <vector>
#include <algorithm>
#include <cmath>
#include <limits>
struct Vec2 { double x, y; };
double dot(Vec2 a, Vec2 b) { return a.x * b.x + a.y * b.y; }
using Poly = std::vector<Vec2>;
void project(const Poly& p, Vec2 axis, double& mn, double& mx) {
mn = std::numeric_limits<double>::max(); mx = -mn;
for (Vec2 v : p) { double d = dot(v, axis); mn = std::min(mn, d); mx = std::max(mx, d); }
}
Vec2 centroid(const Poly& p) {
Vec2 c{0, 0};
for (Vec2 v : p) { c.x += v.x; c.y += v.y; }
return {c.x / p.size(), c.y / p.size()};
}
// Returns false if separated; otherwise the push for `a` (normal * depth).
bool satMTV(const Poly& a, const Poly& b, Vec2& push) {
double best = std::numeric_limits<double>::max();
Vec2 bestAxis{0, 0};
for (const Poly* shape : {&a, &b}) {
const Poly& p = *shape;
for (size_t i = 0; i < p.size(); i++) {
Vec2 e{p[(i + 1) % p.size()].x - p[i].x, p[(i + 1) % p.size()].y - p[i].y};
double len = std::hypot(e.x, e.y);
Vec2 axis{-e.y / len, e.x / len};
double minA, maxA, minB, maxB;
project(a, axis, minA, maxA);
project(b, axis, minB, maxB);
double overlap = std::min(maxA, maxB) - std::max(minA, minB);
if (overlap <= 0) return false;
if (overlap < best) { best = overlap; bestAxis = axis; }
}
}
Vec2 ca = centroid(a), cb = centroid(b); // make the push point from b toward a
if (dot({ca.x - cb.x, ca.y - cb.y}, bestAxis) < 0) bestAxis = {-bestAxis.x, -bestAxis.y};
push = {bestAxis.x * best, bestAxis.y * best};
return true;
}
double z0(double v) { return std::fabs(v) < 5e-4 ? 0.0 : v; }
int main() {
std::cout << std::fixed << std::setprecision(3);
Poly ramp{{0, 0}, {6, 0}, {6, 3}}; // a triangular ramp
Poly box{{2.6, 1.0}, {3.6, 1.0}, {3.6, 2.0}, {2.6, 2.0}}; // a crate sunk into the slope
Vec2 push;
if (satMTV(box, ramp, push))
std::cout << "push the box by (" << z0(push.x) << ", " << z0(push.y) << ")\n";
}push the box by (-0.320, 0.640)
The push is perpendicular to the ramp's slope (direction (-1, 2) normalized), not straight up: SAT found that the shortest way out is along the slope's normal.
SAT in 3D
For 3D convex shapes the candidate axes are the face normals of both shapes plus the cross products of every pair of edges (one edge from each shape). For two oriented boxes (OBBs) that is 3 + 3 + 9 = 15 axes: the classic OBB–OBB test used by physics engines and by Unity's Physics.ComputePenetration. For general convex meshes, physics engines usually switch to GJK and EPA, which the game-physics course covers.
Note: SAT only works for convex shapes. A concave shape (an L-shaped room, a horseshoe) must be split into convex pieces first. That is what Unity's mesh collider "Convex" checkbox and Unreal's "auto convex collision" are about.
Test yourself
Which axes must you test for two convex polygons in 2D?
- Only x and y
- The edge normals of both polygons
- Every angle in 1° steps
- The vectors between all vertex pairs
What does SAT conclude if the projections overlap on every tested axis?
- The shapes are separated
- The shapes overlap
- Nothing; more axes are needed
- The shapes are identical
Why can't SAT be used directly on an L-shaped polygon?
- It has too many vertices
- SAT only works for convex shapes
- L-shapes have no normals
- It can, with no changes