diff --git a/Source/StevesUEHelpers/Private/StevesMathHelpers.cpp b/Source/StevesUEHelpers/Private/StevesMathHelpers.cpp index 43afecf..6331e54 100644 --- a/Source/StevesUEHelpers/Private/StevesMathHelpers.cpp +++ b/Source/StevesUEHelpers/Private/StevesMathHelpers.cpp @@ -50,6 +50,7 @@ float StevesMathHelpers::GetDistanceToConvex2D(const TArray& 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& 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; } - // 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& ConvexPo Dist = FMath::Sqrt(ToPoint.SquaredLength() - FMath::Square(T * Line.Length())); } - ClosestOutside = FMath::Min(ClosestOutside, Dist); + if (DotNormal > 0) + { + ClosestOutside = FMath::Min(ClosestOutside, Dist); + } + else + { + ClosestInside = FMath::Min(ClosestInside, Dist); + } } - return bInside ? 0 : ClosestOutside; + return bInside ? -ClosestInside : ClosestOutside; } diff --git a/Source/StevesUEHelpers/Public/StevesMathHelpers.h b/Source/StevesUEHelpers/Public/StevesMathHelpers.h index 4c93038..2e0cb33 100644 --- a/Source/StevesUEHelpers/Public/StevesMathHelpers.h +++ b/Source/StevesUEHelpers/Public/StevesMathHelpers.h @@ -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& 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& ConvexPoints, const FTransform& ConvexTransform,