Implements GJK collision detection

Adds GJK algorithm implementation for detecting collisions between mesh colliders.

Includes mesh collider definition and unit tests for basic collision detection.

Provides a foundation for more complex collision handling and physics interactions.
This commit is contained in:
2025-11-09 14:04:01 +03:00
parent 10ebf6ed04
commit 338246a618
6 changed files with 315 additions and 0 deletions

View File

@@ -0,0 +1,47 @@
//
// Created by Vlad on 11/9/2025.
//
#pragma once
#include "mesh_collider.hpp"
#include "omath/linear_algebra/vector3.hpp"
#include "simplex.hpp"
namespace omath::collision
{
class GjkAlgorithm final
{
public:
[[nodiscard]]
static Vector3<float> find_support_vertex(const MeshCollider& collider_a, const MeshCollider& collider_b,
const Vector3<float>& direction)
{
return collider_a.find_abs_furthest_vertex(direction) - collider_b.find_abs_furthest_vertex(-direction);
}
[[nodiscard]]
static bool check_collision(const MeshCollider& collider_a, const MeshCollider& collider_b)
{
// Get initial support point in any direction
auto support = find_support_vertex(collider_a, collider_b, {1, 0, 0});
Simplex points;
points.push_front(support);
auto direction = -support;
while (true)
{
support = find_support_vertex(collider_a, collider_b, direction);
if (support.dot(direction) <= 0.f)
return false;
points.push_front(support);
if (handle_simplex(points, direction))
return true;
}
}
};
}// namespace omath::collision