Collision between a circle and a rect

Started by
5 comments, last by Supper-Marrio 21 years, 4 months ago
How do i do that??? and if i can get an example please???
Advertisement
The circle is a kind of rectangle. The outher sides of the cirkel are equalivent to a rectangle. So if you imagine your circle as a rectangle. You just have a colission as if you have 2 rectangles.

I hope you understand it. I don''t speak the best English in the world.
but if i do like this, so itsn''t a perfect collision, there is no other way?
first do a test if the circle is completely outside of the rectangle...
Then test if the rectangle is completely outside the circle
Go post this question on the math forum. You will find far better results, as well as posts that already relate to this subject.
Brendan
Brendan"Mathematics is the Queen of the Sciences, and Arithmetic the Queen of Mathematics" -Gauss
Lol... I wrote a REALLY long reply and then i got a 404 error when I wanted to post it... and then it was lost. ARGHRRHGRHGRAHSDGRASH

Bah... Let''s start again. The absolutely easiest situation is when the rectangle cant be rotated, because in that case you only need to find the closest point on the edge of the rectangle, such as this:


  int closestx = circlex;int closesty = circley;if (closestx < rect.left){   closestx = rect.left;}else if (closestx >= rect.right){   // This assumes that the rectangle doesn''t include the right   // and bottom edges, which is standard.   closestx = rect.right - 1;}if (closesty < rect.top){   closesty = rect.top;}else if (closesty >= rect.bottom ){   // This assumes that the rectangle doesn''t include the right   // and bottom edges, which is standard.   closesty = rect.bottom - 1;}// if the circle was inside the rectangle the closest position// will not have changedif ((closestx == circlex) && (closesty == circley)){   return true;}// now get the distance between the closest point on the// rectangle edge and the circle''s position...int deltax = circlex - closestx;int deltay = circley - closesty;double distance = sqrt((deltax * deltax) + (deltay * deltay));// return true if the distance is smaller than the radius// of the circlereturn (distance < circleradius);  


Note that this doesn''t work if the rectangle can be rotated. If you need it to work for boxes in 3D you just have to add a Z component. Also note that this function can be speed up by removing the square root and replacing it with:

return (((deltax * deltax) + (deltay * deltay)) < (circleradius * circleradius));

I hope I can post a goddamn reply this time!


My Stuff : [ Whispers in Akarra (online rpg) || L33T WAR (multiplayer game) || The Asteroid Menace (another game) ]

My Stuff : [ Whispers in Akarra (online rpg) || L33T WAR (multiplayer game) || The Asteroid Menace (another game) ]

This topic is closed to new replies.

Advertisement