From 1e540862a0e5fdf4c72e9369d1d31e299aa3c421 Mon Sep 17 00:00:00 2001 From: Orange Date: Sun, 9 Nov 2025 14:15:32 +0300 Subject: [PATCH] Refactors GJK tetrahedron handling Updates the `handle_tetrahedron` function to use const references for simplex points, improving efficiency and readability. Corrects a potential bug where the `simplex` variable wasn't being correctly updated when recursively calling `handle_triangle`. Also, const-qualifies `point_to_same_direction` for better safety. --- include/omath/collision/simplex.hpp | 30 ++++++++++++------------ include/omath/linear_algebra/vector3.hpp | 2 +- include/omath/utility/color.hpp | 2 +- 3 files changed, 17 insertions(+), 17 deletions(-) diff --git a/include/omath/collision/simplex.hpp b/include/omath/collision/simplex.hpp index 20ca8de..46d7e1e 100644 --- a/include/omath/collision/simplex.hpp +++ b/include/omath/collision/simplex.hpp @@ -113,30 +113,30 @@ namespace omath::collision return false; } - bool handle_tetrahedron(Simplex& points, Vector3& direction) + bool handle_tetrahedron(Simplex& simplex, Vector3& direction) { - Vector3 a = points[0]; - Vector3 b = points[1]; - Vector3 c = points[2]; - Vector3 d = points[3]; + const auto& a = simplex[0]; + const auto& b = simplex[1]; + const auto& c = simplex[2]; + const auto& d = simplex[3]; - Vector3 ab = b - a; - Vector3 ac = c - a; - Vector3 ad = d - a; - Vector3 ao = -a; + const Vector3 ab = b - a; + const Vector3 ac = c - a; + const Vector3 ad = d - a; + const Vector3 ao = -a; - Vector3 abc = ab.cross(ac); - Vector3 acd = ac.cross(ad); - Vector3 adb = ad.cross(ab); + const Vector3 abc = ab.cross(ac); + const Vector3 acd = ac.cross(ad); + const Vector3 adb = ad.cross(ab); if (abc.point_to_same_direction(ao)) - return handle_triangle(points = {a, b, c}, direction); + return handle_triangle(simplex = {a, b, c}, direction); if (acd.point_to_same_direction(ao)) - return handle_triangle(points = {a, c, d}, direction); + return handle_triangle(simplex = {a, c, d}, direction); if (adb.point_to_same_direction(ao)) - return handle_triangle(points = {a, d, b}, direction); + return handle_triangle(simplex = {a, d, b}, direction); return true; diff --git a/include/omath/linear_algebra/vector3.hpp b/include/omath/linear_algebra/vector3.hpp index 5432bb0..4b1d381 100644 --- a/include/omath/linear_algebra/vector3.hpp +++ b/include/omath/linear_algebra/vector3.hpp @@ -217,7 +217,7 @@ namespace omath } [[nodiscard]] - bool point_to_same_direction(const Vector3& other) + bool point_to_same_direction(const Vector3& other) const { return dot(other) > static_cast(0); } diff --git a/include/omath/utility/color.hpp b/include/omath/utility/color.hpp index cfacee8..fd73451 100644 --- a/include/omath/utility/color.hpp +++ b/include/omath/utility/color.hpp @@ -9,7 +9,7 @@ namespace omath { - struct Hsv + struct Hsv final { float hue{}; float saturation{};