2 * OPCODE - Optimized Collision Detection
3 * http://www.codercorner.com/Opcode.htm
5 * Copyright (c) 2001-2008 Pierre Terdiman, pierre@codercorner.com
7 This software is provided 'as-is', without any express or implied warranty.
8 In no event will the authors be held liable for any damages arising from the use of this software.
9 Permission is granted to anyone to use this software for any purpose,
10 including commercial applications, and to alter it and redistribute it freely,
11 subject to the following restrictions:
13 1. The origin of this software must not be misrepresented; you must not claim that you wrote the original software. If you use this software in a product, an acknowledgment in the product documentation would be appreciated but is not required.
14 2. Altered source versions must be plainly marked as such, and must not be misrepresented as being the original software.
15 3. This notice may not be removed or altered from any source distribution.
18 ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
20 * Sphere-AABB overlap test, based on Jim Arvo's code.
21 * \param center [in] box center
22 * \param extents [in] box extents
23 * \return TRUE on overlap
25 ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
26 inline_ BOOL SphereCollider::SphereAABBOverlap(const Point& center, const Point& extents)
33 //find the square of the distance
34 //from the sphere to the box
36 for(udword i=0;i<3;i++)
38 float tmp = mCenter[i] - center[i];
39 float s = tmp + extents[i];
52 // float tmp = mCenter.x - center.x;
53 // float s = tmp + extents.x;
57 tmp = mCenter.x - center.x;
63 if(d>mRadius2) return FALSE;
71 if(d>mRadius2) return FALSE;
75 tmp = mCenter.y - center.y;
81 if(d>mRadius2) return FALSE;
89 if(d>mRadius2) return FALSE;
93 tmp = mCenter.z - center.z;
99 if(d>mRadius2) return FALSE;
107 if(d>mRadius2) return FALSE;
113 // Point Min = center - extents;
114 // Point Max = center + extents;
118 //find the square of the distance
119 //from the sphere to the box
120 for(udword i=0;i<3;i++)
122 float Min = center[i] - extents[i];
124 // if(mCenter[i]<Min[i])
127 // float s = mCenter[i] - Min[i];
128 float s = mCenter[i] - Min;
133 float Max = center[i] + extents[i];
135 // if(mCenter[i]>Max[i])
138 float s = mCenter[i] - Max;
144 return d <= mRadius2;