private bool CircleBoxCollision(Vector2 circleCenter, int circleRadius, Vector2 boxUpperLeft, Vector2 boxLowerRight)
{
float dX;
float dY;
float dist;
if (circleCenter.X > boxLowerRight.X)
{
if (circleCenter.Y > boxLowerRight.Y)
{
dX = circleCenter.X - boxLowerRight.X;
dY = circleCenter.Y - boxLowerRight.Y;
dist = (dX * dX) + (dY * dY);
return dist <= circleRadius * circleRadius;
}
else if (circleCenter.Y < boxUpperLeft.Y)
{
dX = circleCenter.X - boxLowerRight.X;
dY = circleCenter.Y - boxUpperLeft.Y;
dist = (dX * dX) + (dY * dY);
return dist <= circleRadius * circleRadius;
}
else
{
return circleCenter.X <= boxLowerRight.X + circleRadius;
}
}
else if (circleCenter.X < boxUpperLeft.X)
{
if (circleCenter.Y > boxLowerRight.Y)
{
dX = circleCenter.X - boxUpperLeft.X;
dY = circleCenter.Y - boxLowerRight.Y;
dist = (dX * dX) + (dY * dY);
return dist <= circleRadius * circleRadius;
}
else if (circleCenter.Y < boxUpperLeft.Y)
{
dX = circleCenter.X - boxUpperLeft.X;
dY = circleCenter.Y - boxUpperLeft.Y;
dist = (dX * dX) + (dY * dY);
return dist <= circleRadius * circleRadius;
}
else
{
return circleCenter.X >= boxUpperLeft.X - circleRadius;
}
}
else if (circleCenter.Y > boxLowerRight.Y)
{
return circleCenter.Y <= boxLowerRight.Y + circleRadius;
}
else if (circleCenter.Y < boxUpperLeft.Y)
{
return circleCenter.Y >= boxUpperLeft.Y - circleRadius;
}
else
{
// in box
return true;
}
}