CCRay.cpp 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  1. /****************************************************************************
  2. Copyright (c) 2014-2017 Chukong Technologies Inc.
  3. Permission is hereby granted, free of charge, to any person obtaining a copy
  4. of this software and associated documentation files (the "Software"), to deal
  5. in the Software without restriction, including without limitation the rights
  6. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  7. copies of the Software, and to permit persons to whom the Software is
  8. furnished to do so, subject to the following conditions:
  9. The above copyright notice and this permission notice shall be included in
  10. all copies or substantial portions of the Software.
  11. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  12. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  13. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  14. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  15. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  16. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  17. THE SOFTWARE.
  18. ****************************************************************************/
  19. #include "3d/CCRay.h"
  20. NS_CC_BEGIN
  21. Ray::Ray()
  22. : _direction(0, 0, 1)
  23. {
  24. }
  25. Ray::Ray(const Ray& ray)
  26. {
  27. set(ray._origin, ray._direction);
  28. }
  29. Ray::Ray(const Vec3& origin, const Vec3& direction)
  30. {
  31. set(origin, direction);
  32. }
  33. Ray::~Ray()
  34. {
  35. }
  36. bool Ray::intersects(const AABB& box, float* distance) const
  37. {
  38. float lowt = 0.0f;
  39. float t;
  40. bool hit = false;
  41. Vec3 hitpoint;
  42. const Vec3& min = box._min;
  43. const Vec3& max = box._max;
  44. const Vec3& rayorig = _origin;
  45. const Vec3& raydir = _direction;
  46. // Check origin inside first
  47. if (rayorig > min && rayorig < max)
  48. return true;
  49. // Check each face in turn, only check closest 3
  50. // Min x
  51. if (rayorig.x <= min.x && raydir.x > 0)
  52. {
  53. t = (min.x - rayorig.x) / raydir.x;
  54. if (t >= 0)
  55. {
  56. // Substitute t back into ray and check bounds and dist
  57. hitpoint = rayorig + raydir * t;
  58. if (hitpoint.y >= min.y && hitpoint.y <= max.y &&
  59. hitpoint.z >= min.z && hitpoint.z <= max.z &&
  60. (!hit || t < lowt))
  61. {
  62. hit = true;
  63. lowt = t;
  64. }
  65. }
  66. }
  67. // Max x
  68. if (rayorig.x >= max.x && raydir.x < 0)
  69. {
  70. t = (max.x - rayorig.x) / raydir.x;
  71. if (t >= 0)
  72. {
  73. // Substitute t back into ray and check bounds and dist
  74. hitpoint = rayorig + raydir * t;
  75. if (hitpoint.y >= min.y && hitpoint.y <= max.y &&
  76. hitpoint.z >= min.z && hitpoint.z <= max.z &&
  77. (!hit || t < lowt))
  78. {
  79. hit = true;
  80. lowt = t;
  81. }
  82. }
  83. }
  84. // Min y
  85. if (rayorig.y <= min.y && raydir.y > 0)
  86. {
  87. t = (min.y - rayorig.y) / raydir.y;
  88. if (t >= 0)
  89. {
  90. // Substitute t back into ray and check bounds and dist
  91. hitpoint = rayorig + raydir * t;
  92. if (hitpoint.x >= min.x && hitpoint.x <= max.x &&
  93. hitpoint.z >= min.z && hitpoint.z <= max.z &&
  94. (!hit || t < lowt))
  95. {
  96. hit = true;
  97. lowt = t;
  98. }
  99. }
  100. }
  101. // Max y
  102. if (rayorig.y >= max.y && raydir.y < 0)
  103. {
  104. t = (max.y - rayorig.y) / raydir.y;
  105. if
  106. (t >= 0)
  107. {
  108. // Substitute t back into ray and check bounds and dist
  109. hitpoint = rayorig + raydir * t;
  110. if (hitpoint.x >= min.x && hitpoint.x <= max.x &&
  111. hitpoint.z >= min.z && hitpoint.z <= max.z &&
  112. (!hit || t < lowt))
  113. {
  114. hit = true;
  115. lowt = t;
  116. }
  117. }
  118. }
  119. // Min z
  120. if (rayorig.z <= min.z && raydir.z > 0)
  121. {
  122. t = (min.z - rayorig.z) / raydir.z;
  123. if (t >= 0)
  124. {
  125. // Substitute t back into ray and check bounds and dist
  126. hitpoint = rayorig + raydir * t;
  127. if (hitpoint.x >= min.x && hitpoint.x <= max.x &&
  128. hitpoint.y >= min.y && hitpoint.y <= max.y &&
  129. (!hit || t < lowt))
  130. {
  131. hit = true;
  132. lowt = t;
  133. }
  134. }
  135. }
  136. // Max z
  137. if (rayorig.z >= max.z && raydir.z < 0)
  138. {
  139. t = (max.z - rayorig.z) / raydir.z;
  140. if (t >= 0)
  141. {
  142. // Substitute t back into ray and check bounds and dist
  143. hitpoint = rayorig + raydir * t;
  144. if (hitpoint.x >= min.x && hitpoint.x <= max.x &&
  145. hitpoint.y >= min.y && hitpoint.y <= max.y &&
  146. (!hit || t < lowt))
  147. {
  148. hit = true;
  149. lowt = t;
  150. }
  151. }
  152. }
  153. if (distance)
  154. *distance = lowt;
  155. return hit;
  156. }
  157. bool Ray::intersects(const OBB& obb, float* distance) const
  158. {
  159. AABB aabb;
  160. aabb._min = - obb._extents;
  161. aabb._max = obb._extents;
  162. Ray ray;
  163. ray._direction = _direction;
  164. ray._origin = _origin;
  165. Mat4 mat = Mat4::IDENTITY;
  166. mat.m[0] = obb._xAxis.x;
  167. mat.m[1] = obb._xAxis.y;
  168. mat.m[2] = obb._xAxis.z;
  169. mat.m[4] = obb._yAxis.x;
  170. mat.m[5] = obb._yAxis.y;
  171. mat.m[6] = obb._yAxis.z;
  172. mat.m[8] = obb._zAxis.x;
  173. mat.m[9] = obb._zAxis.y;
  174. mat.m[10] = obb._zAxis.z;
  175. mat.m[12] = obb._center.x;
  176. mat.m[13] = obb._center.y;
  177. mat.m[14] = obb._center.z;
  178. mat = mat.getInversed();
  179. ray.transform(mat);
  180. return ray.intersects(aabb, distance);
  181. }
  182. float Ray::dist(const Plane& plane) const
  183. {
  184. float ndd = Vec3::dot(plane.getNormal(), _direction);
  185. if(ndd == 0)
  186. return 0.0f;
  187. float ndo = Vec3::dot(plane.getNormal(), _origin);
  188. return (plane.getDist() - ndo) / ndd;
  189. }
  190. Vec3 Ray::intersects(const Plane& plane) const
  191. {
  192. float dis = this->dist(plane);
  193. return _origin + dis * _direction;
  194. }
  195. void Ray::set(const Vec3& origin, const Vec3& direction)
  196. {
  197. _origin = origin;
  198. _direction = direction;
  199. _direction.normalize();
  200. }
  201. void Ray::transform(const Mat4& matrix)
  202. {
  203. matrix.transformPoint(&_origin);
  204. matrix.transformVector(&_direction);
  205. _direction.normalize();
  206. }
  207. NS_CC_END