BsRay.h 2.7 KB

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