GJK and EPA convex collision detection
Narrow-phase test for convex shapes: GJK finds whether two shapes overlap or their closest points through support mappings, and EPA recovers penetration depth and normal when they do.
Also known as GJK, Gilbert-Johnson-Keerthi, EPA, expanding polytope algorithm
In the family Simulation & animation, collision detection
Where it runs
Built inOfficial add-onPluginNone foundNot researched
| Engine | Support | How |
|---|---|---|
| three.js r187 | None found | |
| Babylon.js 9.38.0 | None found | |
| Godot 4.7 | Built in | Physics: default GodotPhysics3D (GJK/EPA); Jolt Physics is also bundled docs Internal to the physics engines; not a user-facing setting. |
| Unity | Built in | Built-in 3D Physics (NVIDIA PhysX narrow-phase convex collision via MeshCollider.convex / primitive colliders) docs |
| Unreal 5.7 | None found | |
| Blender 5.2 | None found | |
| WebGPU W3C | None found | |
| Metal | None found |
How it works
Both algorithms work on the Minkowski difference of two convex shapes, which is never built explicitly. All they need is a support function: for a direction, return the farthest point of each shape, and subtract. GJK keeps a small simplex (point, segment, triangle, tetrahedron) inside that difference. Each iteration asks for the support point toward the origin, adds it, and reduces the simplex to the feature closest to the origin. If the simplex encloses the origin the shapes overlap; if the support point cannot pass the origin, they are separate and the last closest feature gives distance and witness points. When overlap is found, EPA starts from the final simplex, inflates it into a polytope, and repeatedly expands the face closest to the origin by a new support point until the face stops moving; that face's normal and distance are the minimum translation vector, and barycentric weights give contact points. Spheres, capsules, boxes and hulls plug in with only a support function.
When to choose it
Reach for it when
- need overlap test between arbitrary convex colliders
- want penetration depth and normal for two hulls
- closest distance between two convex shapes for physics
- need a generic narrow phase using only support functions
Choose it as the generic narrow phase for arbitrary convex colliders (hulls, capsules, cones, rounded boxes) and for continuous or distance queries. Prefer a dedicated analytic test for sphere-sphere or box-box pairs, which are faster and more robust. Prefer CoACD Approximate Convex Decomposition to turn concave meshes into convex pieces GJK can consume, three-mesh-bvh for triangle-mesh queries, and Sequential impulses (rigid bodies) or Extended Position-Based Dynamics to resolve the contacts GJK and EPA produce.
Field marks
- Degenerate simplices (coplanar or collinear points) cause infinite loops or NaNs unless guarded with tolerances
- EPA returns wrong or unstable normals for shallowly penetrating or very large or thin shapes
- Tolerance mismatch between GJK and EPA leaves shapes reported as touching with no usable normal
- Contact manifold from one EPA point jitters; stacking needs a manifold generator or persistent contacts
- Only convex shapes: concave meshes silently give the hull's contacts
Presets
GJK & EPA convex collision — live Minkowski difference A−B, enclosing simplex & EPA penetration vector
{
"P_ORBIT_SEP": 0.22,
"P_ROT_SPEED": 1,
"P_MINKOWSKI_VIS": 1,
"P_SHAPE_ROUND": 0.03
}Show GLSL (170 lines)
const float P_ORBIT_SEP = 0.22; // P Orbit Distance [0.08, 0.42, 0.01]
const float P_ROT_SPEED = 1.0; // P Motion Speed [0.0, 2.5, 0.05]
const float P_MINKOWSKI_VIS = 1.0; // P Minkowski Glow [0.2, 1.8, 0.05]
const float P_SHAPE_ROUND = 0.03; // P Margin Radius [0.005, 0.07, 0.005]
mat2 rot2(float a) { float c = cos(a), s = sin(a); return mat2(c, -s, s, c); }
float sdSegment(vec2 p, vec2 a, vec2 b) {
vec2 pa = p - a, ba = b - a;
float h = clamp(dot(pa, ba) / max(dot(ba, ba), 1e-5), 0.0, 1.0);
return length(pa - ba * h);
}
// Support function for a regular N-gon centered at c, rotated by ang, radius r, margin m
vec2 supportPoly(vec2 dir, vec2 c, float r, float ang, int nSides, float margin) {
vec2 ld = rot2(-ang) * dir;
float bestDot = -1e9;
vec2 bestV = vec2(0.0);
for (int i = 0; i < 6; i++) {
if (i >= nSides) break;
float a = float(i) * (6.2831853 / float(nSides));
vec2 v = vec2(cos(a), sin(a)) * r;
float d = dot(v, ld);
if (d > bestDot) { bestDot = d; bestV = v; }
}
vec2 nd = dir / max(length(dir), 1e-5);
return c + rot2(ang) * bestV + nd * margin;
}
// Signed distance to a regular N-gon via exact half-space intersection
float sdPoly(vec2 p, vec2 c, float r, float ang, int nSides, float margin) {
vec2 q = rot2(-ang) * (p - c);
float dMax = -1e9;
float apothem = r * cos(3.14159265 / float(nSides));
for (int i = 0; i < 6; i++) {
if (i >= nSides) break;
float a = (float(i) + 0.5) * (6.2831853 / float(nSides));
vec2 n = vec2(cos(a), sin(a));
dMax = max(dMax, dot(q, n) - apothem);
}
return dMax - margin;
}
// Signed distance to exact convex Minkowski difference M = A - B via support planes!
// Also outputs EPA closest boundary point to the origin (minimum translation vector)
float sdMinkowski(vec2 p, vec2 cA, float rA, float aA, vec2 cB, float rB, float aB, float m, out vec2 epaVec, out float epaDepth) {
float dMax = -1e9;
epaDepth = 1e9;
epaVec = vec2(1.0, 0.0);
for (int i = 0; i < 24; i++) {
float ang = float(i) * (6.2831853 / 24.0);
vec2 dir = vec2(cos(ang), sin(ang));
vec2 sup = supportPoly(dir, cA, rA, aA, 5, m) - supportPoly(-dir, cB, rB, aB, 3, m);
float planeDist = dot(sup, dir);
// Half-space signed distance at query point p
dMax = max(dMax, dot(p, dir) - planeDist);
// EPA: minimum distance from origin (0,0) to supporting hyperplane
if (planeDist < epaDepth) {
epaDepth = planeDist;
epaVec = dir * planeDist;
}
}
return dMax;
}
vec3 aces(vec3 x) {
x = max(x, 0.0);
return (x * (2.51 * x + 0.03)) / (x * (2.43 * x + 0.59) + 0.14);
}
void mainImage(out vec4 fragColor, in vec2 fragCoord) {
vec2 uv = (fragCoord - 0.5 * iResolution.xy) / iResolution.y;
float px = 1.0 / max(iResolution.y, 1.0);
float tAnim = iTime * max(P_ROT_SPEED, 0.0);
float sep = clamp(P_ORBIT_SEP, 0.05, 0.48);
float mGlow = clamp(P_MINKOWSKI_VIS, 0.1, 2.0);
float margin = clamp(P_SHAPE_ROUND, 0.004, 0.08);
// Split viewport: Left = World Space (Shapes A & B), Right = Configuration Space (Minkowski Diff A - B)
bool rightPane = uv.x > 0.0;
vec2 lp = uv - vec2(rightPane ? 0.42 : -0.42, 0.0);
// Two convex shapes oscillating cleanly between separated and penetrating states
float osc = sep * (0.65 + 0.55 * sin(tAnim * 0.85));
vec2 cA = vec2(-osc * 0.55, 0.06 * cos(tAnim * 0.6));
vec2 cB = vec2(osc * 0.55, -0.06 * cos(tAnim * 0.6));
float rA = 0.145;
float rB = 0.135;
float aA = tAnim * 0.45;
float aB = -tAnim * 0.62 + 0.5;
vec2 epaVec;
float epaDepth;
float dMinkOrigin = sdMinkowski(vec2(0.0), cA, rA, aA, cB, rB, aB, margin, epaVec, epaDepth);
bool colliding = dMinkOrigin < 0.0;
// Blueprint grid background
vec3 col = vec3(0.028, 0.038, 0.062);
vec2 g = abs(fract(lp * 10.0 - 0.5) - 0.5);
col += vec3(0.02, 0.032, 0.048) * smoothstep(0.035, 0.005, min(g.x, g.y));
// Subtle coordinate axes in each pane
float axes = min(abs(lp.x), abs(lp.y));
col += vec3(0.08, 0.12, 0.18) * smoothstep(px * 2.2, px * 0.4, axes);
if (!rightPane) {
// Left Pane: World Space convex shapes A (pentagon) and B (triangle)
float dA = sdPoly(lp, cA, rA, aA, 5, margin);
float dB = sdPoly(lp, cB, rB, aB, 3, margin);
vec3 colA = vec3(0.18, 0.72, 0.96);
vec3 colB = vec3(0.96, 0.58, 0.18);
if (dA < 0.0) col = mix(col, colA, 0.48);
if (dB < 0.0) col = mix(col, colB, 0.48);
if (dA < 0.0 && dB < 0.0) {
// Overlap intersection zone glowing hot crimson
col = mix(col, vec3(1.0, 0.22, 0.36), 0.75);
}
col = mix(col, colA * 1.25, smoothstep(px * 3.2, px * 0.6, abs(dA)));
col = mix(col, colB * 1.25, smoothstep(px * 3.2, px * 0.6, abs(dB)));
// Draw EPA Minimum Translation Vector (MTV) separating B from A when colliding
if (colliding) {
vec2 mtvStart = 0.5 * (cA + cB);
vec2 mtvEnd = mtvStart + epaVec;
float dMtv = sdSegment(lp, mtvStart, mtvEnd);
col = mix(col, vec3(1.0, 0.96, 0.42), smoothstep(px * 4.0, px * 0.8, dMtv));
col = mix(col, vec3(1.0, 0.98, 0.65), smoothstep(0.016, 0.005, length(lp - mtvEnd)));
}
} else {
// Right Pane: Minkowski Difference M = A - B centered on Origin (0,0)
vec2 dummyVec; float dummyD;
float dM = sdMinkowski(lp, cA, rA, aA, cB, rB, aB, margin, dummyVec, dummyD);
// Distance field contour rings outside Minkowski sum
float iso = smoothstep(0.07, 0.01, abs(fract(max(dM, 0.0) * 14.0) - 0.5));
col += vec3(0.07, 0.14, 0.22) * iso * step(0.0, dM) * 0.6;
// Fill Minkowski Difference polygon (emerald when separated, crimson when Origin is inside!)
vec3 minkCol = colliding ? vec3(0.95, 0.24, 0.36) : vec3(0.16, 0.86, 0.58);
if (dM < 0.0) {
col = mix(col, minkCol * 0.55, 0.58 * min(mGlow, 1.4));
}
col = mix(col, minkCol * 1.35, smoothstep(px * 3.5, px * 0.7, abs(dM)));
// Construct a 3-vertex GJK Simplex inside M around the origin
vec2 s0 = supportPoly(vec2(1.0, 0.15), cA, rA, aA, 5, margin) - supportPoly(vec2(-1.0, -0.15), cB, rB, aB, 3, margin);
vec2 s1 = supportPoly(vec2(-0.55, 0.84), cA, rA, aA, 5, margin) - supportPoly(vec2(0.55, -0.84), cB, rB, aB, 3, margin);
vec2 s2 = supportPoly(vec2(-0.45, -0.89), cA, rA, aA, 5, margin) - supportPoly(vec2(0.45, 0.89), cB, rB, aB, 3, margin);
float dSimplex = min(sdSegment(lp, s0, s1), min(sdSegment(lp, s1, s2), sdSegment(lp, s2, s0)));
col = mix(col, vec3(0.86, 0.94, 1.0), smoothstep(px * 2.8, px * 0.6, dSimplex) * 0.85);
// Highlight EPA penetration vector from Origin (0,0) to closest edge of M
if (colliding) {
float dEpa = sdSegment(lp, vec2(0.0), epaVec);
col += vec3(1.0, 0.92, 0.32) * exp(-dEpa * 85.0) * 0.85;
col = mix(col, vec3(1.0, 0.96, 0.45), smoothstep(px * 3.8, px * 0.8, dEpa));
col = mix(col, vec3(1.0, 0.98, 0.72), smoothstep(0.017, 0.005, length(lp - epaVec)));
}
// Origin (0,0) target crosshair
float dOrig = length(lp);
col = mix(col, vec3(1.0), smoothstep(0.022, 0.008, dOrig));
col = mix(col, colliding ? vec3(1.0, 0.25, 0.35) : vec3(0.2, 0.95, 0.65), smoothstep(0.012, 0.004, dOrig));
}
// Center pane divider
col = mix(col, vec3(0.75, 0.84, 0.96), smoothstep(px * 2.5, px * 0.5, abs(uv.x)));
fragColor = vec4(pow(clamp(aces(col), 0.0, 1.0), vec3(1.0 / 2.2)), 1.0);
}Attribution: Original interactive WebGL2 fragment shader written for Texel Atlas (MIT).
Measured GPU weight & timing
| Preset | GPU | Weight / p50 / p95 | Method |
|---|---|---|---|
| gjk-epa-collision-detection-live | Tesla T4 1920x1080 | 52.5× baseline (heavy) · 1.65 ms / 1.80 ms | opengl-timer-query · 40 frames · GPU Weight Atlas · Colab T4 sweep |
| gjk-epa-collision-detection-live | NVIDIA L4 1920x1080 | 55.7× baseline (heavy) · 0.880 ms / 0.900 ms · 1.9× faster on L4 | opengl-timer-query · Cross-architecture validation |
| gjk-epa-collision-detection-live | NVIDIA A100-SXM4-40GB 1920x1080 | 37.4× baseline (heavy) · 1.08 ms / 1.09 ms · 1.5× faster on A100 | opengl-timer-query · Cross-architecture validation |
Lineage
From the citation graph (Crossref reference lists): cited by CoACD Approximate Convex Decomposition, Iterative Closest Point (ICP) registration, Ray-Traced Prism Displacement Mapping.
Crosses
- Sequential impulses (rigid bodies): GJK/EPA output (normal, depth, witness points) becomes the contact constraints the impulse solver iterates over.
- CoACD Approximate Convex Decomposition: Decomposition supplies the convex parts whose support functions GJK queries for concave objects.
Sources
- primaryE.G. Gilbert, D.W. Johnson, S.S. Keerthi (1988). A fast procedure for computing the distance between complex objects in three-dimensional space. IEEE Journal on Robotics and Automation.Verified via crossref · 2026-11-29doi:10.1109/56.2083
- primaryGino Van den Bergen (1999). A Fast and Robust GJK Implementation for Collision Detection of Convex Objects. Journal of Graphics Tools.Verified via crossref · 2026-11-29doi:10.1080/10867651.1999.10487502
- tutorialCasey Muratori. Implementing GJK (Molly Rocket). caseymuratori.com.Verified via live page · 2026-11-29
- implementationjrouwe (2021). jrouwe/JoltPhysics. A multi core friendly rigid body physics and collision detection library. Written in C++. Suitable for games and VR applications. Used by Horizon Forbidden West and Death Stranding 2.GitHub API · license MIT · 2026-11-29
- implementationbulletphysics (2011). bulletphysics/bullet3. Bullet Physics SDK: real-time collision detection and multi-physics simulation for VR, games, visual effects, robotics, machine learning etc.GitHub API · license NOASSERTION · 2026-11-29