Sign in to follow this  

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.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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 this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
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 this post


Link to post
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 this post


Link to post
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.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this