The Gilbert–Johnson–Keerthi distance algorithm, commonly referred to as the GJK algorithm, is a foundational method in computational geometry used to calculate the distance between two convex shapes. Unlike brute-force approaches that check every point, GJK operates efficiently by leveraging the geometric properties of Minkowski Difference to determine if two objects intersect and, if not, how far apart they are. Its robustness and speed make it a standard solution in physics engines, video games, and robotics, where real-time collision detection is critical.
Core Principles and Minkowski Difference
At the heart of GJK is the concept of Minkowski Difference, a mathematical operation that combines two shapes by subtracting every point of one from every point of the other. For two shapes A and B, the resulting shape contains the origin if and only if A and B overlap. Instead of computing the entire Minkowski Difference, which is computationally expensive, GJK incrementally constructs a simplex—a geometric structure of points—within this difference space. The algorithm checks whether the origin lies inside this simplex, using this check to iteratively refine its search direction toward the closest features of the shapes.
The Iterative Search Process
GJK begins with a support function, which finds the farthest point in a given direction for each shape. By querying the support function in an initial direction, the algorithm establishes the first point of the simplex. Subsequent iterations add new points to the simplex by sampling the support function in a direction that aims to push the simplex closer to the origin. This process continues, building a chain of points that approximate the boundary of the Minkowski Difference. The algorithm’s elegance lies in its ability to converge quickly, often in just a few iterations, even for complex shapes.
Simplex Construction and Termination
The simplex evolves through different configurations—a point, a line segment, a triangle, or a tetrahedron—depending on the number of points added. At each step, GJK calculates the closest point on the simplex to the origin and uses this to determine the next search direction. If the origin is found to be inside the simplex, the shapes are intersecting, and the algorithm terminates successfully. If the search direction fails to yield a new point that moves the simplex closer to the origin, the algorithm concludes that the shapes are separate and computes the minimum distance from the origin to the simplex, providing the exact gap between the objects.
Performance and Practical Considerations
One of GJK’s primary advantages is its computational efficiency, typically operating in constant time for a fixed number of iterations. This makes it ideal for real-time applications where performance is paramount. However, its reliance on convex shapes means it cannot directly handle concave objects. In practice, complex models are often decomposed into convex parts, or GJK is combined with other algorithms like EPA (Expanding Polytope Algorithm) to compute contact points and penetration depth after an intersection is detected. Tuning the support function and managing floating-point precision are also crucial for reliable implementation in production environments.
Integration in Modern Systems
GJK is a cornerstone of many physics engines, including Bullet and Box2D, where it forms the backbone of collision detection pipelines. Its ability to quickly answer the "are they touching?" question allows engines to narrow down potential collisions and apply more expensive calculations only when necessary. Beyond gaming, GJK is used in robotics for path planning and proximity checking, ensuring machines can navigate environments without collisions. The algorithm’s adaptability to various geometric representations, from spheres to complex meshes, underscores its enduring relevance in software development.
Limitations and Complementary Techniques
Despite its strengths, GJK is not without limitations. It requires convex inputs, struggles with degenerate cases involving flat simplexes, and does not inherently provide contact points or normals without additional logic. These gaps are often filled by algorithms like EPA, which refine the intersection data after GJK signals a collision. Modern implementations sometimes use conservative advancement or interval arithmetic to handle edge cases more gracefully. Understanding these constraints allows developers to combine GJK with other tools, creating robust systems that leverage the best of multiple approaches.