BsRay.h 2.9 KB

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