mirror of
https://github.com/orange-cpp/omath.git
synced 2026-02-13 07:03:25 +00:00
Refactors collision detection with GJK and EPA
This commit refactors the collision detection pipeline to utilize a more standard GJK algorithm and simplifies the EPA implementation. Removes the custom `GjkAlgorithmWithSimplex` in favor of the standalone `GjkAlgorithm`. This streamlines the collision detection process and enhances code clarity. Updates unit tests to align with the new GJK implementation, ensuring continued functionality and correctness.
This commit is contained in:
@@ -258,51 +258,4 @@ namespace omath::collision
|
|||||||
return V{1, 0, 0};
|
return V{1, 0, 0};
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
// Optional: the GJK that returns a simplex for EPA (unchanged)
|
|
||||||
template<class ColliderType>
|
|
||||||
class GjkAlgorithmWithSimplex final
|
|
||||||
{
|
|
||||||
using Vertex = ColliderType::VertexType;
|
|
||||||
|
|
||||||
public:
|
|
||||||
struct Hit
|
|
||||||
{
|
|
||||||
bool hit{false};
|
|
||||||
Simplex<Vertex> simplex;
|
|
||||||
};
|
|
||||||
|
|
||||||
[[nodiscard]]
|
|
||||||
static Vertex find_support_vertex(const ColliderType& a, const ColliderType& b, const Vertex& dir)
|
|
||||||
{
|
|
||||||
return a.find_abs_furthest_vertex(dir) - b.find_abs_furthest_vertex(-dir);
|
|
||||||
}
|
|
||||||
|
|
||||||
[[nodiscard]]
|
|
||||||
static Hit collide(const ColliderType& a, const ColliderType& b)
|
|
||||||
{
|
|
||||||
auto support = find_support_vertex(a, b, {1, 0, 0});
|
|
||||||
Simplex<Vertex> simplex;
|
|
||||||
simplex.push_front(support);
|
|
||||||
auto direction = -support;
|
|
||||||
|
|
||||||
while (true)
|
|
||||||
{
|
|
||||||
support = find_support_vertex(a, b, direction);
|
|
||||||
if (support.dot(direction) <= 0.f)
|
|
||||||
return {};
|
|
||||||
simplex.push_front(support);
|
|
||||||
if (simplex.handle(direction))
|
|
||||||
{
|
|
||||||
if (simplex.size() == 4)
|
|
||||||
return {true, simplex};
|
|
||||||
// rare degeneracy: reseed
|
|
||||||
support = find_support_vertex(a, b, {0, 1, 0});
|
|
||||||
simplex.clear();
|
|
||||||
simplex.push_front(support);
|
|
||||||
direction = -support;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
};
|
|
||||||
} // namespace omath::collision
|
} // namespace omath::collision
|
||||||
|
|||||||
@@ -1,13 +1,14 @@
|
|||||||
#include <gtest/gtest.h>
|
#include "omath/collision/epa_algorithm.hpp" // Epa<Collider> + GjkAlgorithmWithSimplex<Collider>
|
||||||
#include "omath/linear_algebra/vector3.hpp"
|
#include "omath/collision/gjk_algorithm.hpp"
|
||||||
#include "omath/collision/simplex.hpp"
|
#include "omath/collision/simplex.hpp"
|
||||||
#include "omath/collision/epa_algorithm.hpp" // Epa<Collider> + GjkAlgorithmWithSimplex<Collider>
|
|
||||||
#include "omath/engines/source_engine/mesh.hpp"
|
|
||||||
#include "omath/engines/source_engine/collider.hpp"
|
#include "omath/engines/source_engine/collider.hpp"
|
||||||
|
#include "omath/engines/source_engine/mesh.hpp"
|
||||||
|
#include "omath/linear_algebra/vector3.hpp"
|
||||||
|
#include <gtest/gtest.h>
|
||||||
|
|
||||||
using Mesh = omath::source_engine::Mesh;
|
using Mesh = omath::source_engine::Mesh;
|
||||||
using Collider = omath::source_engine::MeshCollider;
|
using Collider = omath::source_engine::MeshCollider;
|
||||||
using GJK = omath::collision::GjkAlgorithmWithSimplex<Collider>;
|
using GJK = omath::collision::GjkAlgorithm<Collider>;
|
||||||
using EPA = omath::collision::Epa<Collider>;
|
using EPA = omath::collision::Epa<Collider>;
|
||||||
|
|
||||||
TEST(UnitTestEpa, TestCollisionTrue)
|
TEST(UnitTestEpa, TestCollisionTrue)
|
||||||
@@ -29,7 +30,7 @@ TEST(UnitTestEpa, TestCollisionTrue)
|
|||||||
Collider A(a), B(b);
|
Collider A(a), B(b);
|
||||||
|
|
||||||
// GJK
|
// GJK
|
||||||
auto gjk = GJK::collide(A, B);
|
auto gjk = GJK::is_collide_with_simplex_info(A, B);
|
||||||
ASSERT_TRUE(gjk.hit) << "GJK should report collision";
|
ASSERT_TRUE(gjk.hit) << "GJK should report collision";
|
||||||
|
|
||||||
// EPA
|
// EPA
|
||||||
@@ -57,8 +58,8 @@ TEST(UnitTestEpa, TestCollisionTrue)
|
|||||||
|
|
||||||
Collider B_plus(b_plus), B_minus(b_minus);
|
Collider B_plus(b_plus), B_minus(b_minus);
|
||||||
|
|
||||||
const bool sep_plus = !GJK::collide(A, B_plus).hit;
|
const bool sep_plus = !GJK::is_collide_with_simplex_info(A, B_plus).hit;
|
||||||
const bool sep_minus = !GJK::collide(A, B_minus).hit;
|
const bool sep_minus = !GJK::is_collide_with_simplex_info(A, B_minus).hit;
|
||||||
|
|
||||||
// Exactly one direction should separate
|
// Exactly one direction should separate
|
||||||
EXPECT_NE(sep_plus, sep_minus) << "Exactly one of ±penetration must separate";
|
EXPECT_NE(sep_plus, sep_minus) << "Exactly one of ±penetration must separate";
|
||||||
@@ -67,9 +68,9 @@ TEST(UnitTestEpa, TestCollisionTrue)
|
|||||||
const auto resolve = sep_plus ? ( pen * margin) : (-pen * margin);
|
const auto resolve = sep_plus ? ( pen * margin) : (-pen * margin);
|
||||||
|
|
||||||
Mesh b_resolved = b; b_resolved.set_origin(b_resolved.get_origin() + resolve);
|
Mesh b_resolved = b; b_resolved.set_origin(b_resolved.get_origin() + resolve);
|
||||||
EXPECT_FALSE(GJK::collide(A, Collider(b_resolved)).hit) << "Resolved position should be non-colliding";
|
EXPECT_FALSE(GJK::is_collide(A, Collider(b_resolved))) << "Resolved position should be non-colliding";
|
||||||
|
|
||||||
// Moving the other way should still collide
|
// Moving the other way should still collide
|
||||||
Mesh b_wrong = b; b_wrong.set_origin(b_wrong.get_origin() - resolve);
|
Mesh b_wrong = b; b_wrong.set_origin(b_wrong.get_origin() - resolve);
|
||||||
EXPECT_TRUE(GJK::collide(A, Collider(b_wrong)).hit);
|
EXPECT_TRUE(GJK::is_collide(A, Collider(b_wrong)));
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user