# Sphere - AABB collision repsonse

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

## Recommended Posts

Dear fellow GameDev members, I have a small project that I'm working on that has a sphere colliding with an AABB. I have the collision test working, but I am having trouble getting a proper collision response. I would like to push the sphere back and clamp it so that it cannot move. So that when I hit the AABB, the sphere would be pushed back to the point where it's just touching it and is refused any movement toward the AABB. I thank you all for your time :)

##### Share on other sites
I've just read through article 1 to 3 from this site and I think what you need to read is the third one.
It basically tells you how to apply a collision response that will instantly push the objects aside, without having them interpenetrating each other. This is done by applying an impulse instead of a force, because the impulse is applied at an instance, whereas a force acts over time to change a bodys velocity. It also tells you how to calculate said impulse in article 3 for a 2D collision response, and I think it also does in article 4 for a 3D collision response, but I haven't read it through yet.

##### Share on other sites
for simple collision response, the algo is can be :

1) find point 'pbox' on box the closest to the sphere centre.2) if 'pbox' is outside the sphere no collision.3) find point 'pshpere' on sphere surface the closest to point 'pbox'. 4) to push sphere on the box surface, 'pbox' should be equal to 'psphere'.  -> Vector delta = (psphere - pbox);  -> float distance =  delta.length();  -> Vector push = delta * (sphere.radius - distance) / distance;  -> sphere.position += push;  -> sphere.velocity -= delta * (sphere.velocity.dotProduct(delta) / delta.dotProduct(delta));

the sphere position gets pushed outside the box, and the sphere velocity is zero along the collision normal.

##### Share on other sites
Sweet, that did the job. Thanks to the both of you :D

##### Share on other sites
Hey guys, sorry to bug you all, but I have another little quirk. The collision detection works, but I believe that when multiple spheres are colliding with each other and then a sphere collides with the AABB, the checks fails. The sphere would then enter the AABB and when I debug through the program to see if there is a way I can push it out I noticed an odd bug. My position is -1.#IND000. Am I going to have to reset the objects position at this point and if so, how can I reset so that it wouldn't look too clunky. Like have the object randomly spawn in the middle of no where. Thanks again for your time.

##### Share on other sites
There is a division, it is possible you get a division by zero. BTW, tere was a bug anyway.

1) find point 'pbox' on box the closest to the sphere centre.2) if 'pbox' is outside the sphere no collision.3) find point 'pshpere' on sphere surface the closest to point 'pbox'. 4) to push sphere on the box surface, 'pbox' should be equal to 'psphere'.

Vector pbox = aabb.closestPoint(sphere.position);Vector delta = (sphere.position - pbox);float distance =  delta.length();if(distance < 0.000001f || distance > sphere.radius) return false;Vector normal = delta / distance; vector push = normal * (sphere.radius - distance);sphere.position += push;sphere.velocity -= normal * sphere.velocity.dotProduct(delta);

1. 1
2. 2
3. 3
4. 4
Rutin
17
5. 5

• 11
• 33
• 12
• 12
• 11
• ### Forum Statistics

• Total Topics
631411
• Total Posts
2999933
×