Support negative results from GetDistanceToConvex2D

This commit is contained in:
Steve Streeting 2023-11-27 17:13:10 +00:00
parent 8d0fb472f2
commit dad1fde054
2 changed files with 15 additions and 9 deletions

View File

@ -50,6 +50,7 @@ float StevesMathHelpers::GetDistanceToConvex2D(const TArray<FVector2f>& ConvexPo
// Assume inside until 1 or more tests show it's outside
bool bInside = true;
float ClosestOutside = 1e30f;
float ClosestInside = 1e30f;
const int N = ConvexPoints.Num();
for (int i = 0; i < N; ++i)
{
@ -66,13 +67,11 @@ float StevesMathHelpers::GetDistanceToConvex2D(const TArray<FVector2f>& ConvexPo
const FVector2f ToPoint = P - ConvexPoints[i];
const float DotNormal = Normal.Dot(ToPoint);
if (DotNormal < 0)
if (DotNormal > 0)
{
// Inside
continue;
}
// If >0 result is outside, point must be outside
bInside = false;
}
// Do a perpendicular projection onto the line segment to see if we're within the limits of it
const float DotLine = Line.Dot(ToPoint);
@ -95,8 +94,15 @@ float StevesMathHelpers::GetDistanceToConvex2D(const TArray<FVector2f>& ConvexPo
Dist = FMath::Sqrt(ToPoint.SquaredLength() - FMath::Square(T * Line.Length()));
}
if (DotNormal > 0)
{
ClosestOutside = FMath::Min(ClosestOutside, Dist);
}
else
{
ClosestInside = FMath::Min(ClosestInside, Dist);
}
}
return bInside ? 0 : ClosestOutside;
return bInside ? -ClosestInside : ClosestOutside;
}

View File

@ -103,7 +103,7 @@ public:
* Return the distance to a convex polygon in 2D where points are in the same space
* @param ConvexPoints Points on the convex polygon, anti-clockwise order, in a chosen space
* @param LocalPoint Point to test, in same space as convex points
* @return The distance to this convex polygon in 2D space. == 0 if inside
* @return The distance to this convex polygon in 2D space. <= 0 if inside
*/
static float GetDistanceToConvex2D(const TArray<FVector2f>& ConvexPoints,
const FVector& LocalPoint);
@ -112,7 +112,7 @@ public:
* @param ConvexPoints Points on the convex polygon, anti-clockwise order, in local space
* @param ConvexTransform World transform for convex polygon
* @param WorldPoint Point in world space
* @return The distance to this convex polygon in 2D space. == 0 if inside
* @return The distance to this convex polygon in 2D space. <= 0 if inside
*/
static float GetDistanceToConvex2D(const TArray<FVector2f>& ConvexPoints,
const FTransform& ConvexTransform,