Short answer: The Gilbert-Johnson-Keerthi algorithm decides whether two convex sets intersect by chasing a simplex inside their Minkowski difference. The difference contains the origin exactly when the shapes touch or overlap. Each iteration asks a support function for the farthest point in a chosen direction, adds that point to a simplex of at most three vertices in the plane, and updates the direction toward the origin. If the new support does not pass the origin, the shapes are disjoint. If the simplex encloses the origin, they intersect. A typical query finishes in a handful of support calls.
What the problem actually asks
Intersecting two convex polygons by clipping is correct and depends on the number of vertices. Many game and robotics shapes are convex hulls, capsules, or ellipses that can answer a support query, the farthest point in a direction, without listing a mesh. GJK uses only that query on the Minkowski difference, and the support of a difference is the difference of supports. The algorithm never builds the difference explicitly.
The simplex in the plane is a point, a segment, or a triangle. The update reduces the simplex to the subset that is closest to the origin and searches from there. A triangle that contains the origin is a positive answer. A support point that cannot move the simplex past the origin is a negative answer.
What the algorithm guarantees
On convex shapes with exact support functions, the algorithm terminates with a correct intersect-or-miss answer. The number of iterations is small in practice and depends on the shapes. It is not a linear bound in the number of polygon vertices, because the support function hides that cost. If the support itself scans a polygon, each call is linear and the total is a small constant times that scan. Extra memory is the current simplex.
GJK returns a yes or no, and with extra work a separating direction when the answer is no. The penetration depth when the answer is yes is the expanding polytope algorithm, EPA, which starts from the simplex GJK already built. GJK alone does not return a penetration vector.
The working steps
- Start with an arbitrary direction, often the difference of the two shape centers. Compute support A minus support B in that direction and store it as the simplex. The next direction points from that point toward the origin.
- Compute a new support in the current direction. If its dot product with the direction is negative, the origin is not in the Minkowski difference. Return disjoint.
- Add the support to the simplex. If the simplex contains the origin, return intersecting.
- Otherwise reduce the simplex to the edge or vertex closest to the origin and set the next direction toward the origin from that feature.
- Repeat. In the plane the simplex never needs a fourth vertex. A triangle that does not contain the origin drops one vertex.
The test is for a pair of convex sets. A concave mesh has to be decomposed into convex pieces first, or the support function is wrong and a miss can be reported as a hit or the reverse. EPA is the follow-up for penetration. A separating-axis test is the alternative for polygons and polyhedra that want an explicit list of candidate axes, and it is often simpler for those shapes.
Approaches that look simpler and fail
| Approach | Why it fails when the support is evaluated on a concave mesh |
|---|---|
| Take the farthest vertex of a concave mesh and call it a support | The farthest vertex is a support of the convex hull, not of the mesh. The test then answers the hulls, which may overlap when the meshes do not. |
| Build the full Minkowski difference as a polygon | The result can be right and the construction is quadratic. GJK exists to avoid materializing that polygon. |
| Return the last simplex as a penetration depth | The simplex proves overlap and is not the minimum translation. EPA expands it. |
| Add a fourth point in the planar simplex | A planar simplex that still needs a search is a segment or a triangle. A fourth point means the reduction step failed to drop a vertex. |
Penetration depth after a hit is EPA. An explicit axis list for boxes and polygons is the separating-axis theorem. GJK is the support-function intersection test for convex shapes.
A working example
This Python fragment tests two convex polygons by walking a simplex inside their Minkowski difference. The support of the difference is the difference of the supports. These two squares overlap, so the simplex reaches the origin. A square shifted to x = 5 does not. It prints overlap separate.
def support(poly, direction):
return max(poly, key=lambda p: p[0] * direction[0] + p[1] * direction[1])
def sub(u, v):
return (u[0] - v[0], u[1] - v[1])
def gjk(a, b):
direction = [1.0, 0.0]
simplex = [sub(support(a, direction), support(b, [-direction[0], -direction[1]]))]
direction = [-simplex[0][0], -simplex[0][1]]
while True:
point = sub(support(a, direction), support(b, [-direction[0], -direction[1]]))
if point[0] * direction[0] + point[1] * direction[1] < 0:
return False
simplex.append(point)
if contains_origin(simplex, direction):
return True
def contains_origin(simplex, direction):
a = simplex[-1]
ao = [-a[0], -a[1]]
if len(simplex) == 2:
b = simplex[0]
ab = [b[0] - a[0], b[1] - a[1]]
if ab[0] * ao[0] + ab[1] * ao[1] > 0:
perp = [-ab[1], ab[0]]
if perp[0] * ao[0] + perp[1] * ao[1] < 0:
perp = [-perp[0], -perp[1]]
direction[0], direction[1] = perp
else:
simplex.pop(0)
direction[0], direction[1] = ao
return False
# triangle
b, c = simplex[-2], simplex[-3]
ab = [b[0] - a[0], b[1] - a[1]]
ac = [c[0] - a[0], c[1] - a[1]]
ab_perp = [-ab[1], ab[0]]
if ab_perp[0] * ac[0] + ab_perp[1] * ac[1] > 0:
ab_perp = [-ab_perp[0], -ab_perp[1]]
if ab_perp[0] * ao[0] + ab_perp[1] * ao[1] > 0:
simplex.pop(-3)
direction[0], direction[1] = ab_perp
return False
ac_perp = [-ac[1], ac[0]]
if ac_perp[0] * ab[0] + ac_perp[1] * ab[1] > 0:
ac_perp = [-ac_perp[0], -ac_perp[1]]
if ac_perp[0] * ao[0] + ac_perp[1] * ao[1] > 0:
simplex.pop(-2)
direction[0], direction[1] = ac_perp
return False
return True
square = [(0, 0), (1, 0), (1, 1), (0, 1)]
shifted = [(5, 0), (6, 0), (6, 1), (5, 1)]
assert gjk(square, square) and not gjk(square, shifted)
print("overlap", "separate")
Where it is used
The Bullet physics library and PhysX test convex overlap with GJK: the support function walks the Minkowski difference toward the origin. A game collision query between two convex hulls is that test. Concave meshes are decomposed into convex pieces first.
When you can drop this algorithm
Drop GJK for two axis-aligned boxes. An interval test is enough. Keep it when the shapes are convex and a support function is the natural query, and run EPA only if a penetration vector is required.
Why this stays published
We publish this so a search for GJK returns the Minkowski support iteration and the fact that a containing simplex means intersection. GJK decides whether two convex shapes intersect by building a simplex in their Minkowski difference until the simplex contains the origin or a support point proves it cannot.
本文由 HoHo 與 AI 協作整理,最後更新於 2026 年 11 月 13 日。
