BoundingSphere.cpp 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316
  1. #include "Base.h"
  2. #include "BoundingSphere.h"
  3. #include "BoundingBox.h"
  4. namespace gameplay
  5. {
  6. BoundingSphere::BoundingSphere()
  7. : radius(0)
  8. {
  9. }
  10. BoundingSphere::BoundingSphere(const Vector3& center, float radius)
  11. {
  12. set(center, radius);
  13. }
  14. BoundingSphere::BoundingSphere(const BoundingSphere& copy)
  15. {
  16. set(copy);
  17. }
  18. BoundingSphere::~BoundingSphere()
  19. {
  20. }
  21. const BoundingSphere& BoundingSphere::empty()
  22. {
  23. static BoundingSphere s;
  24. return s;
  25. }
  26. bool BoundingSphere::intersects(const BoundingSphere& sphere) const
  27. {
  28. // If the distance between the spheres' centers is less than or equal
  29. // to the sum of their radii, then the spheres intersect.
  30. float vx = sphere.center.x - center.x;
  31. float vy = sphere.center.y - center.y;
  32. float vz = sphere.center.z - center.z;
  33. return sqrt(vx * vx + vy * vy + vz * vz) <= (radius + sphere.radius);
  34. }
  35. bool BoundingSphere::intersects(const BoundingBox& box) const
  36. {
  37. // Determine what point is closest; if the distance to that
  38. // point is less than the radius, then this sphere intersects.
  39. float cpX = center.x;
  40. float cpY = center.y;
  41. float cpZ = center.z;
  42. const Vector3& boxMin = box.min;
  43. const Vector3& boxMax = box.max;
  44. // Closest x value.
  45. if (center.x < boxMin.x)
  46. {
  47. cpX = boxMin.x;
  48. }
  49. else if (center.x > boxMax.x)
  50. {
  51. cpX = boxMax.x;
  52. }
  53. // Closest y value.
  54. if (center.y < boxMin.y)
  55. {
  56. cpY = boxMin.y;
  57. }
  58. else if (center.y > boxMax.y)
  59. {
  60. cpY = boxMax.y;
  61. }
  62. // Closest z value.
  63. if (center.z < boxMin.z)
  64. {
  65. cpZ = boxMin.z;
  66. }
  67. else if (center.z > boxMax.z)
  68. {
  69. cpZ = boxMax.z;
  70. }
  71. // Find the distance to the closest point and see if it is less than or equal to the radius.
  72. cpX -= center.x;
  73. cpY -= center.y;
  74. cpZ -= center.z;
  75. return sqrt(cpX * cpX + cpY * cpY + cpZ * cpZ) <= radius;
  76. }
  77. bool BoundingSphere::intersects(const Frustum& frustum) const
  78. {
  79. // The sphere must either intersect or be in the positive half-space of all six planes of the frustum.
  80. return (intersects(frustum.getNear()) != Plane::INTERSECTS_BACK &&
  81. intersects(frustum.getFar()) != Plane::INTERSECTS_BACK &&
  82. intersects(frustum.getLeft()) != Plane::INTERSECTS_BACK &&
  83. intersects(frustum.getRight()) != Plane::INTERSECTS_BACK &&
  84. intersects(frustum.getBottom()) != Plane::INTERSECTS_BACK &&
  85. intersects(frustum.getTop()) != Plane::INTERSECTS_BACK );
  86. }
  87. float BoundingSphere::intersects(const Plane& plane) const
  88. {
  89. float distance = plane.distance(center);
  90. if (fabsf(distance) <= radius)
  91. {
  92. return Plane::INTERSECTS_INTERSECTING;
  93. }
  94. else if (distance > 0.0f)
  95. {
  96. return Plane::INTERSECTS_FRONT;
  97. }
  98. else
  99. {
  100. return Plane::INTERSECTS_BACK;
  101. }
  102. }
  103. float BoundingSphere::intersects(const Ray& ray) const
  104. {
  105. const Vector3& origin = ray.getOrigin();
  106. const Vector3& direction = ray.getDirection();
  107. // Calculate the vector and the square of the distance from the ray's origin to this sphere's center.
  108. float vx = origin.x - center.x;
  109. float vy = origin.y - center.y;
  110. float vz = origin.z - center.z;
  111. float d2 = vx * vx + vy * vy + vz * vz;
  112. // Solve the quadratic equation using the ray's and sphere's equations together.
  113. // Since the ray's direction is guaranteed to be 1 by the Ray, we don't need to
  114. // calculate and use A (A=ray.getDirection().lengthSquared()).
  115. float B = 2.0f * (vx * direction.x + vy * direction.y + vz * direction.z);
  116. float C = d2 - radius * radius;
  117. float discriminant = B * B - 4.0f * C;
  118. // If the discriminant is negative, then there is no intersection.
  119. if (discriminant < 0.0f)
  120. {
  121. return Ray::INTERSECTS_NONE;
  122. }
  123. else
  124. {
  125. // The intersection is at the smaller positive root.
  126. float sqrtDisc = sqrt(discriminant);
  127. float t0 = (-B - sqrtDisc) * 0.5f;
  128. float t1 = (-B + sqrtDisc) * 0.5f;
  129. return (t0 > 0.0f && t0 < t1) ? t0 : t1;
  130. }
  131. }
  132. bool BoundingSphere::isEmpty() const
  133. {
  134. return radius == 0.0f;
  135. }
  136. void BoundingSphere::merge(const BoundingSphere& sphere)
  137. {
  138. // Calculate the distance between the two centers.
  139. float vx = center.x - sphere.center.x;
  140. float vy = center.y - sphere.center.y;
  141. float vz = center.z - sphere.center.z;
  142. float d = sqrt(vx * vx + vy * vy + vz * vz);
  143. // If one sphere is contained inside the other, set to the larger sphere.
  144. if (d <= (sphere.radius - radius))
  145. {
  146. center = sphere.center;
  147. radius = sphere.radius;
  148. return;
  149. }
  150. else if (d <= (radius - sphere.radius))
  151. {
  152. return;
  153. }
  154. // Calculate the unit vector between the two centers.
  155. float dI = 1.0f / d;
  156. vx *= dI;
  157. vy *= dI;
  158. vz *= dI;
  159. // Calculate the new radius.
  160. float r = (radius + sphere.radius + d) * 0.5f;
  161. // Calculate the new center.
  162. float scaleFactor = (r - sphere.radius);
  163. vx = vx * scaleFactor + sphere.center.x;
  164. vy = vy * scaleFactor + sphere.center.y;
  165. vz = vz * scaleFactor + sphere.center.z;
  166. // Set the new center and radius.
  167. center.x = vx;
  168. center.y = vy;
  169. center.z = vz;
  170. radius = r;
  171. }
  172. void BoundingSphere::merge(const BoundingBox& box)
  173. {
  174. const Vector3& min = box.min;
  175. const Vector3& max = box.max;
  176. // Find the corner of the bounding box that is farthest away from this sphere's center.
  177. float v1x = min.x - center.x;
  178. float v1y = min.y - center.y;
  179. float v1z = min.z - center.z;
  180. float v2x = max.x - center.x;
  181. float v2y = max.y - center.y;
  182. float v2z = max.z - center.z;
  183. float fx = min.x;
  184. float fy = min.y;
  185. float fz = min.z;
  186. if (v2x > v1x)
  187. {
  188. fx = max.x;
  189. }
  190. if (v2y > v1y)
  191. {
  192. fy = max.y;
  193. }
  194. if (v2z > v1z)
  195. {
  196. fz = max.z;
  197. }
  198. // Calculate the unit vector and the distance between the center and the farthest point.
  199. v1x = center.x - fx;
  200. v1y = center.y - fy;
  201. v1z = center.z - fz;
  202. float distance = sqrt(v1x * v1x + v1y * v1y + v1z * v1z);
  203. // If the box is inside the sphere, we are done.
  204. if (distance <= radius)
  205. {
  206. return;
  207. }
  208. // Calculate the unit vector between the center and the farthest point.
  209. float dI = 1.0f / distance;
  210. v1x *= dI;
  211. v1y *= dI;
  212. v1z *= dI;
  213. // Calculate the new radius.
  214. float r = (radius + distance) * 0.5f;
  215. // Calculate the new center.
  216. v1x = v1x * r + fx;
  217. v1y = v1y * r + fy;
  218. v1z = v1z * r + fz;
  219. // Set the new center and radius.
  220. center.x = v1x;
  221. center.y = v1y;
  222. center.z = v1z;
  223. radius = r;
  224. }
  225. void BoundingSphere::set(const Vector3& center, float radius)
  226. {
  227. this->center = center;
  228. this->radius = radius;
  229. }
  230. void BoundingSphere::set(const BoundingSphere& sphere)
  231. {
  232. center = sphere.center;
  233. radius = sphere.radius;
  234. }
  235. void BoundingSphere::set(const BoundingBox& box)
  236. {
  237. center.x = (box.min.x + box.max.x) * 0.5f;
  238. center.y = (box.min.y + box.max.y) * 0.5f;
  239. center.z = (box.min.z + box.max.z) * 0.5f;
  240. radius = center.distance(box.max);
  241. }
  242. void BoundingSphere::transform(const Matrix& matrix)
  243. {
  244. // Translate the center point.
  245. matrix.transformPoint(center, &center);
  246. // Scale the sphere's radius by the scale fo the matrix
  247. Vector3 scale;
  248. matrix.decompose(&scale, NULL, NULL);
  249. float r = radius * scale.x;
  250. r = max(r, radius * scale.y);
  251. r = max(r, radius * scale.z);
  252. radius = r;
  253. }
  254. float BoundingSphere::distance(const BoundingSphere& sphere, const Vector3& point)
  255. {
  256. return sqrt((point.x - sphere.center.x) * (point.x - sphere.center.x) +
  257. (point.y - sphere.center.y) * (point.y - sphere.center.x) +
  258. (point.z - sphere.center.z) * (point.z - sphere.center.x));
  259. }
  260. bool BoundingSphere::contains(const BoundingSphere& sphere, Vector3* points, unsigned int count)
  261. {
  262. for (unsigned int i = 0; i < count; i++)
  263. {
  264. if (distance(sphere, points[i]) > sphere.radius)
  265. {
  266. return false;
  267. }
  268. }
  269. return true;
  270. }
  271. }