Hello,

Does anyone have a code sample to detect collision between a Sphere and a Box?

Thanks in advance.

sorry no code, only pseudo one.

From “Real-Time Rendering” 2nd Edition

This is for an AABB A= (a[size=9px]i_[/size]min,a[size=9px]i_[/size]max) i={x,y,z}

and a Sphere center C= (c[size=9px]i_[/size] and radius r).

That is, in the case of an OBB, you just have to transform the sphere center in the local BOX system, and consider the test in this system. Intersection itself doesn’t care of the system in which it is calculated.

as is :

bool sphereAABB_intersect(C,A)

return ({OVERLAP,DISJOINT})

d=0

for each i in {x,y,z} {

if(c_i < a_i_min)

d += (c_i - a_i_min)^2

else if(c_i>a_i_max)

d += (c_i - a_i_max)^2

}

if(d>r^2)

return DISJOPINT

return OVERLAP

since this is very simple, you may find more information or code for intersection on http://www.magic-software.com/Intersection.html

in your case : Intersection of oriented box and sphere

otherwise I would suggest the book from Eberly : 3D Game Engine Design, you may find in the Eyrolle librairie in Paris, near Saint Michel.

Librairie Eyrolles

57 et 61 boulevard Saint-Germain