BsSphere.h 2.5 KB

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