CCRay.cpp 6.4 KB

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