circle - rectangle collision in 2D, most efficient way

Posted by john smith on Game Development See other posts from Game Development or by john smith
Published on 2012-12-04T09:48:40Z Indexed on 2012/12/04 11:31 UTC
Read the original article Hit count: 308

Filed under:

Suppose I have a circle intersecting a rectangle, what is ideally the least cpu intensive way between the two?

  1. method A

    1. calculate rectangle boundaries
    2. loop through all points of the circle and, for each of those, check if inside the rect.
  2. method B

    1. calculate rectangle boundaries
    2. check where the center of the circle is, compared to the rectangle
    3. make 9 switch/case statements for the following positions:

      • top, bottom, left, right
      • top left, top right, bottom left, bottom right
      • inside rectangle
    4. check only one distance using the circle's radius depending on where the circle happens t be.

I know there are other ways that are definitely better than these two, and if could point me a link to them, would be great but, exactly between those two, which one would you consider to be better, regarding both performance and quality/precision?

Thanks in advance.

© Game Development or respective owner

Related posts about collision-detection