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:

28 thoughts on “Rectangle-circle intersection test

  1. What is rectX in this context? Is it the centre of the rectangle, or is it the bottom left corner?

    • DeltaX = CircleX - Max(RectX - RectHalfWidth, Min(CircleX, RectX + RectHalfWidth));
      DeltaY = CircleY - Max(RectY - RectHalfHeight, Min(CircleY, RectY + RectHalfHeight));
      return (DeltaX * DeltaX + DeltaY * DeltaY) < (CircleRadius * CircleRadius);
  2. Your thoughts on this?

    > Fastest way is to :
    – A) test circle against rect’s outer bounding circle -> reject if too far.
    – B) test circle against rect’s inner bounding circle -> accept if near enough.
    – C) test that the outer point of the circle (on the line joining both centers) is in the AABB.

    • Testing for outer point of the circle will likely cost you more cycles than this entire approach, unless you can find a way to find that point without vector normalization/trigonometry.

  3. Your algorithm may be incomplete. what if the circle is completely inside the rectangle? circle does not intersect with the sides but it intersetcs inner of the rectangle

    • That is of no concern, as if the circle is inside the rectangle, the nearest point of the rectangle is in the middle of the circle. This algorithm does not make use of edge intersection checks at all.

    • 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.

  4. 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

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

  6. 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.