Below is a function that detects if two circles intersect. I want to change it to only detect if the periferi of the circles intersect. Hence, if circle A is completely inside circle B, there is no collision!
How?
private bool IsCircleCollision(
int x1, int y1, int radius1,
int x2, int y2, int radius2)
{
int dx = x2 - x1;
int dy = y2 - y1;
int distance = (dx * dx) + (dy * dy);
int radii = radius1 + radius2;
if (distance < radii * radii)
{
return true;
}
else
{
return false;
}
}
You work this out by calculating the distance between the two centres, D say. There is an intersection if
where R1 and R2 are the radii of the two circles.
The first test,
abs(R1-R2) < Dhandles the case when one circle’s centre is inside the other’s. And the second test,D < R1+R2, handles the case when neither circle contains the other’s centre.So, adapting your code we have:
If performance is important here, you can do without the call to
Math.Sqrtlike this: