From f3f25836e205e1ecb4486d8284b89087c7ee1157 Mon Sep 17 00:00:00 2001 From: intolerantape Date: Tue, 19 Jan 2021 23:09:23 -0800 Subject: [PATCH 1/2] Added 3D version of CollisionsHelper::ClosestPointPointLine(). --- Source/Engine/Core/Math/CollisionsHelper.cpp | 22 ++++++++++++++++++++ Source/Engine/Core/Math/CollisionsHelper.h | 9 ++++++++ 2 files changed, 31 insertions(+) diff --git a/Source/Engine/Core/Math/CollisionsHelper.cpp b/Source/Engine/Core/Math/CollisionsHelper.cpp index 48e7472d1..3200d8962 100644 --- a/Source/Engine/Core/Math/CollisionsHelper.cpp +++ b/Source/Engine/Core/Math/CollisionsHelper.cpp @@ -41,6 +41,28 @@ void CollisionsHelper::ClosestPointPointLine(const Vector2& point, const Vector2 } } +Vector3 CollisionsHelper::ClosestPointPointLine(const Vector3& point, const Vector3& p0, const Vector3& p1) +{ + const Vector3 p = point - p0; + Vector3 n = p1 - p0; + const float length = n.Length(); + if (length < 1e-10) + { + return p0; + } + n /= length; + const float dot = Vector3::Dot(n, p); + if (dot <= 0.0) + { + return p0; + } + else if (dot >= length) + { + return p1; + } + return p0 + n * dot; +} + void CollisionsHelper::ClosestPointPointTriangle(const Vector3& point, const Vector3& vertex1, const Vector3& vertex2, const Vector3& vertex3, Vector3& result) { // Source: Real-Time Collision Detection by Christer Ericson diff --git a/Source/Engine/Core/Math/CollisionsHelper.h b/Source/Engine/Core/Math/CollisionsHelper.h index 33631a682..ee8ea2828 100644 --- a/Source/Engine/Core/Math/CollisionsHelper.h +++ b/Source/Engine/Core/Math/CollisionsHelper.h @@ -72,6 +72,15 @@ public: /// When the method completes, contains the closest point between the two objects. static void ClosestPointPointLine(const Vector2& point, const Vector2& p0, const Vector2& p1, Vector2& result); + /// + /// Determines the closest point between a point and a line. + /// + /// The point to test. + /// The line first point. + /// The line second point. + /// When the method completes, contains the closest point between the two objects. + static Vector3 ClosestPointPointLine(const Vector3& point, const Vector3& p0, const Vector3& p1); + /// /// Determines the closest point between a point and a triangle. /// From 54e544a0f202ac9b14b76071feee205c3f330df3 Mon Sep 17 00:00:00 2001 From: intolerantape Date: Sat, 23 Jan 2021 03:18:04 -0800 Subject: [PATCH 2/2] In CollisionsHelper, added versions of functions with output parameters that return the outputs as said. Also added a 3D version of ClosestPointPointLine() because why wasn't that a thing? --- Source/Engine/Core/Math/CollisionsHelper.cpp | 70 +++++++++++++++++-- Source/Engine/Core/Math/CollisionsHelper.h | 73 ++++++++++++++++++++ 2 files changed, 138 insertions(+), 5 deletions(-) diff --git a/Source/Engine/Core/Math/CollisionsHelper.cpp b/Source/Engine/Core/Math/CollisionsHelper.cpp index 3200d8962..36a0b0182 100644 --- a/Source/Engine/Core/Math/CollisionsHelper.cpp +++ b/Source/Engine/Core/Math/CollisionsHelper.cpp @@ -41,26 +41,44 @@ void CollisionsHelper::ClosestPointPointLine(const Vector2& point, const Vector2 } } -Vector3 CollisionsHelper::ClosestPointPointLine(const Vector3& point, const Vector3& p0, const Vector3& p1) +Vector2 CollisionsHelper::ClosestPointPointLine(const Vector2& point, const Vector2& p0, const Vector2& p1) +{ + Vector2 result; + ClosestPointPointLine(point, p0, p1, result); + return result; +} + + +void CollisionsHelper::ClosestPointPointLine(const Vector3& point, const Vector3& p0, const Vector3& p1, Vector3& result) { const Vector3 p = point - p0; Vector3 n = p1 - p0; const float length = n.Length(); if (length < 1e-10) { - return p0; + result = p0; + return; } n /= length; const float dot = Vector3::Dot(n, p); if (dot <= 0.0) { - return p0; + result = p0; + return; } else if (dot >= length) { - return p1; + result = p1; + return; } - return p0 + n * dot; + result = p0 + n * dot; +} + +Vector3 CollisionsHelper::ClosestPointPointLine(const Vector3& point, const Vector3& p0, const Vector3& p1) +{ + Vector3 result; + ClosestPointPointLine(point, p0, p1, result); + return result; } void CollisionsHelper::ClosestPointPointTriangle(const Vector3& point, const Vector3& vertex1, const Vector3& vertex2, const Vector3& vertex3, Vector3& result) @@ -123,6 +141,13 @@ void CollisionsHelper::ClosestPointPointTriangle(const Vector3& point, const Vec result = vertex1 + ab * v2 + ac * w2; //= u*vertex1 + v*vertex2 + w*vertex3, u = va * denom = 1.0f - v - w } +Vector3 CollisionsHelper::ClosestPointPointTriangle(const Vector3& point, const Vector3& vertex1, const Vector3& vertex2, const Vector3& vertex3) +{ + Vector3 result; + ClosestPointPointTriangle(point, vertex1, vertex2, vertex3, result); + return result; +} + void CollisionsHelper::ClosestPointPlanePoint(const Plane& plane, const Vector3& point, Vector3& result) { // Source: Real-Time Collision Detection by Christer Ericson @@ -134,6 +159,13 @@ void CollisionsHelper::ClosestPointPlanePoint(const Plane& plane, const Vector3& result = point - t * plane.Normal; } +Vector3 CollisionsHelper::ClosestPointPlanePoint(const Plane& plane, const Vector3& point) +{ + Vector3 result; + ClosestPointPlanePoint(plane, point, result); + return result; +} + void CollisionsHelper::ClosestPointBoxPoint(const BoundingBox& box, const Vector3& point, Vector3& result) { // Source: Real-Time Collision Detection by Christer Ericson @@ -144,6 +176,13 @@ void CollisionsHelper::ClosestPointBoxPoint(const BoundingBox& box, const Vector Vector3::Min(temp, box.Maximum, result); } +Vector3 CollisionsHelper::ClosestPointBoxPoint(const BoundingBox& box, const Vector3& point) +{ + Vector3 result; + ClosestPointBoxPoint(box, point, result); + return result; +} + void CollisionsHelper::ClosestPointRectanglePoint(const Rectangle& rect, const Vector2& point, Vector2& result) { Vector2 temp, end; @@ -152,6 +191,13 @@ void CollisionsHelper::ClosestPointRectanglePoint(const Rectangle& rect, const V Vector2::Min(temp, end, result); } +Vector2 CollisionsHelper::ClosestPointRectanglePoint(const Rectangle& rect, const Vector2& point) +{ + Vector2 result; + ClosestPointRectanglePoint(rect, point, result); + return result; +} + void CollisionsHelper::ClosestPointSpherePoint(const BoundingSphere& sphere, const Vector3& point, Vector3& result) { // Source: Jorgy343 @@ -169,6 +215,13 @@ void CollisionsHelper::ClosestPointSpherePoint(const BoundingSphere& sphere, con result += sphere.Center; } +Vector3 CollisionsHelper::ClosestPointSpherePoint(const BoundingSphere& sphere, const Vector3& point) +{ + Vector3 result; + ClosestPointSpherePoint(sphere, point, result); + return result; +} + void CollisionsHelper::ClosestPointSphereSphere(const BoundingSphere& sphere1, const BoundingSphere& sphere2, Vector3& result) { // Source: Jorgy343 @@ -186,6 +239,13 @@ void CollisionsHelper::ClosestPointSphereSphere(const BoundingSphere& sphere1, c result += sphere1.Center; } +Vector3 CollisionsHelper::ClosestPointSphereSphere(const BoundingSphere& sphere1, const BoundingSphere& sphere2) +{ + Vector3 result; + ClosestPointSphereSphere(sphere1, sphere2, result); + return result; +} + float CollisionsHelper::DistancePlanePoint(const Plane& plane, const Vector3& point) { // Source: Real-Time Collision Detection by Christer Ericson diff --git a/Source/Engine/Core/Math/CollisionsHelper.h b/Source/Engine/Core/Math/CollisionsHelper.h index ee8ea2828..06b69741f 100644 --- a/Source/Engine/Core/Math/CollisionsHelper.h +++ b/Source/Engine/Core/Math/CollisionsHelper.h @@ -72,6 +72,15 @@ public: /// When the method completes, contains the closest point between the two objects. static void ClosestPointPointLine(const Vector2& point, const Vector2& p0, const Vector2& p1, Vector2& result); + /// + /// Determines the closest point between a point and a line. + /// + /// The point to test. + /// The line first point. + /// The line second point. + /// The closest point between the two objects. + static Vector2 ClosestPointPointLine(const Vector2& point, const Vector2& p0, const Vector2& p1); + /// /// Determines the closest point between a point and a line. /// @@ -79,6 +88,15 @@ public: /// The line first point. /// The line second point. /// When the method completes, contains the closest point between the two objects. + static void ClosestPointPointLine(const Vector3& point, const Vector3& p0, const Vector3& p1, Vector3& result); + + /// + /// Determines the closest point between a point and a line. + /// + /// The point to test. + /// The line first point. + /// The line second point. + /// The closest point between the two objects. static Vector3 ClosestPointPointLine(const Vector3& point, const Vector3& p0, const Vector3& p1); /// @@ -91,6 +109,16 @@ public: /// When the method completes, contains the closest point between the two objects. static void ClosestPointPointTriangle(const Vector3& point, const Vector3& vertex1, const Vector3& vertex2, const Vector3& vertex3, Vector3& result); + /// + /// Determines the closest point between a point and a triangle. + /// + /// The point to test. + /// The first vertex to test. + /// The second vertex to test. + /// The third vertex to test. + /// The closest point between the two objects. + static Vector3 ClosestPointPointTriangle(const Vector3& point, const Vector3& vertex1, const Vector3& vertex2, const Vector3& vertex3); + /// /// Determines the closest point between a and a point. /// @@ -98,6 +126,14 @@ public: /// The point to test. /// When the method completes, contains the closest point between the two objects. static void ClosestPointPlanePoint(const Plane& plane, const Vector3& point, Vector3& result); + + /// + /// Determines the closest point between a and a point. + /// + /// The plane to test. + /// The point to test. + /// The closest point between the two objects. + static Vector3 ClosestPointPlanePoint(const Plane& plane, const Vector3& point); /// /// Determines the closest point between a and a point. @@ -106,6 +142,14 @@ public: /// The point to test. /// When the method completes, contains the closest point between the two objects. static void ClosestPointBoxPoint(const BoundingBox& box, const Vector3& point, Vector3& result); + + /// + /// Determines the closest point between a and a point. + /// + /// The box to test. + /// The point to test. + /// The closest point between the two objects. + static Vector3 ClosestPointBoxPoint(const BoundingBox& box, const Vector3& point); /// /// Determines the closest point between a and a point. @@ -114,6 +158,14 @@ public: /// The point to test. /// When the method completes, contains the closest point between the two objects. static void ClosestPointRectanglePoint(const Rectangle& rect, const Vector2& point, Vector2& result); + + /// + /// Determines the closest point between a and a point. + /// + /// The rectangle to test. + /// The point to test. + /// The closest point between the two objects. + static Vector2 ClosestPointRectanglePoint(const Rectangle& rect, const Vector2& point); /// /// Determines the closest point between a and a point. @@ -122,6 +174,14 @@ public: /// The point to test. /// When the method completes, contains the closest point between the two objects; or, if the point is directly in the center of the sphere, contains . static void ClosestPointSpherePoint(const BoundingSphere& sphere, const Vector3& point, Vector3& result); + + /// + /// Determines the closest point between a and a point. + /// + /// The sphere to test. + /// The point to test. + /// The closest point between the two objects; or, if the point is directly in the center of the sphere, contains . + static Vector3 ClosestPointSpherePoint(const BoundingSphere& sphere, const Vector3& point); /// /// Determines the closest point between a and a . @@ -135,6 +195,19 @@ public: /// intersection. /// static void ClosestPointSphereSphere(const BoundingSphere& sphere1, const BoundingSphere& sphere2, Vector3& result); + + /// + /// Determines the closest point between a and a . + /// + /// The first sphere to test. + /// The second sphere to test. + /// The closest point between the two objects; or, if the point is directly in the center of the sphere, contains . + /// + /// If the two spheres are overlapping, but not directly on top of each other, the closest point + /// is the 'closest' point of intersection. This can also be considered is the deepest point of + /// intersection. + /// + static Vector3 ClosestPointSphereSphere(const BoundingSphere& sphere1, const BoundingSphere& sphere2); /// /// Determines the distance between a and a point.