circle to rectangle collision detection

Started by
2 comments, last by zoomcrypt 21 years, 12 months ago
so check this out: http://www.hero-interactive.com/images/gallery/robotech3d/c-gunlaser.jpg I''ve got these giant lasers shooting out of my mechs but now i have to do a different type of collision detection. up to this point we''ve been doing circle-circle collision detection which is easy enough, but now i have to do rectangle-circle collision detection. Anyone have some urls or points to where i can get good info on how to do this? SkullOne - Hero Interactive http://www.hero-interactive.com
SkullOne - Hero Interactivehttp://www.hero-interactive.com
Advertisement
Like so:

first assume the rectangle is axis aligned and centred on the origin. If it isn''t then you can apply a rotation and translation to get it there, so work these out and apply them to the coordinates of the circle.

The rectangle now is defined by

-a < x < a
-b < y < b

How you proceed depends on where the centre of the circle is. If the circle is at (X, Y) and has radius r then there are four possibilities

1) if

-a < X < a and -b < Y < b

the circle centre is inside the rectangle, and so it is/has collided

2) if
-a < X < a

the circle is above or below the rectangle (assuming the x axis is horizontal). Then it is nearest the top or bottom edge, and so collides if

Y < b + r or Y > -b - r

3) if
-b < Y < b

the circle is to the side of the rectangle, is nearest the left or right edge, and so collides if

X < a + r or X > -a - r

4) finally if none of the above are true it is nearest a corner. Which one depends on the sign of X and Y; e.g. if X and Y are both positive it is nearest (a, b), and so it collides if

(X - a)^2 + (Y - b)^2 < r^2

The above is based on the geometry of the Voronoi regions of the rectangle - worth mentioning as Voronoi regions can be generated for shapes other than rectangles and in 3D as well as 2D, giving a straightforward way to generalise this to other shapes and higher dimensions.
John BlackburneProgrammer, The Pitbull Syndicate
hey i like that . very cool.
I''ll be back to ask about 3d box collision detectoin soon i''m sure, what''s a good reference for all things collision detection?

SkullOne - Hero Interactive
http://www.hero-interactive.com
SkullOne - Hero Interactivehttp://www.hero-interactive.com
> what''s a good reference for all things collision detection?

Best I''ve found is:

http://www.ams.sunysb.edu/~jklosow/quickcd/QCD_resources.html

You can also look in the archives here, or just search the web, as a lot of this material is published online.
John BlackburneProgrammer, The Pitbull Syndicate

This topic is closed to new replies.

Advertisement