BsSphere.h 2.8 KB

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