# Sphere and AABB intersection

I'm looking for an algorithm that will determine if a sphere is in any way touching an AABB or contained within it. Profile results indicate that this specific use is taking up a large percentage of my time, so it would be nice if it was as efficient as possible.

Edit: ATM, I've got this:

``````        bool IntersectsAABBWithSphere(Math::AABB rhs, Physics::Sphere sphere) const {
// http://www.gamedev.net/topic/335465-is-this-the-simplest-sphere-aabb-collision-test/
auto aabbcenter = (rhs.BottomLeftClosest + rhs.TopRightFurthest) / 2.0f;
auto SepAxis = sphere.origin - aabbcenter;
auto Dist = Math::Length(SepAxis);
SepAxis = Math::Normalize(SepAxis);
if( SepAxis.x >= SepAxis.y && SepAxis.x >= SepAxis.z )
SepAxis /= SepAxis.x;
else if( SepAxis.y >= SepAxis.x && SepAxis.y >= SepAxis.z )
SepAxis /= SepAxis.y;
else
SepAxis /= SepAxis.z;

auto extents = (rhs.TopRightFurthest - rhs.BottomLeftClosest);
SepAxis.x *= extents.x / 2.0f;
SepAxis.y *= extents.y / 2.0f;
SepAxis.z *= extents.z / 2.0f;

return Dist <= (sphere.radius + Math::Length(SepAxis));
}
``````

My AABB's are currently represented as min/max, and my spheres as origin/magnitude.

-
Perhaps you could share what you're currently doing? – Tetrad May 6 '12 at 0:41
@Tetrad: Dang, my mistake. – DeadMG May 6 '12 at 0:48