2
0

OPC_SphereAABBOverlap.h 2.2 KB

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