From b2a512eafe1aea429e7786b056dc1aafd5a3407f Mon Sep 17 00:00:00 2001 From: Orange Date: Sun, 9 Nov 2025 17:02:07 +0300 Subject: [PATCH] Adds mesh scaling to mesh collider Updates the mesh collider to include a scale parameter, allowing for non-uniform scaling of the collision mesh. This provides more flexibility in defining collision shapes and supports a wider range of scenarios. --- include/omath/collision/mesh_collider.hpp | 8 ++++--- tests/general/unit_test_gjk.cpp | 27 ++++------------------- 2 files changed, 9 insertions(+), 26 deletions(-) diff --git a/include/omath/collision/mesh_collider.hpp b/include/omath/collision/mesh_collider.hpp index e0b4ea3..4dce3ae 100644 --- a/include/omath/collision/mesh_collider.hpp +++ b/include/omath/collision/mesh_collider.hpp @@ -13,20 +13,22 @@ namespace omath::collision { public: using VertexType = Vector3; - MeshCollider(const std::vector>& vertexes, const Vector3 origin) - : m_vertexes(vertexes), m_origin(origin) + MeshCollider(const std::vector& vertexes, const VertexType& origin, const VertexType& scale = {1.f, 1.f, 1.f}) + : m_vertexes(vertexes),m_scale(scale), m_origin(origin) { if (m_vertexes.empty()) throw std::runtime_error("Collider cannot have 0 vertexes"); } std::vector> m_vertexes; + Vector3 m_scale; + Vector3 m_origin; source_engine::ViewAngles m_rotation; [[nodiscard]] source_engine::Mat4X4 to_world() const { - return mat_translation(m_origin) * source_engine::rotation_matrix(m_rotation); + return mat_scale(m_scale) * mat_translation(m_origin) * source_engine::rotation_matrix(m_rotation); } [[nodiscard]] diff --git a/tests/general/unit_test_gjk.cpp b/tests/general/unit_test_gjk.cpp index 8c306cc..977cbe9 100644 --- a/tests/general/unit_test_gjk.cpp +++ b/tests/general/unit_test_gjk.cpp @@ -4,7 +4,7 @@ #include #include -TEST(UnitTestGjk, TestCollisionTrue) +namespace { const std::vector> mesh = { {-1.f, -1.f, -1.f}, @@ -16,6 +16,9 @@ TEST(UnitTestGjk, TestCollisionTrue) { 1.f, -1.f, 1.f}, { 1.f, -1.f, -1.f} }; +} +TEST(UnitTestGjk, TestCollisionTrue) +{ const omath::collision::MeshCollider collider_a(mesh, {0.f, 0.f, 0.f}); const omath::collision::MeshCollider collider_b(mesh, {0.f, 0.5f, 0.f}); @@ -26,17 +29,6 @@ TEST(UnitTestGjk, TestCollisionTrue) } TEST(UnitTestGjk, TestCollisionFalse) { - const std::vector> mesh = { - {-1.f, -1.f, -1.f}, - {-1.f, -1.f, 1.f}, - {-1.f, 1.f, -1.f}, - {-1.f, 1.f, 1.f}, - { 1.f, 1.f, 1.f}, // x = +1 vertices (put {1,1,1} first in case your support breaks ties by first-hit) - { 1.f, 1.f, -1.f}, - { 1.f, -1.f, 1.f}, - { 1.f, -1.f, -1.f} - }; - const omath::collision::MeshCollider collider_a(mesh, {0.f, 0.f, 0.f}); const omath::collision::MeshCollider collider_b(mesh, {0.f, 2.1f, 0.f}); @@ -47,17 +39,6 @@ TEST(UnitTestGjk, TestCollisionFalse) TEST(UnitTestGjk, TestCollisionEqualOrigin) { - const std::vector> mesh = { - {-1.f, -1.f, -1.f}, - {-1.f, -1.f, 1.f}, - {-1.f, 1.f, -1.f}, - {-1.f, 1.f, 1.f}, - { 1.f, 1.f, 1.f}, // x = +1 vertices (put {1,1,1} first in case your support breaks ties by first-hit) - { 1.f, 1.f, -1.f}, - { 1.f, -1.f, 1.f}, - { 1.f, -1.f, -1.f} - }; - const omath::collision::MeshCollider collider_a(mesh, {0.f, 0.f, 0.f}); const omath::collision::MeshCollider collider_b(mesh, {0.f, 0.f, 0.f});