fix(util): correct AABB overlap test in MathUtil.HasOverlap
The previous implementation nested a ValueInRange helper that used strict inequalities at both ends. That dropped identical rectangles and shared-edge cases as false negatives — two rectangles with a.X == b.X would miss the overlap on the X axis even when they clearly overlapped. Replaced with the standard AABB test: rectangles overlap iff they overlap on both axes (a.Min < b.Max && a.Max > b.Min per axis). Pre-existing upstream issue (CodeRabbit critical finding); fixed in v1.0.0 standalone cut where we own the codebase.
This commit is contained in:
@@ -31,7 +31,6 @@ public static class MathUtil
|
|||||||
=> $"X: {X} Y: {Y} Width: {Width} Height: {Height}";
|
=> $"X: {X} Y: {Y} Width: {Width} Height: {Height}";
|
||||||
}
|
}
|
||||||
|
|
||||||
// From: https://stackoverflow.com/a/306379
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Checks if two rectangles overlap at any point.
|
/// Checks if two rectangles overlap at any point.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
@@ -40,12 +39,11 @@ public static class MathUtil
|
|||||||
/// <returns>True if overlapping</returns>
|
/// <returns>True if overlapping</returns>
|
||||||
public static bool HasOverlap(this Rectangle a, Rectangle b)
|
public static bool HasOverlap(this Rectangle a, Rectangle b)
|
||||||
{
|
{
|
||||||
bool ValueInRange(int value, int min, int max)
|
// Standard AABB overlap test: two rectangles overlap iff they
|
||||||
=> value > min && value < max;
|
// overlap on both axes. The previous nested ValueInRange approach
|
||||||
|
// used strict inequalities at both ends, which dropped identical
|
||||||
var xOverlap = ValueInRange(a.X, b.X, b.X + b.Width) || ValueInRange(b.X, a.X, a.X + a.Width);
|
// rectangles and shared-edge cases as false negatives.
|
||||||
var yOverlap = ValueInRange(a.Y, b.Y, b.Y + b.Height) || ValueInRange(b.Y, a.Y, a.Y + a.Height);
|
return a.X < b.X + b.Width && a.X + a.Width > b.X &&
|
||||||
|
a.Y < b.Y + b.Height && a.Y + a.Height > b.Y;
|
||||||
return xOverlap && yOverlap;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user