BsRay.h 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. #pragma once
  2. #include "BsPrerequisitesUtil.h"
  3. #include "BsVector3.h"
  4. namespace BansheeEngine
  5. {
  6. /**
  7. * @brief A ray in 3D space represented with an origin and direction.
  8. */
  9. class BS_UTILITY_EXPORT Ray
  10. {
  11. public:
  12. Ray()
  13. :mOrigin(Vector3::ZERO), mDirection(Vector3::UNIT_Z)
  14. { }
  15. Ray(const Vector3& origin, const Vector3& direction)
  16. :mOrigin(origin), mDirection(direction)
  17. { }
  18. void setOrigin(const Vector3& origin) { mOrigin = origin; }
  19. const Vector3& getOrigin(void) const { return mOrigin; }
  20. void setDirection(const Vector3& dir) { mDirection = dir; }
  21. const Vector3& getDirection(void) const {return mDirection;}
  22. /**
  23. * @brief Gets the position of a point t units along the ray.
  24. */
  25. Vector3 getPoint(float t) const
  26. {
  27. return Vector3(mOrigin + (mDirection * t));
  28. }
  29. /**
  30. * @brief Gets the position of a point t units along the ray.
  31. */
  32. Vector3 operator*(float t) const
  33. {
  34. return getPoint(t);
  35. }
  36. /**
  37. * @brief Transforms the ray by the given matrix.
  38. */
  39. void transform(const Matrix4& matrix);
  40. /**
  41. * @brief Transforms the ray by the given matrix.
  42. *
  43. * @note Provided matrix must be affine.
  44. */
  45. void transformAffine(const Matrix4& matrix);
  46. /**
  47. * @brief Ray/plane intersection, returns boolean result and distance to intersection point.
  48. */
  49. std::pair<bool, float> intersects(const Plane& p) const;
  50. /**
  51. * @brief Ray/sphere intersection, returns boolean result and distance to nearest intersection point.
  52. */
  53. std::pair<bool, float> intersects(const Sphere& s) const;
  54. /**
  55. * @brief Ray/axis aligned box intersection, returns boolean result and distance to nearest intersection point.
  56. */
  57. std::pair<bool, float> intersects(const AABox& box) const;
  58. /**
  59. * @brief Ray/triangle intersection, returns boolean result and distance to intersection point.
  60. *
  61. * @param a Triangle first vertex.
  62. * @param b Triangle second vertex.
  63. * @param c Triangle third vertex.
  64. * @param normal The normal of the triangle. Doesn't need to be normalized.
  65. * @param positiveSide (optional) Should intersections with the positive side (normal facing) count.
  66. * @param negativeSide (optional) Should intersections with the negative side (opposite of normal facing) count.
  67. *
  68. * @return Boolean result if intersection happened and distance to intersection point.
  69. */
  70. std::pair<bool, float> intersects(const Vector3& a, const Vector3& b, const Vector3& c,
  71. const Vector3& normal, bool positiveSide = true, bool negativeSide = true) const;
  72. protected:
  73. Vector3 mOrigin;
  74. Vector3 mDirection;
  75. };
  76. }