| 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) |
|---|
| 10 | { |
|---|
| 11 | // Stats |
|---|
| 12 | mNbVolumeBVTests++; |
|---|
| 13 | |
|---|
| 14 | float d = 0.0f; |
|---|
| 15 | |
|---|
| 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++) |
|---|
| 20 | { |
|---|
| 21 | float tmp = mCenter[i] - center[i]; |
|---|
| 22 | float s = tmp + extents[i]; |
|---|
| 23 | |
|---|
| 24 | if(s<0.0f) d += s*s; |
|---|
| 25 | else |
|---|
| 26 | { |
|---|
| 27 | s = tmp - extents[i]; |
|---|
| 28 | if(s>0.0f) d += s*s; |
|---|
| 29 | } |
|---|
| 30 | } |
|---|
| 31 | #endif |
|---|
| 32 | |
|---|
| 33 | //#ifdef NEW_TEST |
|---|
| 34 | |
|---|
| 35 | // float tmp = mCenter.x - center.x; |
|---|
| 36 | // float s = tmp + extents.x; |
|---|
| 37 | |
|---|
| 38 | float tmp,s; |
|---|
| 39 | |
|---|
| 40 | tmp = mCenter.x - center.x; |
|---|
| 41 | s = tmp + extents.x; |
|---|
| 42 | |
|---|
| 43 | if(s<0.0f) |
|---|
| 44 | { |
|---|
| 45 | d += s*s; |
|---|
| 46 | if(d>mRadius2) return FALSE; |
|---|
| 47 | } |
|---|
| 48 | else |
|---|
| 49 | { |
|---|
| 50 | s = tmp - extents.x; |
|---|
| 51 | if(s>0.0f) |
|---|
| 52 | { |
|---|
| 53 | d += s*s; |
|---|
| 54 | if(d>mRadius2) return FALSE; |
|---|
| 55 | } |
|---|
| 56 | } |
|---|
| 57 | |
|---|
| 58 | tmp = mCenter.y - center.y; |
|---|
| 59 | s = tmp + extents.y; |
|---|
| 60 | |
|---|
| 61 | if(s<0.0f) |
|---|
| 62 | { |
|---|
| 63 | d += s*s; |
|---|
| 64 | if(d>mRadius2) return FALSE; |
|---|
| 65 | } |
|---|
| 66 | else |
|---|
| 67 | { |
|---|
| 68 | s = tmp - extents.y; |
|---|
| 69 | if(s>0.0f) |
|---|
| 70 | { |
|---|
| 71 | d += s*s; |
|---|
| 72 | if(d>mRadius2) return FALSE; |
|---|
| 73 | } |
|---|
| 74 | } |
|---|
| 75 | |
|---|
| 76 | tmp = mCenter.z - center.z; |
|---|
| 77 | s = tmp + extents.z; |
|---|
| 78 | |
|---|
| 79 | if(s<0.0f) |
|---|
| 80 | { |
|---|
| 81 | d += s*s; |
|---|
| 82 | if(d>mRadius2) return FALSE; |
|---|
| 83 | } |
|---|
| 84 | else |
|---|
| 85 | { |
|---|
| 86 | s = tmp - extents.z; |
|---|
| 87 | if(s>0.0f) |
|---|
| 88 | { |
|---|
| 89 | d += s*s; |
|---|
| 90 | if(d>mRadius2) return FALSE; |
|---|
| 91 | } |
|---|
| 92 | } |
|---|
| 93 | //#endif |
|---|
| 94 | |
|---|
| 95 | #ifdef OLDIES |
|---|
| 96 | // Point Min = center - extents; |
|---|
| 97 | // Point Max = center + extents; |
|---|
| 98 | |
|---|
| 99 | float d = 0.0f; |
|---|
| 100 | |
|---|
| 101 | //find the square of the distance |
|---|
| 102 | //from the sphere to the box |
|---|
| 103 | for(udword i=0;i<3;i++) |
|---|
| 104 | { |
|---|
| 105 | float Min = center[i] - extents[i]; |
|---|
| 106 | |
|---|
| 107 | // if(mCenter[i]<Min[i]) |
|---|
| 108 | if(mCenter[i]<Min) |
|---|
| 109 | { |
|---|
| 110 | // float s = mCenter[i] - Min[i]; |
|---|
| 111 | float s = mCenter[i] - Min; |
|---|
| 112 | d += s*s; |
|---|
| 113 | } |
|---|
| 114 | else |
|---|
| 115 | { |
|---|
| 116 | float Max = center[i] + extents[i]; |
|---|
| 117 | |
|---|
| 118 | // if(mCenter[i]>Max[i]) |
|---|
| 119 | if(mCenter[i]>Max) |
|---|
| 120 | { |
|---|
| 121 | float s = mCenter[i] - Max; |
|---|
| 122 | d += s*s; |
|---|
| 123 | } |
|---|
| 124 | } |
|---|
| 125 | } |
|---|
| 126 | #endif |
|---|
| 127 | return d <= mRadius2; |
|---|
| 128 | } |
|---|