diff --git a/Source/Engine/Core/Math/CollisionsHelper.cpp b/Source/Engine/Core/Math/CollisionsHelper.cpp index 8ec9d0212..b966523ec 100644 --- a/Source/Engine/Core/Math/CollisionsHelper.cpp +++ b/Source/Engine/Core/Math/CollisionsHelper.cpp @@ -93,14 +93,20 @@ void CollisionsHelper::ClosestPointPointTriangle(const Vector3& point, const Vec const float d1 = Vector3::Dot(ab, ap); const float d2 = Vector3::Dot(ac, ap); if (d1 <= 0.0f && d2 <= 0.0f) + { result = vertex1; //Barycentric coordinates (1,0,0) + return; + } // Check if P in vertex region outside B const Vector3 bp = point - vertex2; const float d3 = Vector3::Dot(ab, bp); const float d4 = Vector3::Dot(ac, bp); if (d3 >= 0.0f && d4 <= d3) + { result = vertex2; // Barycentric coordinates (0,1,0) + return; + } // Check if P in edge region of AB, if so return projection of P onto AB const float vc = d1 * d4 - d3 * d2; @@ -108,6 +114,7 @@ void CollisionsHelper::ClosestPointPointTriangle(const Vector3& point, const Vec { const float v = d1 / (d1 - d3); result = vertex1 + v * ab; //Barycentric coordinates (1-v,v,0) + return; } //Check if P in vertex region outside C @@ -115,7 +122,10 @@ void CollisionsHelper::ClosestPointPointTriangle(const Vector3& point, const Vec const float d5 = Vector3::Dot(ab, cp); const float d6 = Vector3::Dot(ac, cp); if (d6 >= 0.0f && d5 <= d6) + { result = vertex3; //Barycentric coordinates (0,0,1) + return; + } //Check if P in edge region of AC, if so return projection of P onto AC const float vb = d5 * d2 - d1 * d6; @@ -123,6 +133,7 @@ void CollisionsHelper::ClosestPointPointTriangle(const Vector3& point, const Vec { const float w = d2 / (d2 - d6); result = vertex1 + w * ac; //Barycentric coordinates (1-w,0,w) + return; } //Check if P in edge region of BC, if so return projection of P onto BC @@ -131,6 +142,7 @@ void CollisionsHelper::ClosestPointPointTriangle(const Vector3& point, const Vec { const float w = (d4 - d3) / (d4 - d3 + (d5 - d6)); result = vertex2 + w * (vertex3 - vertex2); //Barycentric coordinates (0,1-w,w) + return; } //P inside face region. Compute Q through its Barycentric coordinates (u,v,w)