Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 6 additions & 27 deletions src/GameUtils/Types/Geometry/AABB.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -213,7 +190,9 @@ public bool Intersects(Vector2 start, Vector2 end)
/// </summary>
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;
}

/// <summary>
Expand Down
Loading