Public Group

# Circle-Box Collision Detection

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

## 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 on other sites
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 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 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

This can be simplified to:

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.

{
CheckForCollision(ball, block);
}

where:

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

• ### What is your GameDev Story?

In 2019 we are celebrating 20 years of GameDev.net! Share your GameDev Story with us.

• 10
• 11
• 13
• 9
• 11
• ### Forum Statistics

• Total Topics
634092
• Total Posts
3015448
×