mirror of
https://github.com/orange-cpp/omath.git
synced 2026-02-13 15:03:27 +00:00
Simplifies the GJK algorithm by using a type alias for the vertex type, improving code readability and reducing redundancy. Removes unnecessary includes.
48 lines
1.3 KiB
C++
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
|