Allegro.cc - Online Community

Allegro.cc Forums » Game Design & Concepts » Best rectangle-circle collision algorithm

This thread is locked; no one can reply to it. rss feed Print
Best rectangle-circle collision algorithm
CursedTyrant
Member #7,080
April 2006
avatar

Basically, I'm coding a 2D platformer engine, since I'm sick and tired of writing one everytime I want to code a new game and I just end up ditching the whole thing. I want to concentrate more on the content.

Anyway, what are the best methods of calculating and handling rectangle to circle collisions? Like, for example, when a giant boulder hits the player. Or something.

---------
Signature.
----
[My Website] | [My YouTube Channel]

james_lohr
Member #1,947
February 2002

Start with linesegment-circle collision. This is done by computing the perpendicular distance from the line to the centre of the circle. You also need to find whether or not the perpendicular line through the circle's centre crosses your linesegment

Once you have this working, rectangle-circle collision is trivial.

Billybob
Member #3,136
January 2003

Once you have this working, rectangle-circle collision is trivial.

Assuming solid shapes, you need to handle the circle being inside the rectangle.

Go check out an open-source 2D physics or collision library. Copy->Paste->Profit.

james_lohr
Member #1,947
February 2002

Billybob said:

Assuming solid shapes, you need to handle the circle being inside the rectangle.

Which is trivial: point inside a polygon.

Arthur Kalliokoski
Second in Command
February 2005
avatar

Billybob said:

Assuming solid shapes, you need to handle the circle being inside the rectangle.

Do you mean the circle skips over the border of the rectangle entirely in one logic loop? Or skips over the entire rectangle during one logic update? It would be better if the location of the circle from one logic loop to the next logic update were defined as a line segment itself, and test for collision between this line segment and the rectangle. I figured this out many years ago when playing Need for Speed and you could run over a spike strip without damage if you were going fast enough.

[EDIT]

This would be helpful as far as simulating the radius for this line segment. Even if the game isn't 3d, you need to see if the mid point of the line connecting the two circles (in time) gets within the radius of a corner of the rectangle.

http://local.wasp.uwa.edu.au/~pbourke/geometry/lineline3d/

They all watch too much MSNBC... they get ideas.

james_lohr
Member #1,947
February 2002

Do you mean the circle skips over the border of the rectangle entirely in one logic loop? Or skips over the entire rectangle during one logic update?

He means exactly what he said: circle fully inside the rectangle and so not intersecting any line segments.

Arthur Kalliokoski
Second in Command
February 2005
avatar

I still stand by what I said regarding the circle completely skipping over the rectangle between logic updates. In the "real world" the circle would have necessarily passed through the rectangle, and been inside at some instant.

They all watch too much MSNBC... they get ideas.

Go to: