collision with circles and squares

Started by
9 comments, last by WarAmp 17 years, 8 months ago
What is the fastest algorithm for collision tests with circles against squares (or vice versa)?
-------------------------Unless specified otherwise, my questions pertain:Windows Platform (with the mindset to keep things multi-platform as possible)C++Visual Studio 2008OpenGL with SFML
Advertisement
Depends on if you need a discrete algorithm or a continuous algorithm. What's the context?
what do you mean discrete or continuous? I just pretty much need an equation(s) to figure out if a circle is within a box or a box is within a cirlce.
-------------------------Unless specified otherwise, my questions pertain:Windows Platform (with the mindset to keep things multi-platform as possible)C++Visual Studio 2008OpenGL with SFML
well for if a box is in a circle you could check whether each vertex of the box is within the radius of the cirlce:

get the position of the corner/vertex on the box
get the position of the center of the circle
find the distance between them
if the distance is <= the circle's radius, you are in the circle
else you are outside the circle
Quote:Original post by EvilKnuckles666
what do you mean discrete or continuous? I just pretty much need an equation(s) to figure out if a circle is within a box or a box is within a cirlce.
The question of whether a box is in a circle or vice versa is a containment test, not an intersection test.

In other words, you need to be more specific. What are these objects? How fast are they moving? Is this for a GUI? 2D game? Do you actually need a containment test or just an intersection test? Boolean only? Points of intersection? Feedback for a physics sim?

This may seem like a lot of info to have to provide, but there are a lot of different collision detection algorithms, and it's difficult to recommend one in particular without an understanding of the context.
what about when the circle intersects just a line of a rectange but not any of the edges?
-------------------------Unless specified otherwise, my questions pertain:Windows Platform (with the mindset to keep things multi-platform as possible)C++Visual Studio 2008OpenGL with SFML
Quote:Original post by jyk
Quote:Original post by EvilKnuckles666
what do you mean discrete or continuous? I just pretty much need an equation(s) to figure out if a circle is within a box or a box is within a cirlce.
The question of whether a box is in a circle or vice versa is a containment test, not an intersection test.

In other words, you need to be more specific. What are these objects? How fast are they moving? Is this for a GUI? 2D game? Do you actually need a containment test or just an intersection test? Boolean only? Points of intersection? Feedback for a physics sim?

This may seem like a lot of info to have to provide, but there are a lot of different collision detection algorithms, and it's difficult to recommend one in particular without an understanding of the context.


For now, this won't have any physics, but what i'm looking for is an intersection test. is the circle intersecting any of the lines of the rectangle or is the rectangles lines passing through the circle? They do have a velocity but I store that in the collision object as a vector of motion from the objects last point to it currentpoint and if the intersection is there I want to move the object testing back along that vector until the 2 objects are no longer intersecting for a sticking effect (or move the object to a tangent of a line for a sliding effect). that example supercat1 is great, but it's missing if the circle is intersecting only a line and not necessarily a corner of the rectangle. Once I know that, I will move the object according, but I know how i want to handle that, i just need to figure out how to test if they are intersecting.
-------------------------Unless specified otherwise, my questions pertain:Windows Platform (with the mindset to keep things multi-platform as possible)C++Visual Studio 2008OpenGL with SFML
The short answer is:

1. Find the closest point on the rectangle to the circle center
2. Use the (squared) length of the vector to determine whether the two objects are intersecting
3. If so, use the vector, the length, and the circle radius to move the circle and resolve the intersection

Ask if you need further details on any of these steps.
for step one, is there a single equation i can use?
-------------------------Unless specified otherwise, my questions pertain:Windows Platform (with the mindset to keep things multi-platform as possible)C++Visual Studio 2008OpenGL with SFML
To check if a circle and square collide first check if any of the squares points are inside the circle. If not then get the 4 points on the circle that are aligned with each surface normal(the vector from the center to the point on the outside of the circle has the same direction as a surface normal of the square) and check if those points are inside the square. If your square is axis aligned this is as easy as checking (centerx-radius,centery),(centerx+radius,centery), (centerx,centery-radius),(centerx,centery+radius) to see if any one of them is inside the square. I used this method when I did not need to know precise collission data so I am not certain how to get the exact projection vector(estimations can be found using the points listed above), but it seems to work well in cases where rotation is not present.

This topic is closed to new replies.

Advertisement