Collision detection of cicle and rectangle [closed]

How to find the collision detection of a moving rectangle on circular object? Here i have tried to junp rectangular object but the collision detection of a small rectangular object while jumping doesnt have smooth collision detection? Can anyone please help me ?

closed as unclear what you're asking by Seth Battin, Anko, Kromster, bummzack, congusbongusFeb 12 '15 at 6:33

Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.

• Can you elaborate about the problem, maybe with pictures? It's also unclear what you tried and why it didn't work. You might want to consult help/how-to-ask in order to improve your question so that you can get helpful answers. – Seth Battin Feb 6 '15 at 6:25

As with anything in programming, there's multiple ways to do this. I'm not sure if what I would do is the best/fastest way, but it's a way to get the job done:

First, I would check if the circle and the rectangle are remotely close to eachother by checking
if (vector2.distancesquared(circle.centerpoint,new vector2(rectangle.x,rectangle.y)) <= circle.radius^2 + rectangle.width^2 + rectangle.height^2)

This simply checks if the distance between the centerpoint of the circle and the top left point of the rectangle (you could make it any point in the rectangle, doesn't matter), is smaller than or equal to the maximum distance between those two at collision. Notice I use distancesquared instead of just distance. This is because you don't want to get the square root of anything you don't need. It's a calculation that takes a lot of time and should be avoided when making something that's supposed to run smoothly (like a game). Also, I don't know what kind of circle class you're using, but I'm assuming it has some kind of centerpoint property. If it doesn't, you should still be able to find a way of getting the centerpoint.

After that, I would, for every point in the rectangle, check if it's in the circle: if (vector2.distancesquared(circle.centerpoint, new vector2(rectangle.x,rectangle.y) <= circle.radius^2)

This code only checks the top left corner, but you get the idea. If any of these points are in the circle, you have a collision. But be aware that if none of these points are on the circle, you might still have a collision. It could be that the circle is entirely inside the rectangle, or intersects with one of its sides. To check this, do the following:

if((rectangle.x + rectangle.width >= circle.centerpoint.x - circle.radius) && (rectangle.x <= circle.centerpoint.x - circle.radius))

As you can see, this checks if the right side of the rectangle lies to the right of the leftmost point of the circle, while the left side of the rectangle lies to the left of that point. If this is the case, check if that point on the circle is also between the top and bottom sides. Once you've done that, you've still only checked if it collides with the right side of the rectangle. Do this for all sides and you're done.

I deliberately didn't just fork over code and let that be it. I gave you an idea of how I would handle this situation. Again, it may not be the best way to do this, but I find it to be very readable. Try to finish the code on your own. If you can't seem to figure it out, ask away.

Depending on what you meant, try this:

Method 1: (if you are indeed working in 2D)
-Drawn from any angle, a BoundingSphere is a circle
-A BoundingBox with "0" height is a rectangle
-After constructing the two, use the .intersects method.

Method 2: (un-confirmed/tested)
-Bright red circles are circle(s) to be tested against the rectangle.
-Blue arrows represent CenterToCenterDirection
-Translate the circle(s)' center point(s) by (CenterToCenterDirection * radius)
-Test the new point against the rectangle using the .Contains method • Peethor is testing the rectangle in "circle-space" while I am testing the circles in "rectangle-space". I believe using the BoundingXXX classes will be easier, if not also faster. – Jon Feb 6 '15 at 6:48