diff --git a/Source/Engine/Core/Math/CollisionsHelper.cs b/Source/Engine/Core/Math/CollisionsHelper.cs index d6e0392c8..7d85d2407 100644 --- a/Source/Engine/Core/Math/CollisionsHelper.cs +++ b/Source/Engine/Core/Math/CollisionsHelper.cs @@ -163,6 +163,38 @@ namespace FlaxEngine } } + /// + /// 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. + public static void ClosestPointPointLine(ref Vector3 point, ref Vector3 p0, ref Vector3 p1, out Vector3 result) + { + Vector3 p = point - p0; + Vector3 n = p1 - p0; + float length = n.Length; + if (length < 1e-10f) + { + result = p0; + return; + } + n /= length; + float dot = Vector3.Dot(ref n, ref p); + if (dot <= 0.0f) + { + result = p0; + return; + } + if (dot >= length) + { + result = p1; + return; + } + result = p0 + n * dot; + } + /// /// Determines the closest point between a point and a triangle. ///