skip to content

How does GJK decide whether two convex shapes intersect, using the Minkowski difference, support functions and its simplex cases?

level: seniorimportance: should knowfreq 48%

answer

  1. is the origin inside?
  2. never build the shape itself
  3. farthest point along a direction
  4. point, segment, triangle, tetrahedron

basics

~20 s

GJK tests whether the origin lies inside the Minkowski difference of the two shapes without building it: support points grow a simplex of up to four points toward the origin until it encloses the origin or a new point falls short of it.

solid answer

~50 s

Two convex shapes A and B overlap exactly when their **Minkowski difference** `A - B`, the set of all `a - b`, contains the origin. GJK never builds that set; it only needs each shape's **support function**, the farthest point along a direction, because `supportAB(d) = supportA(d) - supportB(-d)`. Start from any direction, take a support point, and search toward the origin. Each iteration fetches `w = supportAB(d)`; if `dot(w, d) < 0`, the shape's farthest extent in that direction falls short of the origin, so the shapes are separated. Otherwise add `w` to the simplex, reduce the simplex to the feature closest to the origin (vertex, edge, triangle or tetrahedron, decided by region tests), and point `d` from that feature at the origin. A tetrahedron that contains the origin means intersection. Any convex shape with a support function plugs in, and the distance variant returns closest points when the shapes are apart.

code

pseudocode · 20 lines
pseudocode
function gjkIntersect(A, B):
    d = B.center - A.center
    if dot(d, d) < EPSILON:
        d = (1, 0, 0)                   // any direction works to start
    simplex = [ supportAB(A, B, d) ]
    d = -simplex[0]
    for iteration in 1 .. MAX_ITERATIONS:
        if dot(d, d) < EPSILON:
            return true                 // origin lies on the simplex: touching
        w = supportAB(A, B, d)
        if dot(w, d) < 0:
            return false                // farthest point falls short of the origin
        simplex.push(w)
        (simplex, d, enclosed) = solveSimplex(simplex)
        if enclosed:
            return true
    return true                         // cap reached: treat as touching

function supportAB(A, B, d):
    return A.support(d) - B.support(-d)

go deeper

for a junior

Recall that GJK works on convex shapes and asks whether the origin lies inside the Minkowski difference of the two shapes.

for a middle

Explain the support function, how the difference's support point is formed from the two shapes, and the early exit when a support point falls short of the origin.

for a senior

Walk through the segment, triangle and tetrahedron cases on a whiteboard, and name the tolerances, iteration cap and coherence that make it robust in production.

for a principal

Judge when one generic support-function path for every shape pair beats a table of dedicated pair routines, given robustness, maintenance and per-pair speed.

## The Minkowski difference The **Minkowski difference** of two shapes, written `A - B`, is the set of every point `a - b` with `a` in A and `b` in B. If the shapes share any point `p`, then `p - p = 0` is in the set, so: - A and B **intersect** if and only if the **origin** lies inside `A - B`. - The distance between A and B equals the distance from the origin to `A - B`. - If A and B are convex, `A - B` is convex too. That turns a two-shape question into a one-shape, one-point question. The trick of **GJK** (named after Gilbert, Johnson and Keerthi) is that it never constructs `A - B`, which for two hulls would have up to one vertex per pair of vertices. ## Support functions A **support function** `support(d)` returns the point of a shape that lies farthest along direction `d`, the point maximising `dot(p, d)`. For the difference, the farthest point along `d` is A's farthest point along `d` minus B's farthest point along `-d`: `supportAB(d) = supportA(d) - supportB(-d)` | Shape | Support point along `d` | |---|---| | Sphere | `c + r * d / length(d)` | | Oriented box | `c + sum over i of sign(dot(d, u_i)) * e_i * u_i` | | Capsule | the segment endpoint with larger `dot(p, d)`, plus `r * d / length(d)` | | Convex hull | the vertex with the largest `dot(v, d)` (linear scan or hill climbing) | A shape stored in local space answers in world space by rotating the direction in and the point out: `R * supportLocal(transpose(R) * d) + t`. Because the algorithm only ever calls support functions, one GJK routine serves every pair of convex shapes. ## The loop 1. Pick any initial direction `d`, for example from one centre toward the other, and put `supportAB(d)` in the simplex. 2. Set `d` to point from that point toward the origin. 3. Fetch `w = supportAB(d)`. 4. If `dot(w, d) < 0`, the whole of `A - B` lies on the far side of a plane through `w` with normal `d`, and the origin is not reached. **Return separated.** 5. Add `w` to the simplex. 6. Solve the simplex: find the sub-feature closest to the origin, drop the vertices not on it, and set `d` from that feature toward the origin. 7. If the simplex is a tetrahedron containing the origin, **return intersecting**; otherwise go to step 3. ## The simplex cases The **simplex** is the current set of one to four support points. Call the newest point `A`; it was just found beyond the origin along `d`, which rules out several regions. - **Point**: one point; `d` is simply toward the origin. - **Segment** `[A, B]`: if `dot(B - A, -A) > 0`, the origin lies in the region beside the segment; keep both and set `d = cross(cross(AB, AO), AB)`, the perpendicular from the segment toward the origin. Otherwise keep only `A` and set `d = -A`. - **Triangle** `[A, B, C]`: test the regions outside edges `AB` and `AC` and above or below the triangle's plane; reduce to a segment, or keep the triangle with `d` along its normal on the origin's side. - **Tetrahedron**: test the three faces that contain `A`; if the origin is outside one, reduce to that triangle; if it is inside all of them, the origin is enclosed. The regions on B's and C's far side never need testing, because the origin was already shown to lie toward `A`. ## What GJK gives and what it does not - The **boolean** form above answers overlap, usually in a few iterations, and very quickly when the last frame's direction or simplex seeds the search. - The **distance** form keeps iterating while separated and returns the closest points on A and B, by storing each simplex vertex's two support points and applying the closest point's barycentric weights to them. - Neither form returns a **penetration depth** for overlapping shapes: knowing the origin is inside says nothing about how far it is from the boundary. That needs EPA or a margin scheme. ## Pitfalls - **Termination tolerance**: on curved shapes the distance form converges gradually; stop when the support point no longer improves the distance by a relative tolerance. - **Iteration cap**: rounding can make the simplex cycle; cap the loop and treat reaching the cap as touching, a conservative choice. - **Degenerate simplices**: nearly collinear or coplanar points produce tiny normals; checking for them avoids dividing by near zero. - **Flat-faced shapes**: many vertices tie for the support point; any of them is valid, but deterministic tie-breaking keeps results reproducible.

  • Why does GJK not give a penetration depth when the shapes overlap?
    The boolean loop stops as soon as a tetrahedron encloses the origin, which proves overlap but says nothing about the distance from the origin to the boundary of the Minkowski difference. Depth needs a second step: EPA expands that tetrahedron toward the boundary, or margin schemes keep shapes slightly apart so the distance form applies.
  • How does the distance version of GJK produce the two closest points?
    Store, with each simplex vertex, the support points from A and from B that produced it. When the closest point of the simplex to the origin stops improving, express it in barycentric weights of the simplex vertices and apply the same weights to the stored A points and B points. The results are the closest points on each shape.
  • How does GJK use frame-to-frame coherence?
    Bodies move little between steps, so seeding the search with last frame's separating direction or final simplex usually confirms the same answer in one or two iterations. Caching that per pair makes resting and slowly moving pairs very cheap.

  • GJK explicitly builds the Minkowski difference of the two shapes.
  • GJK works directly on concave meshes.
  • A 3D GJK simplex can grow beyond four points.
  • The support function returns the shape's point nearest the other shape.
  • When GJK reports overlap it also knows the penetration depth.