Ray.cpp 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. #include "Base.h"
  2. #include "Ray.h"
  3. #include "Plane.h"
  4. #include "Frustum.h"
  5. #include "BoundingSphere.h"
  6. #include "BoundingBox.h"
  7. namespace gameplay
  8. {
  9. Ray::Ray()
  10. {
  11. }
  12. Ray::Ray(const Vector3& origin, const Vector3& direction)
  13. {
  14. set(origin, direction);
  15. }
  16. Ray::Ray(const Ray& copy)
  17. {
  18. set(copy);
  19. }
  20. Ray::~Ray()
  21. {
  22. }
  23. const Vector3& Ray::getOrigin() const
  24. {
  25. return _origin;
  26. }
  27. void Ray::setOrigin(const Vector3& origin)
  28. {
  29. _origin = origin;
  30. }
  31. const Vector3& Ray::getDirection() const
  32. {
  33. return _direction;
  34. }
  35. void Ray::setDirection(const Vector3& direction)
  36. {
  37. _direction = direction;
  38. normalize();
  39. }
  40. float Ray::intersects(const BoundingSphere& sphere) const
  41. {
  42. return sphere.intersects(*this);
  43. }
  44. float Ray::intersects(const BoundingBox& box) const
  45. {
  46. return box.intersects(*this);
  47. }
  48. float Ray::intersects(const Frustum& frustum) const
  49. {
  50. Plane n = frustum.getNear();
  51. float nD = intersects(n);
  52. float nOD = n.distance(_origin);
  53. Plane f = frustum.getFar();
  54. float fD = intersects(f);
  55. float fOD = f.distance(_origin);
  56. Plane l = frustum.getLeft();
  57. float lD = intersects(l);
  58. float lOD = l.distance(_origin);
  59. Plane r = frustum.getRight();
  60. float rD = intersects(r);
  61. float rOD = r.distance(_origin);
  62. Plane b = frustum.getBottom();
  63. float bD = intersects(b);
  64. float bOD = b.distance(_origin);
  65. Plane t = frustum.getTop();
  66. float tD = intersects(t);
  67. float tOD = t.distance(_origin);
  68. // If the ray's origin is in the negative half-space of one of the frustum's planes
  69. // and it does not intersect that same plane, then it does not intersect the frustum.
  70. if ( (nOD < 0.0f && nD < 0.0f) || (fOD < 0.0f && fD < 0.0f) ||
  71. (lOD < 0.0f && lD < 0.0f) || (rOD < 0.0f && rD < 0.0f) ||
  72. (bOD < 0.0f && bD < 0.0f) || (tOD < 0.0f && tD < 0.0f) )
  73. {
  74. return Ray::INTERSECTS_NONE;
  75. }
  76. // Otherwise, the intersection distance is the minimum positive intersection distance.
  77. float d = (nD > 0.0f) ? nD : 0.0f;
  78. d = (fD > 0.0f) ? ( (d == 0.0f) ? fD : min(fD, d) ) : d;
  79. d = (lD > 0.0f) ? ( (d == 0.0f) ? lD : min(lD, d) ) : d;
  80. d = (rD > 0.0f) ? ( (d == 0.0f) ? rD : min(rD, d) ) : d;
  81. d = (tD > 0.0f) ? ( (d == 0.0f) ? bD : min(bD, d) ) : d;
  82. d = (bD > 0.0f) ? ( (d == 0.0f) ? tD : min(tD, d) ) : d;
  83. return d;
  84. }
  85. float Ray::intersects(const Plane& plane) const
  86. {
  87. const Vector3& normal = plane.getNormal();
  88. // If the origin of the ray is on the plane then the distance is zero.
  89. float alpha = (normal.dot(_origin) + plane.getDistance());
  90. if (fabs(alpha) < MATH_EPSILON)
  91. {
  92. return 0.0f;
  93. }
  94. float dot = normal.dot(_direction);
  95. // If the dot product of the plane's normal and this ray's direction is zero,
  96. // then the ray is parallel to the plane and does not intersect it.
  97. if ( dot == 0.0f )
  98. {
  99. return INTERSECTS_NONE;
  100. }
  101. // Calculate the distance along the ray's direction vector to the point where
  102. // the ray intersects the plane (if it is negative the plane is behind the ray).
  103. float d = -alpha / dot;
  104. if (d < 0.0f)
  105. {
  106. return INTERSECTS_NONE;
  107. }
  108. return d;
  109. }
  110. void Ray::set(const Vector3& origin, const Vector3& direction)
  111. {
  112. _origin = origin;
  113. _direction = direction;
  114. normalize();
  115. }
  116. void Ray::set(const Ray& ray)
  117. {
  118. _origin = ray._origin;
  119. _direction = ray._direction;
  120. normalize();
  121. }
  122. void Ray::transform(const Matrix& matrix)
  123. {
  124. matrix.transformPoint(&_origin);
  125. matrix.transformVector(&_direction);
  126. _direction.normalize();
  127. }
  128. void Ray::normalize()
  129. {
  130. if (_direction.isZero())
  131. return;
  132. // Normalize the ray's direction vector.
  133. float normalizeFactor = 1.0f / sqrt(_direction.x * _direction.x + _direction.y * _direction.y + _direction.z * _direction.z);
  134. if ( normalizeFactor != 1.0f )
  135. {
  136. _direction.x *= normalizeFactor;
  137. _direction.y *= normalizeFactor;
  138. _direction.z *= normalizeFactor;
  139. }
  140. }
  141. }