kroitor/gjk.c logo

kroitor/gjk.c

Free

Gilbert-Johnson-Keerthi (GJK) collision detection algorithm in 200 lines of clean plain C

FreeFree tier
Type
Open Source

About kroitor/gjk.c

Gjk.c is a lightweight, dependency-free implementation of the Gilbert-Johnson-Keerthi (GJK) collision detection algorithm in plain C. Written in under 200 lines, it determines whether two arbitrary convex polygons (2D) are intersecting using Minkowski sums and simplex construction. The code is designed for educational clarity and easy integration into game engines, robotics simulations, or any spatial analysis tool. A 3D version and support for collision distance/contact points are planned.

Key Features

2D collision detection for arbitrary convex polygons using Minkowski sums
Roughly 200 lines of plain C with no external dependencies
Triangle-simplex construction in Minkowski space for intersection testing
Returns boolean collision result (currently no distance or contact points)
Simple API requiring only vertex arrays and count
Free for any usage (public domain, no license)

Pros & Cons

Pros
  • Extremely lightweight and self-contained (single C file, no dependencies)
  • Fast and efficient for real-time applications
  • Easy to integrate into existing C/C++ projects
  • Good for learning GJK algorithm with clear code and Minkowski sum explanation
  • Public domain – no licensing restrictions
Cons
  • Currently limited to 2D (3D version not yet available)
  • Only returns boolean collision status (no penetration depth or contact points yet)
  • Requires convex polygons; does not handle concave shapes directly
  • No distance calculation or collision response information

Best For

Game development – 2D collision detection between convex shapesRobotics navigation – checking spatial overlaps of simplified geometriesSimulation software – detecting intersections of convex hullsEducational tool – learning GJK algorithm through readable C implementation

FAQ

What is GJK collision detection?
GJK (Gilbert-Johnson-Keerthi) is an algorithm that determines whether two convex shapes intersect by checking if their Minkowski difference contains the origin. It builds a simplex (triangle in 2D, tetrahedron in 3D) in the Minkowski space to test for containment.
Does this library handle 3D shapes?
Not yet. The current version is 2D only. The repository states that a full 3D version is upcoming and will build a tetrahedron-simplex inside a 3-dimensional Minkowski space.
Does it compute collision distance or contact points?
No. Currently it only returns a boolean indicating whether a collision exists. Support for distance and contact points is planned for future updates.
What input format does gjk.c expect?
It expects arrays of 2D vectors (with x and y floats) representing the vertices of two convex polygons, along with their respective counts. The order of vertices does not matter as long as the polygon is convex.
Is the code free to use?
Yes. The author explicitly states it is 'absolutely free for any usage' and places it in the public domain. No license is required.