public override bool Intersects(CollisionCircle circle) { float dx = circle.GetCenter().X - GetCenter().X; float dy = circle.GetCenter().Y - GetCenter().Y; float radii = GetRadius() + circle.GetRadius(); return((dx * dx) + (dy * dy) < radii * radii); }
public override bool Intersects(CollisionCircle circle) { var radiusSquared = circle.GetRadius() * circle.GetRadius(); var vertex = GetWorldPosition(Vertices[Vertices.Count - 1]); var circleCenter = circle.GetCenter(); var nearestDistance = float.MaxValue; var nearestIsInside = false; var nearestVertex = -1; var lastIsInside = false; for (var i = 0; i < Vertices.Count; i++) { var nextVertex = GetWorldPosition(Vertices[i]); var axis = circleCenter - vertex; var distance = axis.LengthSquared() - radiusSquared; if (distance <= 0) { return(true); } var isInside = false; var edge = nextVertex - vertex; var edgeLengthSquared = edge.LengthSquared(); if (!edgeLengthSquared.Equals(0)) { // Check whether the circle overlap an edge of the polyhon var dot = Vector2.Dot(edge, axis); if (dot >= 0 && dot <= edgeLengthSquared) { var projection = vertex + (dot / edgeLengthSquared) * edge; axis = projection - circleCenter; if (axis.LengthSquared() <= radiusSquared) { return(true); } if (edge.X > 0) { if (axis.Y > 0) { return(false); } } else if (edge.X < 0) { if (axis.Y < 0) { return(false); } } else if (edge.Y > 0) { if (axis.X < 0) { return(false); } } else if (edge.Y < 0) { if (axis.X > 0) { return(false); } } else { isInside = true; } } } if (distance < nearestDistance) { nearestDistance = distance; nearestIsInside = isInside; nearestVertex = i; } vertex = nextVertex; lastIsInside = isInside; } if (nearestVertex == 0) { return(nearestIsInside || lastIsInside); } return(nearestIsInside); }