diff --git a/src/GameUtils/Types/Geometry/AABB.cs b/src/GameUtils/Types/Geometry/AABB.cs index 96a3c57..35e1d4b 100644 --- a/src/GameUtils/Types/Geometry/AABB.cs +++ b/src/GameUtils/Types/Geometry/AABB.cs @@ -150,42 +150,19 @@ public bool Intersects(Polygon2D polygon) { if (!Intersects(polygon.BoundingBox)) return false; - // 1. Fast bounds check: compute polygon bounds on the fly - if (polygon.Vertices.Length > 0) - { - var pMinX = polygon.Vertices[0].X; - var pMaxX = polygon.Vertices[0].X; - var pMinY = polygon.Vertices[0].Y; - var pMaxY = polygon.Vertices[0].Y; - - for (int i = 1; i < polygon.Vertices.Length; i++) - { - var v = polygon.Vertices[i]; - if (v.X < pMinX) pMinX = v.X; - if (v.X > pMaxX) pMaxX = v.X; - if (v.Y < pMinY) pMinY = v.Y; - if (v.Y > pMaxY) pMaxY = v.Y; - } - - if (pMinX > Max.X || pMaxX < Min.X || pMinY > Max.Y || pMaxY < Min.Y) - { - return false; - } - } - - // 2. Check if any polygon vertex is inside the AABB + // 1. Check if any polygon vertex is inside the AABB foreach (var v in polygon.Vertices) { if (Contains(v)) return true; } - // 3. Check if any polygon edge intersects the AABB + // 2. Check if any polygon edge intersects the AABB foreach (var e in polygon.Edges) { if (Intersects(e)) return true; } - // 4. Check if the AABB is completely inside the polygon + // 3. Check if the AABB is completely inside the polygon return polygon.Contains(Center); } #pragma warning restore S3267 @@ -213,7 +190,9 @@ public bool Intersects(Vector2 start, Vector2 end) /// public bool Intersects(Vector2 center, float radius) { - return Intersects(new Circle(center, radius)); + var closest = Vector2.Clamp(center, Min, Max); + var distance = Vector2.DistanceSquared(center, closest); + return distance <= radius * radius; } ///