Jump to content
  • Advertisement
Sign in to follow this  
Winegums

Circle-Box Collision Detection

This topic is 4281 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

Hi, writeing a breakout game but having a problem with the collision detection and responce. Basically I need a method that can tell me both if there has been a collision and where on the box (left, right, top bottom) the collision is. the responces are the easy bit, but I'm having trouble finding information pertaining to how to do the test. i have the position of both objects, the circles radius, and the coordinates of the corners of the quad.

Share this post


Link to post
Share on other sites
Advertisement
For a simple breakout game, using a bounding box for the ball and doing a simple box-box test should be enough. If you still want to do it your way, you can find some info here: www.realtimerendering.com.
The above contains info on 3D intersection tests but it shouldn't be too difficult to apply it to 2D.

Share this post


Link to post
Share on other sites
Using a box for the ball would make things easier. However, if you really want to do circle vs box, you could:

- create a set of 2D planes representing each side of the box
- test the distance of the center of the circle from the plane. if -radius <= distance <= radius, it's intersecting. if distance <= -radius, it's completely behind. if distance > radius, it's completely in front.
- if the circle is completely in front of one or more of the planes, it's not colliding with the box.

Share this post


Link to post
Share on other sites
box-box collision is not necessarily sufficient, depending on how Winegums want's to handle the case where the ball hits an exposed corner of a block.

Basically what you want to do is immagine the block as something like the center of a tic-tac-toe board with rounded-off corners. The distance to the outsize edges and the radius of the corners is equal to the radius of the ball.

This region is the collision region of the block. For the flat parts, simply reflect the ball about the normal of that face of the block. For the Corners, the normal is the ray from the corner which was collided with to the center of the ball at the moment of impact.

Check the flat sides of potential collision blocks first, as this will solve the case where the corner is not exposed due to an adjacent block.

As an optimization, do not check all blocks for collision, you can do a simple reject test by using an over-sized-bounding circle around your ball. This bounding circle is given by:

bounding_radius = r + sqrt((w/2)^2 + (h/2)^2)

Where:
w = width of blocks
h = height of blocks
r = radius of ball

This can be simplified to:

bounding_radius = r + sqrt((w*w+h*h)/4)

If the center of the distance between the center of a block and the center of a ball is *greater than* the bounding_radius, the block and ball do *not* collide. Otherwise, this block should be tested against for collision.

As Always, because you're only worried about comparitive distance, rather than exact distance, you can leave the sqrt() off both sides for faster results.

if(DistanceSquared(ball, block) <= bounding_radius_squared)
{
CheckForCollision(ball, block);
}

where:
bounding_radius_squared = r*r + (w*w+h*h)/4


[EDIT]

The original algorithm I gave for the radius of the over-sized bounding circle was over-complicated and larger than neccesary. I've updated the post ot have the correct, more efficient formula.

[Edited by - Ravyne on January 26, 2007 4:36:00 PM]

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

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

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!