equation for point of collision box-circle?

Started by
2 comments, last by Helo7777 16 years, 3 months ago
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
Advertisement
Quote:Original post by Helo7777
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.
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.
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.Distance = d - sphere_radius;
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.Distance = d - sphere_radius;
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.
Thanks for the help guys! Everythings working great now.

This topic is closed to new replies.

Advertisement