BsConvexVolume.cpp 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. #include "BsConvexVolume.h"
  2. #include "BsAABox.h"
  3. #include "BsSphere.h"
  4. #include "BsPlane.h"
  5. namespace BansheeEngine
  6. {
  7. ConvexVolume::ConvexVolume(const Vector<Plane>& planes)
  8. :mPlanes(planes)
  9. { }
  10. bool ConvexVolume::intersects(const AABox& box) const
  11. {
  12. Vector3 center = box.getCenter();
  13. Vector3 extents = box.getHalfSize();
  14. Vector3 absExtents(Math::abs(extents.x), Math::abs(extents.y), Math::abs(extents.z));
  15. for (auto& plane : mPlanes)
  16. {
  17. float dist = center.x * plane.normal.x;
  18. dist += center.y * plane.normal.y;
  19. dist += center.z * plane.normal.z;
  20. dist = dist - plane.d;
  21. float pushOut = absExtents.x * Math::abs(plane.normal.x);
  22. pushOut += absExtents.y * Math::abs(plane.normal.y);
  23. pushOut += absExtents.z * Math::abs(plane.normal.z);
  24. if (dist > pushOut)
  25. return false;
  26. }
  27. }
  28. bool ConvexVolume::intersects(const Sphere& sphere) const
  29. {
  30. Vector3 center = sphere.getCenter();
  31. float radius = sphere.getRadius();
  32. for (auto& plane : mPlanes)
  33. {
  34. float dist = center.x * plane.normal.x;
  35. dist += center.y * plane.normal.y;
  36. dist += center.z * plane.normal.z;
  37. dist = dist - plane.d;
  38. if (dist > radius)
  39. return false;
  40. }
  41. return true;
  42. }
  43. }