Added 3D version of CollisionsHelper::ClosestPointPointLine().

This commit is contained in:
intolerantape
2021-01-19 23:09:23 -08:00
parent 049450e31b
commit f3f25836e2
2 changed files with 31 additions and 0 deletions

View File

@@ -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

View File

@@ -72,6 +72,15 @@ public:
/// <param name="result">When the method completes, contains the closest point between the two objects.</param>
static void ClosestPointPointLine(const Vector2& point, const Vector2& p0, const Vector2& p1, Vector2& result);
/// <summary>
/// Determines the closest point between a point and a line.
/// </summary>
/// <param name="point">The point to test.</param>
/// <param name="p0">The line first point.</param>
/// <param name="p1">The line second point.</param>
/// <param name="result">When the method completes, contains the closest point between the two objects.</param>
static Vector3 ClosestPointPointLine(const Vector3& point, const Vector3& p0, const Vector3& p1);
/// <summary>
/// Determines the closest point between a point and a triangle.
/// </summary>