Ray.h 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  1. #ifndef RAY_H_
  2. #define RAY_H_
  3. #include "Vector3.h"
  4. namespace gameplay
  5. {
  6. class Frustum;
  7. class Plane;
  8. class BoundingSphere;
  9. class BoundingBox;
  10. /**
  11. * Defines a 3-dimensional ray.
  12. *
  13. * This class guarantees that its direction vector is always normalized.
  14. */
  15. class Ray
  16. {
  17. public:
  18. /**
  19. * Represents when a 3D entity does not intersect a ray.
  20. */
  21. static const int INTERSECTS_NONE = -1;
  22. /**
  23. * Constructs a new ray initialized to origin(0,0,0) and direction(0,0,1).
  24. */
  25. Ray();
  26. /**
  27. * Constructs a new ray initialized to the specified values.
  28. *
  29. * @param origin The ray's origin.
  30. * @param direction The ray's direction.
  31. */
  32. Ray(const Vector3& origin, const Vector3& direction);
  33. /**
  34. * Constructs a new ray initialized to the specified values.
  35. *
  36. * @param originX The x coordinate of the origin.
  37. * @param originY The y coordinate of the origin.
  38. * @param originZ The z coordinate of the origin.
  39. * @param dirX The x coordinate of the direction.
  40. * @param dirY The y coordinate of the direction.
  41. * @param dirZ The z coordinate of the direction.
  42. */
  43. Ray(float originX, float originY, float originZ, float dirX, float dirY, float dirZ);
  44. /**
  45. * Constructs a new ray from the given ray.
  46. *
  47. * @param copy The ray to copy.
  48. */
  49. Ray(const Ray& copy);
  50. /**
  51. * Destructor.
  52. */
  53. ~Ray();
  54. /**
  55. * Gets the ray's origin.
  56. *
  57. * @return The ray's origin.
  58. */
  59. const Vector3& getOrigin() const;
  60. /**
  61. * Sets the ray's origin to the given point.
  62. *
  63. * @param origin The new origin.
  64. */
  65. void setOrigin(const Vector3& origin);
  66. /**
  67. * Sets the ray's origin.
  68. *
  69. * @param x The x coordinate of the origin.
  70. * @param y The y coordinate of the origin.
  71. * @param z The z coordinate of the origin.
  72. */
  73. void setOrigin(float x, float y, float z);
  74. /**
  75. * Gets the ray's direction.
  76. *
  77. * @return The ray's direction.
  78. */
  79. const Vector3& getDirection() const;
  80. /**
  81. * Sets the ray's direction to the given vector.
  82. *
  83. * @param direction The new direction vector.
  84. */
  85. void setDirection(const Vector3& direction);
  86. /**
  87. * Sets the ray's direction.
  88. *
  89. * @param x The x coordinate of the direction.
  90. * @param y The y coordinate of the direction.
  91. * @param z The z coordinate of the direction.
  92. */
  93. void setDirection(float x, float y, float z);
  94. /**
  95. * Tests whether this ray intersects the specified bounding sphere.
  96. *
  97. * @param sphere The bounding sphere to test intersection with.
  98. *
  99. * @return The distance from the origin of this ray to the bounding object or
  100. * INTERSECTS_NONE if this ray does not intersect the bounding object.
  101. */
  102. float intersects(const BoundingSphere& sphere) const;
  103. /**
  104. * Tests whether this ray intersects the specified bounding box.
  105. *
  106. * @param box The bounding box to test intersection with.
  107. *
  108. * @return The distance from the origin of this ray to the bounding object or
  109. * INTERSECTS_NONE if this ray does not intersect the bounding object.
  110. */
  111. float intersects(const BoundingBox& box) const;
  112. /**
  113. * Tests whether this ray intersects the specified frustum.
  114. *
  115. * @param frustum The frustum to test intersection with.
  116. *
  117. * @return The distance from the origin of this ray to the frustum or
  118. * INTERSECTS_NONE if this ray does not intersect the frustum.
  119. */
  120. float intersects(const Frustum& frustum) const;
  121. /**
  122. * Tests whether this ray intersects the specified plane and returns the distance
  123. * from the origin of the ray to the plane.
  124. *
  125. * @param plane The plane to test intersection with.
  126. *
  127. * @return The distance from the origin of this ray to the plane or
  128. * INTERSECTS_NONE if this ray does not intersect the plane.
  129. */
  130. float intersects(const Plane& plane) const;
  131. /**
  132. * Sets this ray to the specified values.
  133. *
  134. * @param origin The ray's origin.
  135. * @param direction The ray's direction.
  136. */
  137. void set(const Vector3& origin, const Vector3& direction);
  138. /**
  139. * Sets this ray to the given ray.
  140. *
  141. * @param ray The ray to copy.
  142. */
  143. void set(const Ray& ray);
  144. /**
  145. * Transforms this ray by the given transformation matrix.
  146. *
  147. * @param matrix The transformation matrix to transform by.
  148. */
  149. void transform(const Matrix& matrix);
  150. /**
  151. * Transforms this ray by the given matrix.
  152. *
  153. * @param matrix The matrix to transform by.
  154. * @return This ray, after the transformation occurs.
  155. */
  156. inline Ray& operator*=(const Matrix& matrix);
  157. private:
  158. /**
  159. * Normalizes the ray.
  160. */
  161. void normalize();
  162. Vector3 _origin; // The ray origin position.
  163. Vector3 _direction; // The ray direction vector.
  164. };
  165. /**
  166. * Transforms the given ray by the given matrix.
  167. *
  168. * @param matrix The matrix to transform by.
  169. * @param ray The ray to transform.
  170. * @return The resulting transformed ray.
  171. */
  172. inline const Ray operator*(const Matrix& matrix, const Ray& ray);
  173. }
  174. #include "Ray.inl"
  175. #endif