BsSphere.h 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. //********************************** Banshee Engine (www.banshee3d.com) **************************************************//
  2. //**************** Copyright (c) 2016 Marko Pintera ([email protected]). All rights reserved. **********************//
  3. #pragma once
  4. #include "BsPrerequisitesUtil.h"
  5. #include "BsVector3.h"
  6. namespace BansheeEngine
  7. {
  8. /** @addtogroup Math
  9. * @{
  10. */
  11. /** A sphere represented by a center point and a radius. */
  12. class BS_UTILITY_EXPORT Sphere
  13. {
  14. public:
  15. /** Default constructor. Creates a unit sphere around the origin. */
  16. Sphere() : mRadius(1.0), mCenter(Vector3::ZERO)
  17. { }
  18. Sphere(const Vector3& center, float radius)
  19. :mRadius(radius), mCenter(center)
  20. { }
  21. /** Returns the radius of the sphere. */
  22. float getRadius(void) const { return mRadius; }
  23. /** Sets the radius of the sphere. */
  24. void setRadius(float radius) { mRadius = radius; }
  25. /** Returns the center point of the sphere. */
  26. const Vector3& getCenter(void) const { return mCenter; }
  27. /** Sets the center point of the sphere. */
  28. void setCenter(const Vector3& center) { mCenter = center; }
  29. /** Merges the two spheres, creating a new sphere that encapsulates them both. */
  30. void merge(const Sphere& rhs);
  31. /** Expands the sphere so it includes the provided point. */
  32. void merge(const Vector3& point);
  33. /** Transforms the sphere by the given matrix. */
  34. void transform(const Matrix4& matrix);
  35. /** Returns whether or not this sphere contains the provided point. */
  36. bool contains(const Vector3& v) const
  37. {
  38. return ((v - mCenter).squaredLength() <= Math::sqr(mRadius));
  39. }
  40. /** Returns whether or not this sphere intersects another sphere. */
  41. bool intersects(const Sphere& s) const
  42. {
  43. return (s.mCenter - mCenter).squaredLength() <=
  44. Math::sqr(s.mRadius + mRadius);
  45. }
  46. /** Returns whether or not this sphere intersects a box. */
  47. bool intersects(const AABox& box) const;
  48. /** Returns whether or not this sphere intersects a plane. */
  49. bool intersects(const Plane& plane) const;
  50. /**
  51. * Ray/sphere intersection, returns boolean result and distance to nearest intersection.
  52. *
  53. * @param[in] discardInside (optional) If true the intersection will be discarded if ray origin
  54. * is located within the sphere.
  55. */
  56. std::pair<bool, float> intersects(const Ray& ray, bool discardInside = true) const;
  57. private:
  58. float mRadius;
  59. Vector3 mCenter;
  60. };
  61. /** @} */
  62. /** @cond SPECIALIZATIONS */
  63. BS_ALLOW_MEMCPY_SERIALIZATION(Sphere);
  64. /** @endcond */
  65. }