Archived

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

Collision between a circle and a rect

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

Recommended Posts

How do i do that??? and if i can get an example please???

Share on other sites
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 on other sites
but if i do like this, so itsn''t a perfect collision, there is no other way?

Share on other sites
first do a test if the circle is completely outside of the rectangle...

Share on other sites
Then test if the rectangle is completely outside the circle

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 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

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

1. 1
Rutin
38
2. 2
3. 3
4. 4
5. 5

• 11
• 9
• 12
• 14
• 9
• Forum Statistics

• Total Topics
633350
• Total Posts
3011470
• Who's Online (See full list)

There are no registered users currently online

×