BsRay.h 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  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 ray in 3D space represented with an origin and direction.
  11. */
  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. /**
  26. * @brief Gets the position of a point t units along the ray.
  27. */
  28. Vector3 getPoint(float t) const
  29. {
  30. return Vector3(mOrigin + (mDirection * t));
  31. }
  32. /**
  33. * @brief Gets the position of a point t units along the ray.
  34. */
  35. Vector3 operator*(float t) const
  36. {
  37. return getPoint(t);
  38. }
  39. /**
  40. * @brief Ray/plane intersection, returns boolean result and distance to intersection point.
  41. */
  42. std::pair<bool, float> intersects(const Plane& p) const;
  43. /**
  44. * @brief Ray/sphere intersection, returns boolean result and distance to nearest intersection point.
  45. */
  46. std::pair<bool, float> intersects(const Sphere& s) const;
  47. /**
  48. * @brief Ray/axis aligned box intersection, returns boolean result and distance to nearest intersection point.
  49. */
  50. std::pair<bool, float> intersects(const AABox& box) const;
  51. /**
  52. * @brief Ray/triangle intersection, returns boolean result and distance to intersection point.
  53. *
  54. * @param a Triangle first vertex.
  55. * @param b Triangle second vertex.
  56. * @param c Triangle third vertex.
  57. * @param normal The normal of the triangle. Doesn't need to be normalized.
  58. * @param positiveSide (optional) Should intersections with the positive side (normal facing) count.
  59. * @param negativeSide (optional) Should intersections with the negative side (opposite of normal facing) count.
  60. *
  61. * @return Boolean result if intersection happened and distance to intersection point.
  62. */
  63. std::pair<bool, float> intersects(const Vector3& a, const Vector3& b, const Vector3& c,
  64. const Vector3& normal, bool positiveSide = true, bool negativeSide = true) const;
  65. protected:
  66. Vector3 mOrigin;
  67. Vector3 mDirection;
  68. };
  69. }