---
id: "gjk-epa-collision-detection"
title: "GJK and EPA convex collision detection"
aliases: [GJK, Gilbert-Johnson-Keerthi, EPA, expanding polytope algorithm]
domain: "simulation"
stage: "simulation"
realtime: "realtime"
platforms: [cpu, cuda, unity, unreal, godot, webgl2, webgpu, desktop-gpu]
year_introduced: 1988
inputs: [mesh]
outputs: [point-cloud]
style: "both"
dynamic: "dynamic"
cost: "cheap"
authority: "Gilbert et al., 1988"
url: "https://texelatlas.vercel.app/t/gjk-epa-collision-detection/"
sources_verified_at: "2026-11-29"
---
# GJK and EPA convex collision detection (Gilbert et al., 1988)

> 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.

## 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

## 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 use it

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](https://texelatlas.vercel.app/t/coacd-convex-decomposition.md) to turn concave meshes into convex pieces GJK can consume, [three-mesh-bvh](https://texelatlas.vercel.app/t/three-mesh-bvh.md) for triangle-mesh queries, and [Sequential impulses (rigid bodies)](https://texelatlas.vercel.app/t/sequential-impulses.md) or [Extended Position-Based Dynamics](https://texelatlas.vercel.app/t/xpbd.md) to resolve the contacts GJK and EPA produce.

## Cost

cheap — A handful of support evaluations per pair, usually under about ten iterations when warm-started from the previous frame's simplex.
Measured GPU Weight: 52.47× baseline (heavy) — 1.6477 ms median / 1.8002 ms p95 on Tesla T4/PCIe/SSE2 @ 1920x1080 (opengl-timer-query).
Ada L4 scaling: 55.7× baseline (heavy) — 0.8801 ms median (1.87× faster on L4).
Ampere A100 scaling: 37.36× baseline (heavy) — 1.076 ms median (1.53× faster on A100).

## Failure modes

- 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

## Engine support

- godot: builtin — `Physics: default GodotPhysics3D (GJK/EPA); Jolt Physics is also bundled` (evidence: https://github.com/godotengine/godot/blob/4.7.2-stable/modules/godot_physics_3d/gjk_epa.cpp, checked 2026-12-01) — Internal to the physics engines; not a user-facing setting.
- unity: builtin — `Built-in 3D Physics (NVIDIA PhysX narrow-phase convex collision via MeshCollider.convex / primitive colliders)` (evidence: https://docs.unity3d.com/6000.0/Documentation/Manual/PhysicsOverview.html, checked 2026-12-01)

## Dataflow

- Inputs: mesh
- Outputs: point-cloud
- Platforms: cpu, cuda, unity, unreal, godot, webgl2, webgpu, desktop-gpu

## Presets

### GJK & EPA convex collision — live Minkowski difference A−B, enclosing simplex & EPA penetration vector (glsl-fragment; rendered 2026-11-30)

```json
{
  "P_ORBIT_SEP": 0.22,
  "P_ROT_SPEED": 1,
  "P_MINKOWSKI_VIS": 1,
  "P_SHAPE_ROUND": 0.03
}
```

```glsl
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).

## Combines with

- [Sequential impulses (rigid bodies)](https://texelatlas.vercel.app/t/sequential-impulses.md): GJK/EPA output (normal, depth, witness points) becomes the contact constraints the impulse solver iterates over.
- [CoACD Approximate Convex Decomposition](https://texelatlas.vercel.app/t/coacd-convex-decomposition.md): Decomposition supplies the convex parts whose support functions GJK queries for concave objects.

## Sources

1. [primary] E.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. https://doi.org/10.1109/56.2083 (verified via crossref on 2026-11-29)
2. [primary] Gino Van den Bergen (1999) A Fast and Robust GJK Implementation for Collision Detection of Convex Objects. Journal of Graphics Tools. https://doi.org/10.1080/10867651.1999.10487502 (verified via crossref on 2026-11-29)
3. [tutorial] Casey Muratori Implementing GJK (Molly Rocket). caseymuratori.com. https://caseymuratori.com/blog_0003 (verified via http on 2026-11-29)
4. [implementation] jrouwe (2021) jrouwe/JoltPhysics. https://github.com/jrouwe/JoltPhysics License: MIT. (verified via github on 2026-11-29)
5. [implementation] bulletphysics (2011) bulletphysics/bullet3. https://github.com/bulletphysics/bullet3 License: NOASSERTION. (verified via github on 2026-11-29)

## Cite this entry

Texel. "GJK and EPA convex collision detection." https://texelatlas.vercel.app/t/gjk-epa-collision-detection/ (accessed 2026-12-01).
