BsConvexVolume.cpp 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. //********************************** Banshee Engine (www.banshee3d.com) **************************************************//
  2. //**************** Copyright (c) 2016 Marko Pintera ([email protected]). All rights reserved. **********************//
  3. #include "BsConvexVolume.h"
  4. #include "BsAABox.h"
  5. #include "BsSphere.h"
  6. #include "BsPlane.h"
  7. #include "BsMath.h"
  8. namespace BansheeEngine
  9. {
  10. ConvexVolume::ConvexVolume(const Vector<Plane>& planes)
  11. :mPlanes(planes)
  12. { }
  13. bool ConvexVolume::intersects(const AABox& box) const
  14. {
  15. Vector3 center = box.getCenter();
  16. Vector3 extents = box.getHalfSize();
  17. Vector3 absExtents(Math::abs(extents.x), Math::abs(extents.y), Math::abs(extents.z));
  18. for (auto& plane : mPlanes)
  19. {
  20. float dist = center.dot(plane.normal) - plane.d;
  21. float effectiveRadius = absExtents.x * Math::abs(plane.normal.x);
  22. effectiveRadius += absExtents.y * Math::abs(plane.normal.y);
  23. effectiveRadius += absExtents.z * Math::abs(plane.normal.z);
  24. if (dist < -effectiveRadius)
  25. return false;
  26. }
  27. return true;
  28. }
  29. bool ConvexVolume::intersects(const Sphere& sphere) const
  30. {
  31. Vector3 center = sphere.getCenter();
  32. float radius = sphere.getRadius();
  33. for (auto& plane : mPlanes)
  34. {
  35. float dist = center.dot(plane.normal) - plane.d;
  36. if (dist < -radius)
  37. return false;
  38. }
  39. return true;
  40. }
  41. }