Added 3D version of CollisionsHelper::ClosestPointPointLine().
This commit is contained in:
@@ -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)
|
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
|
// Source: Real-Time Collision Detection by Christer Ericson
|
||||||
|
|||||||
@@ -72,6 +72,15 @@ public:
|
|||||||
/// <param name="result">When the method completes, contains the closest point between the two objects.</param>
|
/// <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);
|
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>
|
/// <summary>
|
||||||
/// Determines the closest point between a point and a triangle.
|
/// Determines the closest point between a point and a triangle.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
|
|||||||
Reference in New Issue
Block a user