Fixed: Some incorrect changes from #2114 were reverted and full reorder of constraint...
[ode.git] / OPCODE / OPC_SphereAABBOverlap.h
blob2278bc01f94d0e8ece522dbec54bb361e11d988a
1 ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
2 /**
3 * Sphere-AABB overlap test, based on Jim Arvo's code.
4 * \param center [in] box center
5 * \param extents [in] box extents
6 * \return TRUE on overlap
7 */
8 ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
9 inline_ BOOL SphereCollider::SphereAABBOverlap(const Point& center, const Point& extents)
11 // Stats
12 mNbVolumeBVTests++;
14 float d = 0.0f;
16 //find the square of the distance
17 //from the sphere to the box
18 #ifdef OLDIES
19 for(udword i=0;i<3;i++)
21 float tmp = mCenter[i] - center[i];
22 float s = tmp + extents[i];
24 if(s<0.0f) d += s*s;
25 else
27 s = tmp - extents[i];
28 if(s>0.0f) d += s*s;
31 #endif
33 //#ifdef NEW_TEST
35 // float tmp = mCenter.x - center.x;
36 // float s = tmp + extents.x;
38 float tmp,s;
40 tmp = mCenter.x - center.x;
41 s = tmp + extents.x;
43 if(s<0.0f)
45 d += s*s;
46 if(d>mRadius2) return FALSE;
48 else
50 s = tmp - extents.x;
51 if(s>0.0f)
53 d += s*s;
54 if(d>mRadius2) return FALSE;
58 tmp = mCenter.y - center.y;
59 s = tmp + extents.y;
61 if(s<0.0f)
63 d += s*s;
64 if(d>mRadius2) return FALSE;
66 else
68 s = tmp - extents.y;
69 if(s>0.0f)
71 d += s*s;
72 if(d>mRadius2) return FALSE;
76 tmp = mCenter.z - center.z;
77 s = tmp + extents.z;
79 if(s<0.0f)
81 d += s*s;
82 if(d>mRadius2) return FALSE;
84 else
86 s = tmp - extents.z;
87 if(s>0.0f)
89 d += s*s;
90 if(d>mRadius2) return FALSE;
93 //#endif
95 #ifdef OLDIES
96 // Point Min = center - extents;
97 // Point Max = center + extents;
99 float d = 0.0f;
101 //find the square of the distance
102 //from the sphere to the box
103 for(udword i=0;i<3;i++)
105 float Min = center[i] - extents[i];
107 // if(mCenter[i]<Min[i])
108 if(mCenter[i]<Min)
110 // float s = mCenter[i] - Min[i];
111 float s = mCenter[i] - Min;
112 d += s*s;
114 else
116 float Max = center[i] + extents[i];
118 // if(mCenter[i]>Max[i])
119 if(mCenter[i]>Max)
121 float s = mCenter[i] - Max;
122 d += s*s;
126 #endif
127 return d <= mRadius2;