# OT: Sphere-Box intersection algorithm

Sorry for the slightly off-topic question.

I need a (decently) simple algoritm for finding whether a sphere intersects an axis-aligned box. The algoritm should return true if the sphere a little as touches the box. At first I thought this was an easy problem, but after trying to find a simple solution myself and then googleing like mad over it I still haven’t found a good solution. The only solution I can come up with right now is to check for intersection on all 6 planes and all 12 lines.

Do anyone have a better solution?

You can calculate the plane equation for every box side (pretty cheap for a box since you don’t really need to calculate it) and check the distance of the sphere’s radius to every plane. If the distance is somewhere greater than the radius, there is no collision. But make sure that your normals are all pointing out of the box!

LaBasX2

Thanks for your reply, but that’s not a complete solution. I need it to return false even in a situation like this .

Hmm…you’re right, that won’t work in that case. I always though that test were safe for convex objects but probably only as a point in brush test, not sphere in brush test…

If you’ve done a google search, you’ve probably also found that Gamasutra article about collision detection. I haven’t tested it but it is looking quite interesting: http://www.gamasutra.com/features/19991018/Gomez_4.htm

LaBasX2

That’s the algorithm I use. It’s pretty much identicle to the one in Graphics Gems. It handles all special cases ( including Humus’ ), sphere completely inside box and sphere enclosing entire box.

Ah! Thanks, that’s exactly what I need. So simple and easy to understand once you see it.
(I had to redo the search to see if I got that one or if I just used a bad search string. Sure, I did get that one too, quite early too. Not sure why I passed it by, probably because of that AABB abbreviation that looked like something completely different at first. I just now understood what it means, Axis-Aligned Bounding Box)

realtimerendering website have a page with a table which lists a LOT of various intersection tests.
also sphere->AABB can be found at dave eberly’s site

hehe, that code snipped is CUTE!
my version would involve checking against the points, against the egdes, against the faces, etc… HEHE

well then, i think that one could be a bit faster