Jump to content
  • Advertisement

Archived

This topic is now archived and is closed to further replies.

Collision between a circle and a rect

This topic is 5646 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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.

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
first do a test if the circle is completely outside of the rectangle...

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
Then test if the rectangle is completely outside the circle

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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 changed

if ((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 circle

return (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) ]

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!