From e05eba42c3404f57fe27504dac70d699821cb3cf Mon Sep 17 00:00:00 2001 From: Orange Date: Sat, 6 Dec 2025 13:34:34 +0300 Subject: [PATCH 1/5] added collider interface --- include/omath/3d_primitives/mesh.hpp | 10 ++-- .../omath/collision/collider_interface.hpp | 23 ++++++++ include/omath/collision/epa_algorithm.hpp | 12 ++--- include/omath/collision/gjk_algorithm.hpp | 7 +-- include/omath/collision/mesh_collider.hpp | 45 +++++++++------- include/omath/collision/triangle_collider.hpp | 52 +++++++++++++++++++ include/omath/linear_algebra/mat.hpp | 2 +- tests/general/unit_test_colider.cpp | 2 +- tests/general/unit_test_epa.cpp | 8 +-- 9 files changed, 118 insertions(+), 43 deletions(-) create mode 100644 include/omath/collision/collider_interface.hpp create mode 100644 include/omath/collision/triangle_collider.hpp diff --git a/include/omath/3d_primitives/mesh.hpp b/include/omath/3d_primitives/mesh.hpp index baaa8cd..cc2e5e4 100644 --- a/include/omath/3d_primitives/mesh.hpp +++ b/include/omath/3d_primitives/mesh.hpp @@ -99,10 +99,10 @@ namespace omath::primitives } [[nodiscard]] - VectorType vertex_to_world_space(const Vector3& vertex_position) const + VectorType vertex_position_to_world_space(const Vector3& vertex_position) const requires HasPosition { - auto abs_vec = get_to_world_matrix() * mat_column_from_vector(vertex_position); + auto abs_vec = get_to_world_matrix() * mat_column_from_vector(vertex_position); return {abs_vec.at(0, 0), abs_vec.at(1, 0), abs_vec.at(2, 0)}; } @@ -111,9 +111,9 @@ namespace omath::primitives Triangle make_face_in_world_space(const Ebo::const_iterator vao_iterator) const requires HasPosition { - return {vertex_to_world_space(m_vertex_buffer.at(vao_iterator->x).position), - vertex_to_world_space(m_vertex_buffer.at(vao_iterator->y).position), - vertex_to_world_space(m_vertex_buffer.at(vao_iterator->z).position)}; + return {vertex_position_to_world_space(m_vertex_buffer.at(vao_iterator->x).position), + vertex_position_to_world_space(m_vertex_buffer.at(vao_iterator->y).position), + vertex_position_to_world_space(m_vertex_buffer.at(vao_iterator->z).position)}; } private: diff --git a/include/omath/collision/collider_interface.hpp b/include/omath/collision/collider_interface.hpp new file mode 100644 index 0000000..b27487b --- /dev/null +++ b/include/omath/collision/collider_interface.hpp @@ -0,0 +1,23 @@ +// +// Created by Vladislav on 06.12.2025. +// +#pragma once + + +namespace omath::collision +{ + template> + class ColliderInterface + { + public: + using VectorType = VecType; + virtual ~ColliderInterface() = default; + + [[nodiscard]] + virtual VectorType find_abs_furthest_vertex_position(const VectorType& direction) const = 0; + + [[nodiscard]] + virtual const VectorType& get_origin() const = 0; + virtual void set_origin(const VectorType& new_origin) = 0; + }; +} \ No newline at end of file diff --git a/include/omath/collision/epa_algorithm.hpp b/include/omath/collision/epa_algorithm.hpp index 6b1d732..6083aad 100644 --- a/include/omath/collision/epa_algorithm.hpp +++ b/include/omath/collision/epa_algorithm.hpp @@ -101,10 +101,7 @@ namespace omath::collision out.num_vertices = static_cast(vertexes.size()); out.num_faces = static_cast(faces.size()); - const auto centers = b.get_origin() - a.get_origin(); - const auto sign = out.normal.dot(centers) >= 0 ? 1 : -1; - - out.penetration_vector = out.normal * out.depth * sign; + out.penetration_vector = out.normal * out.depth; return out; } @@ -163,10 +160,7 @@ namespace omath::collision out.num_vertices = static_cast(vertexes.size()); out.num_faces = static_cast(faces.size()); - const auto centers = b.get_origin() - a.get_origin(); - const auto sign = out.normal.dot(centers) >= 0 ? 1 : -1; - - out.penetration_vector = out.normal * out.depth * sign; + out.penetration_vector = out.normal * out.depth; return out; } @@ -253,7 +247,7 @@ namespace omath::collision [[nodiscard]] static VectorType support_point(const ColliderType& a, const ColliderType& b, const VectorType& dir) { - return a.find_abs_furthest_vertex(dir).position - b.find_abs_furthest_vertex(-dir).position; + return a.find_abs_furthest_vertex_position(dir) - b.find_abs_furthest_vertex_position(-dir); } template diff --git a/include/omath/collision/gjk_algorithm.hpp b/include/omath/collision/gjk_algorithm.hpp index c3db4ea..8cce66d 100644 --- a/include/omath/collision/gjk_algorithm.hpp +++ b/include/omath/collision/gjk_algorithm.hpp @@ -17,14 +17,15 @@ namespace omath::collision template class GjkAlgorithm final { - using VertexType = ColliderType::VertexType; - using VectorType = VertexType::VectorType; + using VectorType = ColliderType::VectorType; + public: [[nodiscard]] static VectorType find_support_vertex(const ColliderType& collider_a, const ColliderType& collider_b, const VectorType& direction) { - return collider_a.find_abs_furthest_vertex(direction).position - collider_b.find_abs_furthest_vertex(-direction).position; + return collider_a.find_abs_furthest_vertex_position(direction) + - collider_b.find_abs_furthest_vertex_position(-direction); } [[nodiscard]] diff --git a/include/omath/collision/mesh_collider.hpp b/include/omath/collision/mesh_collider.hpp index 96b4d7a..53f15d9 100644 --- a/include/omath/collision/mesh_collider.hpp +++ b/include/omath/collision/mesh_collider.hpp @@ -3,13 +3,22 @@ // #pragma once +#include "collider_interface.hpp" #include "omath/linear_algebra/vector3.hpp" +#ifdef OMATH_BUILD_TESTS +// ReSharper disable once CppInconsistentNaming +class UnitTestColider_FindFurthestVertex_Test; +#endif + namespace omath::collision { template - class MeshCollider + class MeshCollider final : public ColliderInterface> { +#ifdef OMATH_BUILD_TESTS + friend UnitTestColider_FindFurthestVertex_Test; +#endif public: using VertexType = MeshType::VertexType; using VectorType = VertexType::VectorType; @@ -17,6 +26,23 @@ namespace omath::collision { } + [[nodiscard]] + VectorType find_abs_furthest_vertex_position(const VectorType& direction) const override + { + return m_mesh.vertex_position_to_world_space(find_furthest_vertex(direction).position); + } + + [[nodiscard]] + const VectorType& get_origin() const override + { + return m_mesh.get_origin(); + } + void set_origin(const VectorType& new_origin) override + { + m_mesh.set_origin(new_origin); + } + + private: [[nodiscard]] const VertexType& find_furthest_vertex(const VectorType& direction) const { @@ -24,23 +50,6 @@ namespace omath::collision m_mesh.m_vertex_buffer, [&direction](const auto& first, const auto& second) { return first.position.dot(direction) < second.position.dot(direction); }); } - - [[nodiscard]] - VertexType find_abs_furthest_vertex(const VectorType& direction) const - { - const auto& vertex = find_furthest_vertex(direction); - auto new_vertex = vertex; - new_vertex.position = m_mesh.vertex_to_world_space(find_furthest_vertex(direction).position); - return new_vertex; - } - - [[nodiscard]] - const VectorType& get_origin() const - { - return m_mesh.get_origin(); - } - - private: MeshType m_mesh; }; } // namespace omath::collision \ No newline at end of file diff --git a/include/omath/collision/triangle_collider.hpp b/include/omath/collision/triangle_collider.hpp new file mode 100644 index 0000000..0482e14 --- /dev/null +++ b/include/omath/collision/triangle_collider.hpp @@ -0,0 +1,52 @@ +// +// Created by Vlad on 11/9/2025. +// + +#pragma once +#include "omath/linear_algebra/vector3.hpp" +#include + +namespace omath::collision +{ + struct TriangleCollider + { + Vector3 position; + }; + class TriangleMeshCollider final : public ColliderInterface> + { + public: + using VectorType = Vector3; + using VertexType = TriangleCollider; + explicit TriangleMeshCollider(const Triangle>& triangle) + : m_mesh({triangle.m_vertex1, triangle.m_vertex2, triangle.m_vertex3}) + { + } + + [[nodiscard]] + VectorType find_abs_furthest_vertex_position(const VectorType& direction) const override + { + return find_furthest_vertex(direction).position; + } + + [[nodiscard]] + const VectorType& get_origin() const override + { + return m_origin; + } + void set_origin(const VectorType&) override + { + + } + private: + [[nodiscard]] + const VertexType& find_furthest_vertex(const VectorType& direction) const + { + return *std::ranges::max_element( + m_mesh, [&direction](const auto& first, const auto& second) + { return first.position.dot(direction) < second.position.dot(direction); }); + } + + Vector3 m_origin{}; + std::array m_mesh; + }; +} // namespace omath::collision \ No newline at end of file diff --git a/include/omath/linear_algebra/mat.hpp b/include/omath/linear_algebra/mat.hpp index 18b7d71..3ec43ea 100644 --- a/include/omath/linear_algebra/mat.hpp +++ b/include/omath/linear_algebra/mat.hpp @@ -46,7 +46,7 @@ namespace omath } [[nodiscard]] - constexpr static MatStoreType get_store_ordering() noexcept + consteval static MatStoreType get_store_ordering() noexcept { return StoreType; } diff --git a/tests/general/unit_test_colider.cpp b/tests/general/unit_test_colider.cpp index 4800ea2..ce3a960 100644 --- a/tests/general/unit_test_colider.cpp +++ b/tests/general/unit_test_colider.cpp @@ -17,7 +17,7 @@ TEST(UnitTestColider, CheckToWorld) mesh.set_origin({0, 2, 0}); const omath::source_engine::MeshCollider collider(mesh); - const auto vertex = collider.find_abs_furthest_vertex({1.f, 0.f, 0.f}).position; + const auto vertex = collider.find_abs_furthest_vertex_position({1.f, 0.f, 0.f}); EXPECT_EQ(vertex, omath::Vector3(1.f, 3.f, 1.f)); } diff --git a/tests/general/unit_test_epa.cpp b/tests/general/unit_test_epa.cpp index 1b9cd5b..3e8d0ad 100644 --- a/tests/general/unit_test_epa.cpp +++ b/tests/general/unit_test_epa.cpp @@ -61,7 +61,7 @@ TEST(UnitTestEpa, TestCollisionTrue) // Try both signs with a tiny margin (avoid grazing contacts) const float margin = 1.0f + 1e-3f; - const auto pen = epa->normal * epa->depth; + const auto pen = epa->penetration_vector; Mesh b_plus = b; b_plus.set_origin(b_plus.get_origin() + pen * margin); @@ -133,12 +133,8 @@ TEST(UnitTestEpa, TestCollisionTrue2) EXPECT_NEAR(epa->normal.y, 0.0f, 1e-3f); EXPECT_NEAR(epa->normal.z, 0.0f, 1e-3f); - // Choose a deterministic sign: orient penetration from A toward B - const auto centers = b.get_origin() - a.get_origin(); // (0.5, 0, 0) - float sign = (epa->normal.dot(centers) >= 0.0f) ? +1.0f : -1.0f; - constexpr float margin = 1.0f + 1e-3f; // tiny slack to avoid grazing - const auto pen = epa->normal * epa->depth * sign; + const auto pen = epa->normal * epa->depth; // Apply once: B + pen must separate; the opposite must still collide Mesh b_resolved = b; From c158f084305e6a84e18d77cd9346c71ebc71de72 Mon Sep 17 00:00:00 2001 From: Orange Date: Sat, 6 Dec 2025 13:56:13 +0300 Subject: [PATCH 2/5] fix --- include/omath/collision/epa_algorithm.hpp | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/include/omath/collision/epa_algorithm.hpp b/include/omath/collision/epa_algorithm.hpp index 6083aad..c2ae8c2 100644 --- a/include/omath/collision/epa_algorithm.hpp +++ b/include/omath/collision/epa_algorithm.hpp @@ -32,7 +32,7 @@ namespace omath::collision struct Result final { - VectorType normal{}; // outward normal (from B to A) + VectorType normal{}; // from A to B VectorType penetration_vector; float depth{0.0f}; int iterations{0}; @@ -86,17 +86,17 @@ namespace omath::collision break; const int fidx = heap.top().idx; - const Face f = faces[fidx]; + const Face face = faces[fidx]; // Get the furthest point in face normal direction - const VectorType p = support_point(a, b, f.n); - const float p_dist = f.n.dot(p); + const VectorType p = support_point(a, b, face.n); + const float p_dist = face.n.dot(p); // Converged if we can’t push the face closer than tolerance - if (p_dist - f.d <= params.tolerance) + if (p_dist - face.d <= params.tolerance) { - out.normal = f.n; - out.depth = f.d; // along unit normal + out.normal = face.n; + out.depth = face.d; // along unit normal out.iterations = it + 1; out.num_vertices = static_cast(vertexes.size()); out.num_faces = static_cast(faces.size()); From d23bc3204d3bd2e98b0abcd83cb4d88739015c4e Mon Sep 17 00:00:00 2001 From: Orange Date: Sat, 6 Dec 2025 14:30:44 +0300 Subject: [PATCH 3/5] update --- include/omath/collision/mesh_collider.hpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/include/omath/collision/mesh_collider.hpp b/include/omath/collision/mesh_collider.hpp index 53f15d9..a9a8870 100644 --- a/include/omath/collision/mesh_collider.hpp +++ b/include/omath/collision/mesh_collider.hpp @@ -14,14 +14,14 @@ class UnitTestColider_FindFurthestVertex_Test; namespace omath::collision { template - class MeshCollider final : public ColliderInterface> + class MeshCollider final : public ColliderInterface { #ifdef OMATH_BUILD_TESTS friend UnitTestColider_FindFurthestVertex_Test; #endif public: using VertexType = MeshType::VertexType; - using VectorType = VertexType::VectorType; + using VectorType = MeshType::VertexType::VectorType; explicit MeshCollider(MeshType mesh): m_mesh(std::move(mesh)) { } From 3831bc09992e5d33317f0bc13c0d4c8e17016d0d Mon Sep 17 00:00:00 2001 From: Orange Date: Sat, 6 Dec 2025 14:34:20 +0300 Subject: [PATCH 4/5] removed file --- include/omath/collision/triangle_collider.hpp | 52 ------------------- 1 file changed, 52 deletions(-) delete mode 100644 include/omath/collision/triangle_collider.hpp diff --git a/include/omath/collision/triangle_collider.hpp b/include/omath/collision/triangle_collider.hpp deleted file mode 100644 index 0482e14..0000000 --- a/include/omath/collision/triangle_collider.hpp +++ /dev/null @@ -1,52 +0,0 @@ -// -// Created by Vlad on 11/9/2025. -// - -#pragma once -#include "omath/linear_algebra/vector3.hpp" -#include - -namespace omath::collision -{ - struct TriangleCollider - { - Vector3 position; - }; - class TriangleMeshCollider final : public ColliderInterface> - { - public: - using VectorType = Vector3; - using VertexType = TriangleCollider; - explicit TriangleMeshCollider(const Triangle>& triangle) - : m_mesh({triangle.m_vertex1, triangle.m_vertex2, triangle.m_vertex3}) - { - } - - [[nodiscard]] - VectorType find_abs_furthest_vertex_position(const VectorType& direction) const override - { - return find_furthest_vertex(direction).position; - } - - [[nodiscard]] - const VectorType& get_origin() const override - { - return m_origin; - } - void set_origin(const VectorType&) override - { - - } - private: - [[nodiscard]] - const VertexType& find_furthest_vertex(const VectorType& direction) const - { - return *std::ranges::max_element( - m_mesh, [&direction](const auto& first, const auto& second) - { return first.position.dot(direction) < second.position.dot(direction); }); - } - - Vector3 m_origin{}; - std::array m_mesh; - }; -} // namespace omath::collision \ No newline at end of file From 27c1d147c5d4e3492e54c83e5c228a641a436952 Mon Sep 17 00:00:00 2001 From: Orange Date: Sat, 6 Dec 2025 14:49:30 +0300 Subject: [PATCH 5/5] fixed naming --- include/omath/collision/epa_algorithm.hpp | 11 ++++++----- include/omath/collision/gjk_algorithm.hpp | 14 +++++++------- 2 files changed, 13 insertions(+), 12 deletions(-) diff --git a/include/omath/collision/epa_algorithm.hpp b/include/omath/collision/epa_algorithm.hpp index c2ae8c2..e98cf05 100644 --- a/include/omath/collision/epa_algorithm.hpp +++ b/include/omath/collision/epa_algorithm.hpp @@ -6,10 +6,10 @@ #include #include #include +#include #include #include #include -#include namespace omath::collision { @@ -23,11 +23,11 @@ namespace omath::collision { a / s } -> std::same_as; }; - template + template class Epa final { public: - using VectorType = ColliderType::VectorType; + using VectorType = ColliderInterfaceType::VectorType; static_assert(EpaVector, "VertexType must satisfy EpaVector concept"); struct Result final @@ -48,7 +48,7 @@ namespace omath::collision // Precondition: simplex.size()==4 and contains the origin. [[nodiscard]] - static std::optional solve(const ColliderType& a, const ColliderType& b, + static std::optional solve(const ColliderInterfaceType& a, const ColliderInterfaceType& b, const Simplex& simplex, const Params params = {}, std::shared_ptr mem_resource = { std::shared_ptr{}, std::pmr::get_default_resource()}) @@ -245,7 +245,8 @@ namespace omath::collision } [[nodiscard]] - static VectorType support_point(const ColliderType& a, const ColliderType& b, const VectorType& dir) + static VectorType support_point(const ColliderInterfaceType& a, const ColliderInterfaceType& b, + const VectorType& dir) { return a.find_abs_furthest_vertex_position(dir) - b.find_abs_furthest_vertex_position(-dir); } diff --git a/include/omath/collision/gjk_algorithm.hpp b/include/omath/collision/gjk_algorithm.hpp index 8cce66d..b29bd25 100644 --- a/include/omath/collision/gjk_algorithm.hpp +++ b/include/omath/collision/gjk_algorithm.hpp @@ -14,29 +14,29 @@ namespace omath::collision Simplex simplex; // valid only if hit == true and size==4 }; - template + template class GjkAlgorithm final { - using VectorType = ColliderType::VectorType; + using VectorType = ColliderInterfaceType::VectorType; public: [[nodiscard]] - static VectorType find_support_vertex(const ColliderType& collider_a, const ColliderType& collider_b, - const VectorType& direction) + static VectorType find_support_vertex(const ColliderInterfaceType& collider_a, + const ColliderInterfaceType& collider_b, const VectorType& direction) { return collider_a.find_abs_furthest_vertex_position(direction) - collider_b.find_abs_furthest_vertex_position(-direction); } [[nodiscard]] - static bool is_collide(const ColliderType& collider_a, const ColliderType& collider_b) + static bool is_collide(const ColliderInterfaceType& collider_a, const ColliderInterfaceType& collider_b) { return is_collide_with_simplex_info(collider_a, collider_b).hit; } [[nodiscard]] - static GjkHitInfo is_collide_with_simplex_info(const ColliderType& collider_a, - const ColliderType& collider_b) + static GjkHitInfo is_collide_with_simplex_info(const ColliderInterfaceType& collider_a, + const ColliderInterfaceType& collider_b) { auto support = find_support_vertex(collider_a, collider_b, VectorType{1, 0, 0});