# collision with circles and squares

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

## Recommended Posts

What is the fastest algorithm for collision tests with circles against squares (or vice versa)?

##### Share on other sites
Depends on if you need a discrete algorithm or a continuous algorithm. What's the context?

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

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

##### Share on other sites
Quote:
 Original post by EvilKnuckles666what 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.

##### Share on other sites
what about when the circle intersects just a line of a rectange but not any of the edges?

##### Share on other sites
Quote:
Original post by jyk
Quote:
 Original post by EvilKnuckles666what 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.

##### Share on other sites

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.

##### Share on other sites
for step one, is there a single equation i can use?

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

##### Share on other sites
If this is a 2d Square vs 2d circle test, then just do 4 point-segment distance checks.

Check the center of the circle against each segment of the box. If the distance is ever less than the Radius, then they touch.

http://astronomy.swin.edu.au/~pbourke/geometry/pointline/
http://www.geometrictools.com/Documentation/DistancePointLine.pdf

##### Share on other sites

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

## Create an account

Register a new account

• ### Forum Statistics

• Total Topics
628736
• Total Posts
2984456

• 25
• 11
• 10
• 16
• 14