Use omath::collision::Simplex for GJK/EPA algorithms
mainomath::collision::Simplex is a lightweight, fixed-capacity container (maximum 4 points) designed for use in GJK (Gilbert-Johnson-Keerthi) and EPA (Expanding Polytope Algorithm) collision detection. It stores geometric vertices that form a simplex (point, line, triangle, or tetrahedron) to help determine if the origin is contained within a shape's Minkowski difference.
Key Characteristics
- Fixed Capacity: Always holds a maximum of 4 points.
- No Heap Allocation: All storage is inline, making it highly performant.
- constexpr Support: Most methods are
constexpr, allowing for compile-time usage. - Vertex Requirements: The vertex type must satisfy the
GjkVectorconcept (supporting negation, subtraction, cross products, andpoint_to_same_direction).omath::Vector3<float>is the default.
Basic Workflow in GJK
- Initialize: Create an empty
Simplex. - Add Points: Use
push_front(new_support_point)to add the latest support point at index 0. - Test Origin: Call
contains_origin().- If
true: A collision is detected. - If
false: The simplex is automatically simplified (points are removed) to stay close to the origin.
- If
- Update Direction: If
contains_origin()wasfalse, callnext_direction()to get the new search vector for the next iteration.
#include <omath/collision/simplex.hpp>
using namespace omath::collision;
// Initialize an empty simplex
Simplex<Vector3<float>> simplex;
// Add a point (newest point becomes index 0)
simplex.push_front(Vector3<float>{1.0f, 0.0f, 0.0f});
// Check for collision and simplify
if (simplex.contains_origin()) {
// Collision detected!
} else {
// Get direction for next support point query
Vector3<float> dir = simplex.next_direction();
}