intersection.cpp 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  1. /*
  2. * Copyright (c) 2012-2014 Daniele Bartolini and individual contributors.
  3. * License: https://github.com/taylor001/crown/blob/master/LICENSE
  4. */
  5. #include "intersection.h"
  6. #include "aabb.h"
  7. #include "plane.h"
  8. #include "sphere.h"
  9. #include "vector3.h"
  10. namespace crown
  11. {
  12. /// Returns the distance along ray (from, dir) to intersection point with plane @a p.
  13. /// -1.0f if no collision.
  14. float ray_plane_intersection(const Vector3& from, const Vector3& dir, const Plane& p)
  15. {
  16. float nd = vector3::dot(dir, p.n);
  17. float orpn = vector3::dot(from, p.n);
  18. float dist = -1.0f;
  19. if (nd < 0.0f)
  20. dist = (-p.d - orpn) / nd;
  21. return dist > 0.0f ? dist : -1.0f;
  22. }
  23. /// Returns the distance along ray (from, dir) to intersection point with sphere @a s.
  24. /// -1.0f if no collision.
  25. float ray_sphere_intersection(const Vector3& from, const Vector3& dir, const Sphere& s)
  26. {
  27. Vector3 v = s.c - from;
  28. float b = vector3::dot(v, dir);
  29. float det = (s.r * s.r) - vector3::dot(v, v) + (b * b);
  30. if (det < 0.0 || b < s.r)
  31. {
  32. return -1.0f;
  33. }
  34. return b - sqrt(det);
  35. }
  36. // http://www.opengl-tutorial.org/miscellaneous/clicking-on-objects/picking-with-custom-ray-obb-function/
  37. float ray_oobb_intersection(const Vector3& from, const Vector3& dir, const OBB& obb)
  38. {
  39. using namespace vector3;
  40. float tmin = 0.0f;
  41. float tmax = 100000.0f;
  42. Vector3 obb_pos(obb.tm.t.x, obb.tm.t.y, obb.tm.t.z);
  43. Vector3 delta = obb_pos - from;
  44. {
  45. const Vector3 xaxis(obb.tm.x.x, obb.tm.x.y, obb.tm.x.z);
  46. float e = dot(xaxis, delta);
  47. float f = dot(dir, xaxis);
  48. if (fabs(f) > 0.001f)
  49. {
  50. float t1 = (e+obb.aabb.min.x)/f;
  51. float t2 = (e+obb.aabb.max.x)/f;
  52. if (t1>t2){
  53. float w=t1;t1=t2;t2=w;
  54. }
  55. if (t2 < tmax)
  56. tmax = t2;
  57. if (t1 > tmin)
  58. tmin = t1;
  59. if (tmax < tmin)
  60. return -1.0f;
  61. }
  62. else
  63. {
  64. if(-e+obb.aabb.min.x > 0.0f || -e+obb.aabb.max.x < 0.0f)
  65. return -1.0f;
  66. }
  67. }
  68. {
  69. const Vector3 yaxis(obb.tm.y.x, obb.tm.y.y, obb.tm.y.z);
  70. float e = dot(yaxis, delta);
  71. float f = dot(dir, yaxis);
  72. if (fabs(f) > 0.001f){
  73. float t1 = (e+obb.aabb.min.y)/f;
  74. float t2 = (e+obb.aabb.max.y)/f;
  75. if (t1>t2){float w=t1;t1=t2;t2=w;}
  76. if (t2 < tmax)
  77. tmax = t2;
  78. if (t1 > tmin)
  79. tmin = t1;
  80. if (tmin > tmax)
  81. return -1.0f;
  82. }
  83. else
  84. {
  85. if(-e+obb.aabb.min.y > 0.0f || -e+obb.aabb.max.y < 0.0f)
  86. return -1.0f;
  87. }
  88. }
  89. {
  90. const Vector3 zaxis(obb.tm.z.x, obb.tm.z.y, obb.tm.z.z);
  91. float e = dot(zaxis, delta);
  92. float f = dot(dir, zaxis);
  93. if (fabs(f) > 0.001f){
  94. float t1 = (e+obb.aabb.min.z)/f;
  95. float t2 = (e+obb.aabb.max.z)/f;
  96. if (t1>t2){float w=t1;t1=t2;t2=w;}
  97. if (t2 < tmax)
  98. tmax = t2;
  99. if (t1 > tmin)
  100. tmin = t1;
  101. if (tmin > tmax)
  102. return -1.0f;
  103. }
  104. else
  105. {
  106. if(-e+obb.aabb.min.z > 0.0f || -e+obb.aabb.max.z < 0.0f)
  107. return -1.0f;
  108. }
  109. }
  110. return tmin;
  111. }
  112. bool plane_3_intersection(const Plane& p1, const Plane& p2, const Plane& p3, Vector3& ip)
  113. {
  114. const Vector3& n1 = p1.n;
  115. const Vector3& n2 = p2.n;
  116. const Vector3& n3 = p3.n;
  117. float den = -vector3::dot(vector3::cross(n1, n2), n3);
  118. if (equals(den, (float)0.0))
  119. {
  120. return false;
  121. }
  122. Vector3 res = p1.d * vector3::cross(n2, n3) + p2.d * vector3::cross(n3, n1) + p3.d * vector3::cross(n1, n2);
  123. ip = res / den;
  124. return true;
  125. }
  126. bool frustum_sphere_intersection(const Frustum& f, const Sphere& s)
  127. {
  128. if (plane::distance_to_point(f.left, s.c) < -s.r ||
  129. plane::distance_to_point(f.right, s.c) < -s.r)
  130. {
  131. return false;
  132. }
  133. if (plane::distance_to_point(f.bottom, s.c) < -s.r ||
  134. plane::distance_to_point(f.top, s.c) < -s.r)
  135. {
  136. return false;
  137. }
  138. if (plane::distance_to_point(f.near, s.c) < -s.r ||
  139. plane::distance_to_point(f.far, s.c) < -s.r)
  140. {
  141. return false;
  142. }
  143. return true;
  144. }
  145. bool frustum_box_intersection(const Frustum& f, const AABB& b)
  146. {
  147. uint8_t out;
  148. out = 0;
  149. if (plane::distance_to_point(f.left, aabb::vertex(b, 0)) < 0.0) out++;
  150. if (plane::distance_to_point(f.left, aabb::vertex(b, 1)) < 0.0) out++;
  151. if (plane::distance_to_point(f.left, aabb::vertex(b, 2)) < 0.0) out++;
  152. if (plane::distance_to_point(f.left, aabb::vertex(b, 3)) < 0.0) out++;
  153. if (plane::distance_to_point(f.left, aabb::vertex(b, 4)) < 0.0) out++;
  154. if (plane::distance_to_point(f.left, aabb::vertex(b, 5)) < 0.0) out++;
  155. if (plane::distance_to_point(f.left, aabb::vertex(b, 6)) < 0.0) out++;
  156. if (plane::distance_to_point(f.left, aabb::vertex(b, 7)) < 0.0) out++;
  157. // If all vertices are outside one face, then the box doesn't intersect the frustum
  158. if (out == 8) return false;
  159. out = 0;
  160. if (plane::distance_to_point(f.right, aabb::vertex(b, 0)) < 0.0) out++;
  161. if (plane::distance_to_point(f.right, aabb::vertex(b, 1)) < 0.0) out++;
  162. if (plane::distance_to_point(f.right, aabb::vertex(b, 2)) < 0.0) out++;
  163. if (plane::distance_to_point(f.right, aabb::vertex(b, 3)) < 0.0) out++;
  164. if (plane::distance_to_point(f.right, aabb::vertex(b, 4)) < 0.0) out++;
  165. if (plane::distance_to_point(f.right, aabb::vertex(b, 5)) < 0.0) out++;
  166. if (plane::distance_to_point(f.right, aabb::vertex(b, 6)) < 0.0) out++;
  167. if (plane::distance_to_point(f.right, aabb::vertex(b, 7)) < 0.0) out++;
  168. if (out == 8) return false;
  169. out = 0;
  170. if (plane::distance_to_point(f.bottom, aabb::vertex(b, 0)) < 0.0) out++;
  171. if (plane::distance_to_point(f.bottom, aabb::vertex(b, 1)) < 0.0) out++;
  172. if (plane::distance_to_point(f.bottom, aabb::vertex(b, 2)) < 0.0) out++;
  173. if (plane::distance_to_point(f.bottom, aabb::vertex(b, 3)) < 0.0) out++;
  174. if (plane::distance_to_point(f.bottom, aabb::vertex(b, 4)) < 0.0) out++;
  175. if (plane::distance_to_point(f.bottom, aabb::vertex(b, 5)) < 0.0) out++;
  176. if (plane::distance_to_point(f.bottom, aabb::vertex(b, 6)) < 0.0) out++;
  177. if (plane::distance_to_point(f.bottom, aabb::vertex(b, 7)) < 0.0) out++;
  178. if (out == 8) return false;
  179. out = 0;
  180. if (plane::distance_to_point(f.top, aabb::vertex(b, 0)) < 0.0) out++;
  181. if (plane::distance_to_point(f.top, aabb::vertex(b, 1)) < 0.0) out++;
  182. if (plane::distance_to_point(f.top, aabb::vertex(b, 2)) < 0.0) out++;
  183. if (plane::distance_to_point(f.top, aabb::vertex(b, 3)) < 0.0) out++;
  184. if (plane::distance_to_point(f.top, aabb::vertex(b, 4)) < 0.0) out++;
  185. if (plane::distance_to_point(f.top, aabb::vertex(b, 5)) < 0.0) out++;
  186. if (plane::distance_to_point(f.top, aabb::vertex(b, 6)) < 0.0) out++;
  187. if (plane::distance_to_point(f.top, aabb::vertex(b, 7)) < 0.0) out++;
  188. if (out == 8) return false;
  189. out = 0;
  190. if (plane::distance_to_point(f.near, aabb::vertex(b, 0)) < 0.0) out++;
  191. if (plane::distance_to_point(f.near, aabb::vertex(b, 1)) < 0.0) out++;
  192. if (plane::distance_to_point(f.near, aabb::vertex(b, 2)) < 0.0) out++;
  193. if (plane::distance_to_point(f.near, aabb::vertex(b, 3)) < 0.0) out++;
  194. if (plane::distance_to_point(f.near, aabb::vertex(b, 4)) < 0.0) out++;
  195. if (plane::distance_to_point(f.near, aabb::vertex(b, 5)) < 0.0) out++;
  196. if (plane::distance_to_point(f.near, aabb::vertex(b, 6)) < 0.0) out++;
  197. if (plane::distance_to_point(f.near, aabb::vertex(b, 7)) < 0.0) out++;
  198. if (out == 8) return false;
  199. out = 0;
  200. if (plane::distance_to_point(f.far, aabb::vertex(b, 0)) < 0.0) out++;
  201. if (plane::distance_to_point(f.far, aabb::vertex(b, 1)) < 0.0) out++;
  202. if (plane::distance_to_point(f.far, aabb::vertex(b, 2)) < 0.0) out++;
  203. if (plane::distance_to_point(f.far, aabb::vertex(b, 3)) < 0.0) out++;
  204. if (plane::distance_to_point(f.far, aabb::vertex(b, 4)) < 0.0) out++;
  205. if (plane::distance_to_point(f.far, aabb::vertex(b, 5)) < 0.0) out++;
  206. if (plane::distance_to_point(f.far, aabb::vertex(b, 6)) < 0.0) out++;
  207. if (plane::distance_to_point(f.far, aabb::vertex(b, 7)) < 0.0) out++;
  208. if (out == 8) return false;
  209. // If we are here, it is because either the box intersects or it is contained in the frustum
  210. return true;
  211. }
  212. } // namespace crown