# equation for point of collision box-circle?

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

## Recommended Posts

hi all, Im just after an equation that will give me the point of contact for a 2d circle and a 2d bounding box. Ive got code to determine there is a collision using SAT tests but now im after the point of contact. Thanks

##### Share on other sites
Quote:
 Original post by Helo7777hi all, Im just after an equation that will give me the point of contact for a 2d circle and a 2d bounding box. Ive got code to determine there is a collision using SAT tests but now im after the point of contact.
The point of contact between a circle and a box that are just touching is the closest point on the box to the circle center.

The closest point on an axis-aligned box to a point can be computed as follows:
closest = vector2(    clamp(query_point.x, min.x, max.x),    clamp(query_point.y, min.y, max.y));
The equivalent code for an oriented box is similar but has a couple of extra steps.

If you're performing a discrete test, then presumably you are resolving any intersection by pushing one or both objects along the minimum translational vector. After this they will be just touching, and the above method can be used.

If you're performing a continuous (swept) test, the objects will be just touching after you've moved them to time t, at which point the above method can be used.

Also note that the collision normal is the normalized vector from the point of contact to the circle center.

##### Share on other sites
Basically the algorithm for circle (sphere) against any convex shape is to distinguish between two cases:

1) either the circle (sphere) center is inside or on the other convex shape
2) or circle (sphere) center is outside the other convex shape and the distance is less than the circle (sphere) radius

If the circle (sphere) center is inside or on your box find the closest face to the sphere center (simple plane tests). Let n be the outward normal of this face and d be the *negative* distance to this face then the contact is:

contact.Position = sphere_center;
contact.Normal = n;

If the circle (sphere) center is outside the other convex shape compute the closest point on your box to the sphere center. Let cp_box be the closest point on the box and d be the *positive* distance between this closest point and the sphere center then the contact becomes (if the distance is less then the sphere radius):

contact.Position = sphere_center;
contact.Normal = (sphere_center - cp_box) / |sphere_center - cp_box|;

You need some extra steps when the closest points is very close to a face since the normal might become numerically invalid.

For the closest point computation you might also consider GJK.

##### Share on other sites
Thanks for the help guys! Everythings working great now.

1. 1
2. 2
3. 3
Rutin
14
4. 4
5. 5
khawk
11

• 9
• 9
• 11
• 11
• 23
• ### Forum Statistics

• Total Topics
633671
• Total Posts
3013266
×