Skip to content

Commit

Permalink
fix possible division by zero in Circle
Browse files Browse the repository at this point in the history
  • Loading branch information
NoelFB committed May 21, 2024
1 parent f6f37e3 commit d0012ae
Showing 1 changed file with 11 additions and 2 deletions.
13 changes: 11 additions & 2 deletions Framework/Spatial/Circle.cs
Original file line number Diff line number Diff line change
Expand Up @@ -61,11 +61,20 @@ public readonly bool Overlaps(in Circle other, out Vector2 pushout)
{
pushout = Vector2.Zero;

var combinedRadius = (Radius + other.Radius);
var lengthSqrd = (other.Position - Position).LengthSquared();
if (lengthSqrd < (Radius + other.Radius) * (Radius + other.Radius))

if (lengthSqrd < combinedRadius * combinedRadius)
{
var length = MathF.Sqrt(lengthSqrd);
pushout = ((Position - other.Position) / length) * (Radius + other.Radius - length);

// they overlap exactly, so there is no "direction" to push out of.
// instead just push out along the unit-x vector
if (length <= 0)
pushout = Vector2.UnitX * combinedRadius;
else
pushout = ((Position - other.Position) / length) * (combinedRadius - length);

return true;
}

Expand Down

0 comments on commit d0012ae

Please sign in to comment.