Rectangle-circle intersection test

Rectangle-circle intersection test demo

This is a blog post about handling circle-rectangle collisions. For some reason, these seem to be generally regarded as something complicated, even though they aren't.

First things first, you may already know how to check circle-point collision - it's simply checking that the distance between the circle' center and the point is smaller than the circle' radius:

DeltaX = CircleX - PointX;
DeltaY = CircleY - PointY;
return (DeltaX * DeltaX + DeltaY * DeltaY) < (CircleRadius * CircleRadius);

Surprisingly or not, rectangle-circle collisions are not all too different - first you find the point of rectangle that is the closest to the circle' center, and check that point is in the circle.

And, if the rectangle is not rotated, finding a point closest to the circle' center is simply a matter of clamping the circle' center coordinates to rectangle coordinates:

NearestX = Max(RectX, Min(CircleX, RectX + RectWidth));
NearestY = Max(RectY, Min(CircleY, RectY + RectHeight));

So, combining the above two snippets yields you a 3-line function for circle-rectangle check:

DeltaX = CircleX - Max(RectX, Min(CircleX, RectX + RectWidth));
DeltaY = CircleY - Max(RectY, Min(CircleY, RectY + RectHeight));
return (DeltaX * DeltaX + DeltaY * DeltaY) < (CircleRadius * CircleRadius);

And here's it in action, along with a bit of debug drawing:

Click and drag elements.

And that is it. I've told you that it really isn't complicated, didn't I?

Bonus: Visualization ported to GameMaker

Related posts:

19 thoughts on “Rectangle-circle intersection test

    • Pixon, you can translate rotated coordinates to unrotated version:

      unrotatedCircleX = cos(rotation) * ( circle.x – rectCenterX ) – sin( rotation ) * ( circle.y – rectCenterY ) + rectCenterX;
      unrotatedCircleY = sin( rotation ) * ( circle.x – rectCenterX ) + cos( rotation ) * ( circle.y – rectCenterY ) + rectCenterY;

    • Top-left, assuming that +x is to the right and +y is downwards.

      If your coordinate system assumes +y to be upwards, it would be bottom-left instead.

  1. Hey, so for this what does Max mean, which one of the two values is bigger, or something else?

    I moved the code into java and it doesn’t work properly…

    • Indeed – the bigger of two arguments, not too unlike an extra line with an if-branch.

      I think the only things to watch for are:
      – that both objects are in same coordinate space
      – that you are passing (x, y, width, height) for rectangle rather than (x1, y1, x2, y2)
      – that circle radius is not negative

  2. This is way to simple ^^ its something i have been thinking about for some time, but this is really qute nice :)

  3. Hi there! I’ve been searching for ages for this! I’m not really a math person so even if this looked really simple, I can’t seem to understand how to apply this into my code. Are you willing to share the project file with me? I need to also see the debug drawing to understand it.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.