Inferensys

Glossary

GJK Algorithm (Gilbert-Johnson-Keerthi)

The GJK algorithm is a fast, iterative method for computing the distance between two convex shapes and detecting collisions by examining the Minkowski difference between them.
Stylish WeWork-like workspace with hot desks and document wall, professional searching through enterprise knowledge base on a mounted ultrawide display, warm industrial pendants overhead.
CONTACT AND RIGID BODY DYNAMICS

What is GJK Algorithm (Gilbert-Johnson-Keerthi)?

The GJK algorithm is a fast, iterative method for computing the distance between two convex shapes and detecting collision by examining the Minkowski difference between them.

The GJK algorithm is a collision detection and distance calculation method for convex shapes. It operates by iteratively constructing a simplex (a point, line segment, triangle, or tetrahedron) within the Minkowski difference of the two shapes. The algorithm's core insight is that the origin is contained within this Minkowski difference if and only if the shapes intersect. For non-intersecting shapes, the algorithm converges to the point on the Minkowski difference closest to the origin, yielding the separation distance and direction.

GJK is exceptionally efficient due to its support function mechanism, which queries the farthest point in a given direction for each shape. This iterative search avoids explicit computation of the full Minkowski difference. In physics engines for robotics simulation, GJK is the standard narrow-phase algorithm, often paired with the EPA algorithm to calculate penetration depth when a collision is detected. Its speed and numerical stability make it foundational for real-time rigid body dynamics.

COMPUTATIONAL GEOMETRY

Key Features of the GJK Algorithm

The Gilbert-Johnson-Keerthi (GJK) algorithm is a cornerstone of real-time physics simulation, providing a fast, iterative method for collision detection and distance calculation between convex shapes. Its efficiency stems from several core computational features.

01

Minkowski Difference as the Core Abstraction

The GJK algorithm does not operate directly on the two shapes. Instead, it transforms the problem by computing the Minkowski difference between them. For shapes A and B, this is defined as A ⊖ B = {a - b | a ∈ A, b ∈ B}. The critical insight is that the two original shapes intersect if and only if the Minkowski difference contains the origin. Furthermore, the distance between the shapes is the distance from the origin to this new set. This abstraction reduces collision and distance queries to a single problem: finding the closest point on a convex set to the origin.

02

The Support Function for Convex Shapes

GJK never explicitly constructs the full Minkowski difference, which would be computationally prohibitive. Instead, it queries the shape via a support function. For a given direction vector d, the support function for a convex shape returns the point on that shape's boundary that is farthest in the direction of d. For the Minkowski difference, the support point is easily computed as: s_A⊖B(d) = s_A(d) - s_B(-d), where s_A and s_B are the support functions for the individual shapes. This allows GJK to sample the Minkowski difference on-demand using only the geometry of the original objects.

03

Iterative Simplex Construction and Refinement

GJK is an iterative method that builds a sequence of increasingly accurate approximations, called simplices, inside the Minkowski difference. A simplex is the convex hull of a small set of points (e.g., a point, line segment, triangle, or tetrahedron).

  • Initialization: The algorithm starts with an arbitrary direction and adds a support point to form the initial simplex.
  • Iteration: Each iteration uses the current simplex to find the closest point on it to the origin. The direction from this closest point toward the origin is used to query a new support point.
  • Termination: If the new support point is not farther in this direction than the current closest point, the algorithm has converged. If the simplex contains the origin, a collision is detected.
04

Johnson's Distance Subalgorithm

A critical sub-step within each GJK iteration is finding the point on the current simplex closest to the origin. This is efficiently solved using Johnson's distance algorithm. It works by representing the closest point as a convex combination of the simplex vertices and using a series of geometric tests (e.g., Voronoi region checks) to determine which sub-simplex (a face, edge, or vertex) contains the closest point. This subalgorithm is what enables GJK to quickly refine its search direction each iteration without solving a general quadratic program.

05

Fast Convergence and Termination Criteria

GJK is prized for its rapid convergence, typically requiring only a few iterations (often 2-10) for standard convex shapes. This speed is due to:

  • Geometric progression: Each iteration moves the simplex closer to the origin.
  • Robust termination: The algorithm terminates with a collision if the simplex contains the origin. It terminates with a minimum distance when a new support point fails to advance the simplex closer to the origin than the current closest point, within a numerical tolerance.
  • Caching: Support points and directions can be cached between frames for objects with small relative motion, a technique known as warm starting, which drastically reduces iteration count.
06

Pairing with EPA for Penetration Depth

While GJK excels at detecting collision and finding distance, it only provides a yes/no answer and a separation vector for non-colliding objects. When shapes are interpenetrating, the Expanding Polytope Algorithm (EPA) is used as a post-processor. EPA takes the final simplex from GJK that contains the origin and expands it outward by iteratively adding support points until it reconstructs the full Minkowski difference near the origin. This allows EPA to calculate the penetration depth—the minimum translation vector required to separate the objects—and the corresponding contact normal. The GJK-EPA pipeline is standard in physics engines for generating full contact manifolds.

CONTACT AND RIGID BODY DYNAMICS

How the GJK Algorithm Works

The Gilbert-Johnson-Keerthi (GJK) algorithm is a fast, iterative method for computing the distance between two convex shapes and detecting collisions by examining the Minkowski difference between them.

The algorithm operates by iteratively constructing a simplex—a triangle in 2D or a tetrahedron in 3D—within the Minkowski difference of the two shapes. This difference represents the set of all vectors from one shape to the other. The core insight is that if this Minkowski difference contains the origin, the original shapes are intersecting. GJK uses support functions to find the farthest point in a given direction for each shape, which are subtracted to build the simplex efficiently.

The algorithm's power lies in its use of Johnson's distance subalgorithm to determine if the origin is inside the current simplex and to iteratively refine it. If the origin is outside, the simplex is updated towards the origin using a new support point, and the process repeats. For non-intersecting shapes, GJK converges to a separation vector representing the minimum translation distance. For penetration, it is often paired with the Expanding Polytope Algorithm (EPA) to compute penetration depth.

NARROW-PHASE COMPARISON

GJK vs. Other Collision Detection Methods

A feature comparison of the GJK algorithm against other common narrow-phase collision detection techniques used in physics simulation and robotics.

Feature / MetricGJK AlgorithmSeparating Axis Theorem (SAT)Bounding Volume IntersectionVoxel/Pixel-Based

Primary Purpose

Distance calculation & collision detection for convex shapes

Collision detection & contact manifold generation for convex polytopes

Fast overlap test for simple volumes (AABB, Sphere, OBB)

Collision detection for arbitrary, non-convex, or deformable shapes

Shape Support

Convex shapes only

Convex polytopes only

Any shape (via bounding volume approximation)

Any shape (discretized)

Output Data

Minimum distance & closest points; penetration via EPA

Boolean collision, separation vector, contact manifold (faces/edges)

Boolean overlap result

Boolean collision; approximate contact surface

Performance Complexity (Typical)

O(n) iterative, sub-linear convergence

O(n²) in worst case for polytopes

O(1) for pairwise tests

O(n³) for volume resolution, memory intensive

Penetration Depth Resolution

Requires separate EPA algorithm

Built-in via MTV (Minimum Translation Vector)

None

Approximate, resolution-dependent

Continuous Collision Detection (CCD) Support

Yes (via raycast on Minkowski difference)

Limited, computationally expensive

No (discrete only)

Yes (via swept volume testing)

Memory Overhead

Low (stores simplex of 4 points max)

Moderate (stores face/edge projections)

Very Low (stores volume parameters only)

Very High (stores full 3D grid)

Implementation Complexity

High (requires careful numerical robustness)

Moderate to High (handling edge/face cases)

Very Low

Low (conceptually), but high for optimization

Best Used For

General convex shapes in motion, distance queries

Static convex polyhedra, need full contact data

Broad-phase culling, simple shapes, hierarchical tests

Complex non-convex shapes, medical imaging, voxel worlds

GJK ALGORITHM

Common Applications and Examples

The Gilbert-Johnson-Keerthi (GJK) algorithm is a cornerstone of real-time physics simulation, prized for its speed and iterative nature. Its primary function is to compute the distance between two convex shapes and detect collisions by examining their Minkowski difference.

02

Robotic Motion Planning & Manipulation

In robotics, GJK is critical for collision avoidance and path planning. It enables:

  • Configuration Space (C-Space) checking: Determining if a robot's proposed pose intersects with obstacles.
  • Grasp planning: Verifying that a robotic gripper's geometry does not collide with non-target objects during manipulation.
  • Safe trajectory validation: Ensuring every incremental motion between waypoints is collision-free. Its efficiency allows for real-time re-planning in dynamic environments.
03

Proximity Queries for Convex Hulls

Beyond simple collision yes/no detection, GJK's core output is the minimum distance between two convex sets. This is essential for:

  • Separation distance monitoring in safety systems.
  • Computing the penetration vector when used with the Expanding Polytope Algorithm (EPA).
  • Support function optimization: The algorithm iteratively calls a support function for each shape, which finds the farthest point in a given direction. This makes it highly efficient for complex convex shapes represented by their hulls.
04

The Minkowski Difference & Simplex

GJK operates on a powerful geometric concept: the Minkowski difference between shapes A and B (A ⊖ B).

  • Key Insight: The distance between A and B is the distance from the origin to this Minkowski difference. If the origin is inside A ⊖ B, the shapes intersect.
  • Simplex Evolution: The algorithm constructs a simplex (a point, line segment, triangle, or tetrahedron) inside A ⊖ B that iteratively moves closer to the origin.
  • Termination: It converges when the simplex contains the origin (collision) or when the distance to the origin is minimized (closest points found).
05

Comparison to Other Methods

GJK excels where other methods falter:

  • Vs. SAT (Separating Axis Theorem): GJK is generally faster for complex convex polyhedra, as SAT performance depends on the number of face/edge axes to test.
  • Vs. Spatial Partitioning Alone: While grids or trees excel at broad-phase culling, GJK provides the exact narrow-phase result for convex pairs.
  • Limitation: It is designed for convex shapes. For concave objects, they must first be decomposed into convex parts (convex decomposition), and GJK is run on each part pair.
06

Integration with Constraint Solvers

A collision detection algorithm like GJK is only the first step in a physics pipeline. Its outputs feed the constraint solver:

  • Output Data: For colliding pairs, GJK (with EPA) provides the penetration depth and contact normal.
  • Solver Input: This data formulates non-penetration constraints and, with Coulomb friction models, friction constraints.
  • Resolution: The solver (e.g., solving an LCP) computes the impulses or forces to resolve the collision, applying Newton-Euler equations to update body velocities and positions.
GJK ALGORITHM

Frequently Asked Questions

The Gilbert-Johnson-Keerthi (GJK) algorithm is a cornerstone of real-time physics simulation, providing a fast, iterative method for collision detection and distance calculation between convex shapes. These questions address its core mechanics, applications, and relationship to other critical algorithms in robotics and simulation.

The GJK algorithm is an iterative method for computing the minimum distance between two convex sets and detecting collision by examining the Minkowski difference between them. It works by constructing a simplex (a point, line segment, triangle, or tetrahedron) within the Minkowski difference and iteratively refining this simplex towards the origin. If the origin is contained within the Minkowski difference, the shapes are colliding; otherwise, the algorithm returns the minimum distance and the closest points. Its efficiency stems from using support functions, which return the farthest point in a given direction for a shape, allowing it to operate without pre-computing or storing the full Minkowski difference geometry.

Key Steps:

  1. Initialize: Start with an arbitrary search direction.
  2. Support Mapping: Calculate the support point for each shape in the current direction and its opposite.
  3. Minkowski Difference: Subtract the support points to get a vertex of the Minkowski difference.
  4. Simplex Update: Add the new vertex to the current simplex (a subset of points).
  5. Check for Origin: Determine if the simplex contains the origin (collision) or find the new closest simplex to the origin.
  6. New Direction: Calculate a new search direction from the simplex towards the origin.
  7. Iterate: Repeat from step 2 until the distance converges or the origin is included.
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.