Inferensys

Glossary

Gilbert–Johnson–Keerthi (GJK) Algorithm

The Gilbert–Johnson–Keerthi (GJK) algorithm is an iterative method for computing the distance between two convex shapes and detecting collisions, central to modern physics engines.
Developer reviewing semantic search engine results on laptop, relevance scores visible, technical search demo.
PHYSICS SIMULATION ENGINES

What is the Gilbert–Johnson–Keerthi (GJK) Algorithm?

A core algorithm for efficient collision detection and distance computation in physics engines.

The Gilbert–Johnson–Keerthi (GJK) algorithm is an iterative method for computing the minimum distance between two convex shapes and determining if they intersect. It operates by constructing a Minkowski difference of the shapes and iteratively refining a simplex (a point, line segment, or triangle) within this difference to find the point closest to the origin, which corresponds to the separation distance. Its efficiency stems from its support function, which queries the farthest point of a shape in a given direction.

GJK is foundational to modern physics engines and rigid body dynamics simulations because it provides a fast, robust, and dimensionally general solution for collision detection. It is often paired with the Expanding Polytope Algorithm (EPA) to calculate penetration depth and contact manifolds when shapes overlap. This makes GJK critical for simulating precise physical interactions in robotics training, video games, and digital twin environments.

COMPUTATIONAL GEOMETRY

Key Features of the GJK Algorithm

The Gilbert–Johnson–Keerthi (GJK) algorithm is an iterative method for computing the distance between two convex shapes and detecting collisions. Its efficiency stems from several core computational features.

01

Support Function Abstraction

The GJK algorithm operates on shapes not through their explicit geometry, but via a support function. This function, given a direction vector d, returns the point on the shape that is farthest in that direction. This abstraction allows GJK to work with any convex shape (polyhedra, spheres, cones) as long as a support function is defined, making it highly versatile for complex collision geometries in physics engines.

02

Minkowski Difference & Origin Inclusion

GJK reduces the problem of finding the distance between two convex sets, A and B, to finding the distance between the origin and their Minkowski difference (A - B). A collision occurs if and only if the origin is inside this Minkowski difference set. The algorithm iteratively builds a simplex (a triangle in 3D, a line in 2D) inside this set, trying to enclose the origin.

03

Iterative Simplex Evolution

The algorithm maintains a simplex—the simplest convex hull of a set of points from the Minkowski difference. Starting with a single point, it iteratively:

  • Uses the support function to find a new point in a direction toward the origin.
  • Updates the simplex to be the smallest subset that best approximates the origin's vicinity.
  • Terminates when the simplex contains the origin (collision) or when the distance to the origin cannot be further reduced (minimum separation found).
04

Johnson's Distance Subalgorithm

A critical subroutine determines the point on the current simplex closest to the origin and identifies which subset of the simplex's vertices is relevant. For a 3D simplex (a tetrahedron), this involves checking which faces, edges, or vertices are closest to the origin. This efficiently reduces the simplex's dimensionality at each step, guiding the next support function query.

05

Termination & Convergence

GJK terminates under two conditions:

  • Collision Detected: The origin is found to be inside the current simplex.
  • Minimum Distance Found: The algorithm converges when the new point found by the support function does not move closer to the origin than the closest point on the current simplex. The distance to this closest point is the separation distance. The algorithm typically converges in a very small number of iterations, often <10, even for complex shapes.
06

Expanding Polytope Algorithm (EPA) for Penetration Depth

When GJK detects a collision (origin inside the Minkowski difference), the Expanding Polytope Algorithm (EPA) is often used as a post-processing step. While GJK finds that shapes intersect, EPA expands the final simplex from GJK into a full polytope surrounding the origin to calculate the penetration depth and the associated contact normal, which are necessary for collision response in physics engines.

FEATURE COMPARISON

GJK vs. Other Collision Detection Methods

A technical comparison of the Gilbert–Johnson–Keerthi (GJK) algorithm against other common collision detection and distance calculation methods used in physics simulation engines.

Feature / MetricGilbert–Johnson–Keerthi (GJK)Separating Axis Theorem (SAT)Bounding Volume Hierarchy (BVH) TraversalVoxel / Grid-Based

Primary Function

Distance calculation & collision detection for convex shapes

Collision detection for convex polygons/polyhedra

Broadphase culling & hierarchical collision queries

Spatial partitioning for many simple shapes

Shape Support

Convex shapes only (via support function)

Convex polygons & polyhedra

Any shape (via bounding volume)

Primitive shapes (spheres, boxes, voxels)

Output Data

Minimum distance & closest points

Boolean collision & separation vector

List of potentially colliding pairs

List of objects per spatial cell

Algorithmic Complexity

O(n) iterative, converges quickly

O(n²) in axes to test

O(n log n) for construction, O(log n) for query

O(1) insertion, O(k) query for k neighbors

Continuous Collision Detection (CCD) Support

Requires Explicit Geometry

Best For

Robotic motion planning, precise distance queries

Real-time game physics, simple convex shapes

Large scenes with static/kinematic objects

Particle systems, uniform density environments

Memory Overhead

Low (stores simplex state)

Medium (stores all face/edge data)

High (tree structure storage)

Medium (grid data structure)

GJK ALGORITHM

Frequently Asked Questions

The Gilbert–Johnson–Keerthi (GJK) algorithm is a cornerstone of modern collision detection in physics simulation engines. These questions address its core mechanics, applications, and relationship to other simulation technologies.

The Gilbert–Johnson–Keerthi (GJK) algorithm is an iterative method for computing the minimum distance between two convex shapes and detecting whether they intersect (collide). It works by leveraging the concept of the Minkowski difference. The algorithm constructs a simplex (a point, line segment, or triangle in 3D) within this difference space and iteratively refines it toward the origin. If the origin is contained within the Minkowski difference, the shapes are intersecting. Its power lies in its linear convergence and its reliance solely on a support function—a subroutine that returns the farthest point on a shape in a given direction—making it applicable to any convex geometry.

Key Steps in the GJK Loop:

  1. Initialization: Choose an arbitrary search direction.
  2. Support Mapping: Calculate a support point for each shape in the current direction and its opposite, then compute their difference (a point in Minkowski space).
  3. Simplex Update: Add the new support point to a working simplex (up to 4 points in 3D).
  4. Origin Check: Determine if the simplex contains the origin. If yes, collision detected.
  5. Direction Update: If the origin is not contained, compute a new search direction toward the origin from the current simplex and discard any simplex points that don't help the search.
  6. Iteration: Repeat steps 2-5 until the origin is found (collision) or the distance to the origin stops decreasing (minimum separation found).
Prasad Kumkar

About the author

Prasad Kumkar

CEO & MD, Inference Systems

Prasad Kumkar is the CEO & MD of Inference Systems and writes about AI systems architecture, LLM infrastructure, model serving, evaluation, and production deployment. Over 5+ years, he has worked across computer vision models, L5 autonomous vehicle systems, and LLM research, with a focus on taking complex AI ideas into real-world engineering systems.

His work and writing cover AI systems, large language models, AI agents, multimodal systems, autonomous systems, inference optimization, RAG, evaluation, and production AI engineering.