Circle Collisions

BlitzMax Forums/BlitzMax Beginners Area/Circle Collisions

(tu) ENAY(Posted 2005) [#1]
Hi,

I'm trying to make a Rect (Non rotating) to Circle collision function but I'm having a bit of difficulty getting it to work properly.

I've got a Circle 2 Circle, Rect to Rect and Point to Circle working ok, but I just can't seem to figure out a good way of doing Rect to circle.
At the moment I have a function that checks all 4 four corners of the square and checks Point To Circle style with this against all 4 corners.

But this doesn't work properly at all with lengthy rectangles and small circles or very large circles and small rects.

Basically what I'm trying to achieve is something like this:-

Circle2Rect(C_X, C_Y, C_RADIUS, R_X, R_Y, R_WIDTH, R_HEIGHT)

Anyone want to help and put me out of my misery? ;)


Tom Darby(Posted 2005) [#2]
Not the optimal solution below, but it should work:

1. Check Rect to Rect. If no collision, return False. (Circle-Rectangle collision will be a subset of Rectangle-Rectangle collision.)
2. Check To see if the circle's x origin falls between the x bounds of the rect. If yes, return True. (If there was a rect-rect collision, then you'll be sure that there's a collision if the x value for the center of the circle falls between the x bounds of the rectangle; the only time there might -not- be a collision is if the circle's rectangle intersects a corner, but the circle itself doesn't.)
3. Repeat 2, except for y.
4. Check the "four points to circle" collision. Return the result.


tonyg(Posted 2005) [#3]
If the distance between the centrepoint of circle1 and the centrepoint of circle2 is less than the radius of Circle1 + radius of circle2 then there's been a collision.


FlameDuck(Posted 2005) [#4]
Or to answer the question you're asking: http://vband3d.tripod.com/visualbasic/tut_mixedcollisions.htm


(tu) ENAY(Posted 2005) [#5]
/me pushes everyone to the floor and gives them a big hug.


Tom Darby(Posted 2005) [#6]
Glad to be of help!