Files
omath/include/omath/collision/gjk_algorithm.hpp
Orange 66919af46a Refactors GJK algorithm vertex type
Simplifies the GJK algorithm by using a type alias for the vertex type, improving code readability and reducing redundancy. Removes unnecessary includes.
2025-11-11 23:37:56 +03:00

48 lines
1.3 KiB
C++

//
// Created by Vlad on 11/9/2025.
//
#pragma once
#include "simplex.hpp"
namespace omath::collision
{
template<class ColliderType>
class GjkAlgorithm final
{
using VertexType = typename ColliderType::VertexType;
public:
[[nodiscard]]
static VertexType find_support_vertex(const ColliderType& collider_a, const ColliderType& collider_b,
const VertexType& direction)
{
return collider_a.find_abs_furthest_vertex(direction) - collider_b.find_abs_furthest_vertex(-direction);
}
[[nodiscard]]
static bool is_collide(const ColliderType& collider_a, const ColliderType& collider_b)
{
// Get initial support point in any direction
auto support = find_support_vertex(collider_a, collider_b, {1, 0, 0});
Simplex<VertexType> simplex;
simplex.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;
simplex.push_front(support);
if (simplex.handle(direction))
return true;
}
}
};
} // namespace omath::collision