FunctionsTestCollision.cpp 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418
  1. // Copyright (C) 2009-present, Panagiotis Christopoulos Charitos and contributors.
  2. // All rights reserved.
  3. // Code licensed under the BSD License.
  4. // http://www.anki3d.org/LICENSE
  5. #include <AnKi/Collision/Functions.h>
  6. #include <AnKi/Collision/ConvexHullShape.h>
  7. #include <AnKi/Collision/Obb.h>
  8. #include <AnKi/Collision/LineSegment.h>
  9. #include <AnKi/Collision/Cone.h>
  10. #include <AnKi/Collision/Sphere.h>
  11. #include <AnKi/Collision/GjkEpa.h>
  12. namespace anki {
  13. template<typename T, typename Y>
  14. static Bool testCollisionGjk(const T& a, const Y& b)
  15. {
  16. auto callbackA = [](const void* shape, const Vec4& dir) {
  17. return static_cast<const T*>(shape)->computeSupport(dir);
  18. };
  19. auto callbackB = [](const void* shape, const Vec4& dir) {
  20. return static_cast<const Y*>(shape)->computeSupport(dir);
  21. };
  22. return gjkIntersection(&a, callbackA, &b, callbackB);
  23. }
  24. Bool testCollision(const Aabb& a, const Aabb& b)
  25. {
  26. #if ANKI_SIMD_SSE
  27. const __m128 gt0 = _mm_cmpgt_ps(a.getMin().getSimd(), b.getMax().getSimd());
  28. const __m128 gt1 = _mm_cmpgt_ps(b.getMin().getSimd(), a.getMax().getSimd());
  29. const __m128 combined = _mm_or_ps(gt0, gt1);
  30. const int res = _mm_movemask_ps(combined); // Will set the first bit of each byte of combined
  31. return res == 0;
  32. #else
  33. // if separated in x direction
  34. if(a.getMin().x() > b.getMax().x() || b.getMin().x() > a.getMax().x())
  35. {
  36. return false;
  37. }
  38. // if separated in y direction
  39. if(a.getMin().y() > b.getMax().y() || b.getMin().y() > a.getMax().y())
  40. {
  41. return false;
  42. }
  43. // if separated in z direction
  44. if(a.getMin().z() > b.getMax().z() || b.getMin().z() > a.getMax().z())
  45. {
  46. return false;
  47. }
  48. // no separation, must be intersecting
  49. return true;
  50. #endif
  51. }
  52. Bool testCollision(const Aabb& aabb, const Sphere& s)
  53. {
  54. const Vec4& c = s.getCenter();
  55. // Find the box's closest point to the sphere
  56. #if ANKI_SIMD_SSE
  57. __m128 gt = _mm_cmpgt_ps(c.getSimd(), aabb.getMax().getSimd());
  58. __m128 lt = _mm_cmplt_ps(c.getSimd(), aabb.getMin().getSimd());
  59. __m128 m = _mm_or_ps(_mm_and_ps(gt, aabb.getMax().getSimd()), _mm_andnot_ps(gt, c.getSimd()));
  60. __m128 n = _mm_or_ps(_mm_and_ps(lt, aabb.getMin().getSimd()), _mm_andnot_ps(lt, m));
  61. const Vec4 cp(n);
  62. #else
  63. Vec4 cp(c); // Closest Point
  64. for(U i = 0; i < 3; i++)
  65. {
  66. // if the center is greater than the max then the closest point is the max
  67. if(c[i] > aabb.getMax()[i])
  68. {
  69. cp[i] = aabb.getMax()[i];
  70. }
  71. else if(c[i] < aabb.getMin()[i]) // relative to the above
  72. {
  73. cp[i] = aabb.getMin()[i];
  74. }
  75. else
  76. {
  77. // the c lies between min and max, do nothing
  78. }
  79. }
  80. #endif
  81. // if the c lies totally inside the box then the sub is the zero, this means that the length is also zero and thus
  82. // it's always smaller than rsq
  83. const Vec4 sub = c - cp;
  84. return (sub.lengthSquared() <= (s.getRadius() * s.getRadius())) ? true : false;
  85. }
  86. Bool testCollision(const Aabb& aabb, const Obb& obb)
  87. {
  88. return testCollisionGjk(aabb, obb);
  89. }
  90. Bool testCollision(const Aabb& aabb, const ConvexHullShape& hull)
  91. {
  92. return testCollisionGjk(aabb, hull);
  93. }
  94. Bool testCollision(const Aabb& aabb, const LineSegment& ls)
  95. {
  96. F32 maxS = kMinF32;
  97. F32 minT = kMaxF32;
  98. // do tests against three sets of planes
  99. for(U i = 0; i < 3; ++i)
  100. {
  101. // segment is parallel to plane
  102. if(isZero(ls.getDirection()[i]))
  103. {
  104. // segment passes by box
  105. if(ls.getOrigin()[i] < aabb.getMin()[i] || ls.getOrigin()[i] > aabb.getMax()[i])
  106. {
  107. return false;
  108. }
  109. }
  110. else
  111. {
  112. // compute intersection parameters and sort
  113. F32 s = (aabb.getMin()[i] - ls.getOrigin()[i]) / ls.getDirection()[i];
  114. F32 t = (aabb.getMax()[i] - ls.getOrigin()[i]) / ls.getDirection()[i];
  115. if(s > t)
  116. {
  117. F32 temp = s;
  118. s = t;
  119. t = temp;
  120. }
  121. // adjust min and max values
  122. if(s > maxS)
  123. {
  124. maxS = s;
  125. }
  126. if(t < minT)
  127. {
  128. minT = t;
  129. }
  130. // check for intersection failure
  131. if(minT < 0.0 || maxS > 1.0 || maxS > minT)
  132. {
  133. return false;
  134. }
  135. }
  136. }
  137. // done, have intersection
  138. return true;
  139. }
  140. Bool testCollision([[maybe_unused]] const Aabb& aabb, [[maybe_unused]] const Cone& cone)
  141. {
  142. ANKI_ASSERT(!"TODO");
  143. return false;
  144. }
  145. Bool testCollision(const Sphere& a, const Sphere& b)
  146. {
  147. const F32 tmp = a.getRadius() + b.getRadius();
  148. return (a.getCenter() - b.getCenter()).lengthSquared() <= tmp * tmp;
  149. }
  150. Bool testCollision(const Sphere& sphere, const Obb& obb)
  151. {
  152. return testCollisionGjk(sphere, obb);
  153. }
  154. Bool testCollision(const Sphere& sphere, const ConvexHullShape& hull)
  155. {
  156. return testCollisionGjk(sphere, hull);
  157. }
  158. Bool testCollision(const Sphere& s, const LineSegment& ls)
  159. {
  160. const Vec4& v = ls.getDirection();
  161. const Vec4 w0 = s.getCenter() - ls.getOrigin();
  162. const F32 w0dv = w0.dot(v);
  163. const F32 rsq = s.getRadius() * s.getRadius();
  164. if(w0dv < 0.0f) // if the ang is >90
  165. {
  166. return w0.lengthSquared() <= rsq;
  167. }
  168. const Vec4 w1 = w0 - v; // aka center - P1, where P1 = seg.origin + seg.dir
  169. const F32 w1dv = w1.dot(v);
  170. if(w1dv > 0.0f) // if the ang is <90
  171. {
  172. return w1.lengthSquared() <= rsq;
  173. }
  174. // the big parenthesis is the projection of w0 to v
  175. const Vec4 tmp = w0 - (v * (w0.dot(v) / v.lengthSquared()));
  176. return tmp.lengthSquared() <= rsq;
  177. }
  178. Bool testCollision(const Sphere& sphere, const Cone& cone)
  179. {
  180. // https://bartwronski.com/2017/04/13/cull-that-cone/
  181. const F32 coneAngle = cone.getAngle() / 2.0f;
  182. const Vec4 V = sphere.getCenter() - cone.getOrigin();
  183. const F32 VlenSq = V.dot(V);
  184. const F32 V1len = V.dot(cone.getDirection());
  185. const F32 distanceClosestPoint = cos(coneAngle) * sqrt(VlenSq - V1len * V1len) - V1len * sin(coneAngle);
  186. const Bool angleCull = distanceClosestPoint > sphere.getRadius();
  187. const Bool frontCull = V1len > sphere.getRadius() + cone.getLength();
  188. const Bool backCull = V1len < -sphere.getRadius();
  189. return !(angleCull || frontCull || backCull);
  190. }
  191. Bool testCollision(const Obb& a, const Obb& b)
  192. {
  193. return testCollisionGjk(a, b);
  194. }
  195. Bool testCollision(const Obb& obb, const ConvexHullShape& hull)
  196. {
  197. return testCollisionGjk(obb, hull);
  198. }
  199. Bool testCollision(const Obb& obb, const LineSegment& ls)
  200. {
  201. F32 maxS = kMinF32;
  202. F32 minT = kMaxF32;
  203. // compute difference vector
  204. const Vec4 diff = obb.getCenter() - ls.getOrigin();
  205. // for each axis do
  206. for(U i = 0; i < 3; ++i)
  207. {
  208. // get axis i
  209. const Vec4 axis = obb.getRotation().getColumn(i).xyz0();
  210. // project relative vector onto axis
  211. const F32 e = axis.dot(diff);
  212. const F32 f = ls.getDirection().dot(axis);
  213. // ray is parallel to plane
  214. if(isZero(f))
  215. {
  216. // ray passes by box
  217. if(-e - obb.getExtend()[i] > 0.0 || -e + obb.getExtend()[i] > 0.0)
  218. {
  219. return false;
  220. }
  221. continue;
  222. }
  223. F32 s = (e - obb.getExtend()[i]) / f;
  224. F32 t = (e + obb.getExtend()[i]) / f;
  225. // fix order
  226. if(s > t)
  227. {
  228. F32 temp = s;
  229. s = t;
  230. t = temp;
  231. }
  232. // adjust min and max values
  233. if(s > maxS)
  234. {
  235. maxS = s;
  236. }
  237. if(t < minT)
  238. {
  239. minT = t;
  240. }
  241. // check for intersection failure
  242. if(minT < 0.0 || maxS > 1.0 || maxS > minT)
  243. {
  244. return false;
  245. }
  246. }
  247. // done, have intersection
  248. return true;
  249. }
  250. Bool testCollision([[maybe_unused]] const Obb& obb, [[maybe_unused]] const Cone& cone)
  251. {
  252. ANKI_ASSERT(!"TODO");
  253. return false;
  254. }
  255. Bool testCollision(const ConvexHullShape& a, const ConvexHullShape& b)
  256. {
  257. return testCollisionGjk(a, b);
  258. }
  259. Bool testCollision([[maybe_unused]] const ConvexHullShape& hull, [[maybe_unused]] const LineSegment& ls)
  260. {
  261. ANKI_ASSERT(!"TODO");
  262. return false;
  263. }
  264. Bool testCollision([[maybe_unused]] const ConvexHullShape& hull, [[maybe_unused]] const Cone& cone)
  265. {
  266. ANKI_ASSERT(!"TODO");
  267. return false;
  268. }
  269. Bool testCollision([[maybe_unused]] const LineSegment& a, [[maybe_unused]] const LineSegment& b)
  270. {
  271. ANKI_ASSERT(!"TODO");
  272. return false;
  273. }
  274. Bool testCollision([[maybe_unused]] const Cone& a, [[maybe_unused]] const Cone& b)
  275. {
  276. ANKI_ASSERT(!"TODO");
  277. return false;
  278. }
  279. Bool testCollision(const Plane& plane, const Ray& ray, Vec4& intersection)
  280. {
  281. Bool intersects = false;
  282. const F32 d = testPlane(plane, ray.getOrigin()); // Dist of origin to the plane
  283. const F32 a = plane.getNormal().dot(ray.getDirection());
  284. if(d > 0.0f && a < 0.0f)
  285. {
  286. // To have intersection the d should be positive and the s as well. So the 'a' must be negative and not zero
  287. // because of the division.
  288. const F32 s = -d / a;
  289. ANKI_ASSERT(s > 0.0f);
  290. intersection = ray.getOrigin() + s * ray.getDirection();
  291. intersects = true;
  292. }
  293. return intersects;
  294. }
  295. Bool testCollision(const Plane& plane, const Vec4& vector, Vec4& intersection)
  296. {
  297. ANKI_ASSERT(vector.w() == 0.0f);
  298. const Vec4 pp = vector.normalize();
  299. const F32 dot = pp.dot(plane.getNormal());
  300. if(!isZero(dot))
  301. {
  302. const F32 s = plane.getOffset() / dot;
  303. intersection = pp * s;
  304. return true;
  305. }
  306. else
  307. {
  308. return false;
  309. }
  310. }
  311. Bool intersect(const Sphere& sphere, const Ray& ray, Array<Vec4, 2>& intersectionPoints, U& intersectionPointCount)
  312. {
  313. ANKI_ASSERT(isZero(ray.getDirection().lengthSquared() - 1.0f));
  314. // See https://en.wikipedia.org/wiki/Line%E2%80%93sphere_intersection
  315. const Vec4& o = ray.getOrigin();
  316. const Vec4& l = ray.getDirection();
  317. const Vec4& c = sphere.getCenter();
  318. const F32 R2 = sphere.getRadius() * sphere.getRadius();
  319. const Vec4 o_c = o - c;
  320. const F32 a = l.dot(o_c);
  321. const F32 b = a * a - o_c.lengthSquared() + R2;
  322. if(b < 0.0f)
  323. {
  324. intersectionPointCount = 0;
  325. return false;
  326. }
  327. else if(b == 0.0f)
  328. {
  329. intersectionPointCount = 1;
  330. intersectionPoints[0] = -a * l + o;
  331. return true;
  332. }
  333. else
  334. {
  335. F32 d = -a - sqrt(b);
  336. intersectionPointCount = 0;
  337. if(d > 0.0f)
  338. {
  339. intersectionPointCount = 1;
  340. intersectionPoints[0] = d * l + o;
  341. }
  342. d = -a + sqrt(b);
  343. if(d > 0.0f)
  344. {
  345. intersectionPoints[intersectionPointCount++] = d * l + o;
  346. }
  347. return intersectionPointCount > 0;
  348. }
  349. }
  350. } // end namespace anki