Jump to content
  • Advertisement
Sign in to follow this  
Helo7777

equation for point of collision box-circle?

This topic is 3806 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

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


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

Share this post


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

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!