Jump to content

  • Log In with Google      Sign In   
  • Create Account


Intersection between a circle and an aabb?


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
6 replies to this topic

#1 Doublefris   Members   -  Reputation: 297

Like
0Likes
Like

Posted 16 October 2013 - 10:14 AM

Hey, for a little physics project I want to find the collision normal and penetration for a circle and an aabb. How would I go about doing this?



Sponsor:

#2 WarAmp   Members   -  Reputation: 716

Like
0Likes
Like

Posted 16 October 2013 - 12:06 PM

Step 1) Find the closest point on the AABB to the center © of the circle: This will be your potential collision location. (P)

Step 2) If the distance between C and P (D = magnitude(C - P)) is less than the radius of the circle ®, then you have a collision!

Step 3) The penetration depth is then (R - D)

Step 4) The normal is the normalized (C - P) vector.


Waramp.Before you insult a man, walk a mile in his shoes.That way, when you do insult him, you'll be a mile away, and you'll have his shoes.

#3 Doublefris   Members   -  Reputation: 297

Like
0Likes
Like

Posted 16 October 2013 - 01:07 PM

It works! However it leaves a tiny bit of overlap.



#4 Dirk Gregorius   Members   -  Reputation: 706

Like
0Likes
Like

Posted 16 October 2013 - 02:06 PM

This only works if the sphere center is outside of the box (which should be the majority of cases though). If the sphere center is inside the box you need to compute the distance to each side of the box and keep track of the closest one. The defines the axis of minimum penetration and you can construct the contact point from there.

 

You also need to be careful with the location of your collision point. I would move the contact point out onto the surface of the sphere for consistent friction in a rigid body simulation.


Edited by Dirk Gregorius, 16 October 2013 - 02:07 PM.


#5 Doublefris   Members   -  Reputation: 297

Like
0Likes
Like

Posted 17 October 2013 - 06:44 AM

For the record, using this method( The one WarAmp gave) I still get a little bit of overlap. here is my code :

            Vector2 p;
            p.X = Utils.Clamp(c.myCenter.X, b.Min.X, b.Max.X);
            p.Y = Utils.Clamp(c.myCenter.Y, b.Max.Y, b.Min.Y);

            Vector2 toClosest = c.myCenter - p;

            float d = toClosest.LengthFast;

            Depth = c.myRadius - d;
            myNormal = toClosest / d;

289wqhv.jpg

 

The triangle in this picture is encapsulated by a bounding circle, and this is how far it still impregnates.


Edited by Doublefris, 17 October 2013 - 06:49 AM.


#6 Dirk Gregorius   Members   -  Reputation: 706

Like
0Likes
Like

Posted 17 October 2013 - 10:12 AM

p.X = Utils.Clamp(c.myCenter.X, b.Min.X, b.Max.X);
p.Y = Utils.Clamp(c.myCenter.Y, b.Max.Y, b.Min.Y); <=== Check this line



#7 Doublefris   Members   -  Reputation: 297

Like
0Likes
Like

Posted 17 October 2013 - 11:58 AM

Ah of course! Silly error. Thanks!






Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS