b2TimeOfImpact.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484
  1. /*
  2. * Copyright (c) 2007-2009 Erin Catto http://www.box2d.org
  3. *
  4. * This software is provided 'as-is', without any express or implied
  5. * warranty. In no event will the authors be held liable for any damages
  6. * arising from the use of this software.
  7. * Permission is granted to anyone to use this software for any purpose,
  8. * including commercial applications, and to alter it and redistribute it
  9. * freely, subject to the following restrictions:
  10. * 1. The origin of this software must not be misrepresented; you must not
  11. * claim that you wrote the original software. If you use this software
  12. * in a product, an acknowledgment in the product documentation would be
  13. * appreciated but is not required.
  14. * 2. Altered source versions must be plainly marked as such, and must not be
  15. * misrepresented as being the original software.
  16. * 3. This notice may not be removed or altered from any source distribution.
  17. */
  18. #include <Box2D/Collision/b2Collision.h>
  19. #include <Box2D/Collision/b2Distance.h>
  20. #include <Box2D/Collision/b2TimeOfImpact.h>
  21. #include <Box2D/Collision/Shapes/b2CircleShape.h>
  22. #include <Box2D/Collision/Shapes/b2PolygonShape.h>
  23. #include <cstdio>
  24. using namespace std;
  25. int32 b2_toiCalls, b2_toiIters, b2_toiMaxIters;
  26. int32 b2_toiRootIters, b2_toiMaxRootIters;
  27. struct b2SeparationFunction
  28. {
  29. enum Type
  30. {
  31. e_points,
  32. e_faceA,
  33. e_faceB
  34. };
  35. // TODO_ERIN might not need to return the separation
  36. float32 Initialize(const b2SimplexCache* cache,
  37. const b2DistanceProxy* proxyA, const b2Sweep& sweepA,
  38. const b2DistanceProxy* proxyB, const b2Sweep& sweepB,
  39. float32 t1)
  40. {
  41. m_proxyA = proxyA;
  42. m_proxyB = proxyB;
  43. int32 count = cache->count;
  44. b2Assert(0 < count && count < 3);
  45. m_sweepA = sweepA;
  46. m_sweepB = sweepB;
  47. b2Transform xfA, xfB;
  48. m_sweepA.GetTransform(&xfA, t1);
  49. m_sweepB.GetTransform(&xfB, t1);
  50. if (count == 1)
  51. {
  52. m_type = e_points;
  53. b2Vec2 localPointA = m_proxyA->GetVertex(cache->indexA[0]);
  54. b2Vec2 localPointB = m_proxyB->GetVertex(cache->indexB[0]);
  55. b2Vec2 pointA = b2Mul(xfA, localPointA);
  56. b2Vec2 pointB = b2Mul(xfB, localPointB);
  57. m_axis = pointB - pointA;
  58. float32 s = m_axis.Normalize();
  59. return s;
  60. }
  61. else if (cache->indexA[0] == cache->indexA[1])
  62. {
  63. // Two points on B and one on A.
  64. m_type = e_faceB;
  65. b2Vec2 localPointB1 = proxyB->GetVertex(cache->indexB[0]);
  66. b2Vec2 localPointB2 = proxyB->GetVertex(cache->indexB[1]);
  67. m_axis = b2Cross(localPointB2 - localPointB1, 1.0f);
  68. m_axis.Normalize();
  69. b2Vec2 normal = b2Mul(xfB.q, m_axis);
  70. m_localPoint = 0.5f * (localPointB1 + localPointB2);
  71. b2Vec2 pointB = b2Mul(xfB, m_localPoint);
  72. b2Vec2 localPointA = proxyA->GetVertex(cache->indexA[0]);
  73. b2Vec2 pointA = b2Mul(xfA, localPointA);
  74. float32 s = b2Dot(pointA - pointB, normal);
  75. if (s < 0.0f)
  76. {
  77. m_axis = -m_axis;
  78. s = -s;
  79. }
  80. return s;
  81. }
  82. else
  83. {
  84. // Two points on A and one or two points on B.
  85. m_type = e_faceA;
  86. b2Vec2 localPointA1 = m_proxyA->GetVertex(cache->indexA[0]);
  87. b2Vec2 localPointA2 = m_proxyA->GetVertex(cache->indexA[1]);
  88. m_axis = b2Cross(localPointA2 - localPointA1, 1.0f);
  89. m_axis.Normalize();
  90. b2Vec2 normal = b2Mul(xfA.q, m_axis);
  91. m_localPoint = 0.5f * (localPointA1 + localPointA2);
  92. b2Vec2 pointA = b2Mul(xfA, m_localPoint);
  93. b2Vec2 localPointB = m_proxyB->GetVertex(cache->indexB[0]);
  94. b2Vec2 pointB = b2Mul(xfB, localPointB);
  95. float32 s = b2Dot(pointB - pointA, normal);
  96. if (s < 0.0f)
  97. {
  98. m_axis = -m_axis;
  99. s = -s;
  100. }
  101. return s;
  102. }
  103. }
  104. float32 FindMinSeparation(int32* indexA, int32* indexB, float32 t) const
  105. {
  106. b2Transform xfA, xfB;
  107. m_sweepA.GetTransform(&xfA, t);
  108. m_sweepB.GetTransform(&xfB, t);
  109. switch (m_type)
  110. {
  111. case e_points:
  112. {
  113. b2Vec2 axisA = b2MulT(xfA.q, m_axis);
  114. b2Vec2 axisB = b2MulT(xfB.q, -m_axis);
  115. *indexA = m_proxyA->GetSupport(axisA);
  116. *indexB = m_proxyB->GetSupport(axisB);
  117. b2Vec2 localPointA = m_proxyA->GetVertex(*indexA);
  118. b2Vec2 localPointB = m_proxyB->GetVertex(*indexB);
  119. b2Vec2 pointA = b2Mul(xfA, localPointA);
  120. b2Vec2 pointB = b2Mul(xfB, localPointB);
  121. float32 separation = b2Dot(pointB - pointA, m_axis);
  122. return separation;
  123. }
  124. case e_faceA:
  125. {
  126. b2Vec2 normal = b2Mul(xfA.q, m_axis);
  127. b2Vec2 pointA = b2Mul(xfA, m_localPoint);
  128. b2Vec2 axisB = b2MulT(xfB.q, -normal);
  129. *indexA = -1;
  130. *indexB = m_proxyB->GetSupport(axisB);
  131. b2Vec2 localPointB = m_proxyB->GetVertex(*indexB);
  132. b2Vec2 pointB = b2Mul(xfB, localPointB);
  133. float32 separation = b2Dot(pointB - pointA, normal);
  134. return separation;
  135. }
  136. case e_faceB:
  137. {
  138. b2Vec2 normal = b2Mul(xfB.q, m_axis);
  139. b2Vec2 pointB = b2Mul(xfB, m_localPoint);
  140. b2Vec2 axisA = b2MulT(xfA.q, -normal);
  141. *indexB = -1;
  142. *indexA = m_proxyA->GetSupport(axisA);
  143. b2Vec2 localPointA = m_proxyA->GetVertex(*indexA);
  144. b2Vec2 pointA = b2Mul(xfA, localPointA);
  145. float32 separation = b2Dot(pointA - pointB, normal);
  146. return separation;
  147. }
  148. default:
  149. b2Assert(false);
  150. *indexA = -1;
  151. *indexB = -1;
  152. return 0.0f;
  153. }
  154. }
  155. float32 Evaluate(int32 indexA, int32 indexB, float32 t) const
  156. {
  157. b2Transform xfA, xfB;
  158. m_sweepA.GetTransform(&xfA, t);
  159. m_sweepB.GetTransform(&xfB, t);
  160. switch (m_type)
  161. {
  162. case e_points:
  163. {
  164. b2Vec2 axisA = b2MulT(xfA.q, m_axis);
  165. b2Vec2 axisB = b2MulT(xfB.q, -m_axis);
  166. b2Vec2 localPointA = m_proxyA->GetVertex(indexA);
  167. b2Vec2 localPointB = m_proxyB->GetVertex(indexB);
  168. b2Vec2 pointA = b2Mul(xfA, localPointA);
  169. b2Vec2 pointB = b2Mul(xfB, localPointB);
  170. float32 separation = b2Dot(pointB - pointA, m_axis);
  171. return separation;
  172. }
  173. case e_faceA:
  174. {
  175. b2Vec2 normal = b2Mul(xfA.q, m_axis);
  176. b2Vec2 pointA = b2Mul(xfA, m_localPoint);
  177. b2Vec2 axisB = b2MulT(xfB.q, -normal);
  178. b2Vec2 localPointB = m_proxyB->GetVertex(indexB);
  179. b2Vec2 pointB = b2Mul(xfB, localPointB);
  180. float32 separation = b2Dot(pointB - pointA, normal);
  181. return separation;
  182. }
  183. case e_faceB:
  184. {
  185. b2Vec2 normal = b2Mul(xfB.q, m_axis);
  186. b2Vec2 pointB = b2Mul(xfB, m_localPoint);
  187. b2Vec2 axisA = b2MulT(xfA.q, -normal);
  188. b2Vec2 localPointA = m_proxyA->GetVertex(indexA);
  189. b2Vec2 pointA = b2Mul(xfA, localPointA);
  190. float32 separation = b2Dot(pointA - pointB, normal);
  191. return separation;
  192. }
  193. default:
  194. b2Assert(false);
  195. return 0.0f;
  196. }
  197. }
  198. const b2DistanceProxy* m_proxyA;
  199. const b2DistanceProxy* m_proxyB;
  200. b2Sweep m_sweepA, m_sweepB;
  201. Type m_type;
  202. b2Vec2 m_localPoint;
  203. b2Vec2 m_axis;
  204. };
  205. // CCD via the local separating axis method. This seeks progression
  206. // by computing the largest time at which separation is maintained.
  207. void b2TimeOfImpact(b2TOIOutput* output, const b2TOIInput* input)
  208. {
  209. ++b2_toiCalls;
  210. output->state = b2TOIOutput::e_unknown;
  211. output->t = input->tMax;
  212. const b2DistanceProxy* proxyA = &input->proxyA;
  213. const b2DistanceProxy* proxyB = &input->proxyB;
  214. b2Sweep sweepA = input->sweepA;
  215. b2Sweep sweepB = input->sweepB;
  216. // Large rotations can make the root finder fail, so we normalize the
  217. // sweep angles.
  218. sweepA.Normalize();
  219. sweepB.Normalize();
  220. float32 tMax = input->tMax;
  221. float32 totalRadius = proxyA->m_radius + proxyB->m_radius;
  222. float32 target = b2Max(b2_linearSlop, totalRadius - 3.0f * b2_linearSlop);
  223. float32 tolerance = 0.25f * b2_linearSlop;
  224. b2Assert(target > tolerance);
  225. float32 t1 = 0.0f;
  226. const int32 k_maxIterations = 20; // TODO_ERIN b2Settings
  227. int32 iter = 0;
  228. // Prepare input for distance query.
  229. b2SimplexCache cache;
  230. cache.count = 0;
  231. b2DistanceInput distanceInput;
  232. distanceInput.proxyA = input->proxyA;
  233. distanceInput.proxyB = input->proxyB;
  234. distanceInput.useRadii = false;
  235. // The outer loop progressively attempts to compute new separating axes.
  236. // This loop terminates when an axis is repeated (no progress is made).
  237. for(;;)
  238. {
  239. b2Transform xfA, xfB;
  240. sweepA.GetTransform(&xfA, t1);
  241. sweepB.GetTransform(&xfB, t1);
  242. // Get the distance between shapes. We can also use the results
  243. // to get a separating axis.
  244. distanceInput.transformA = xfA;
  245. distanceInput.transformB = xfB;
  246. b2DistanceOutput distanceOutput;
  247. b2Distance(&distanceOutput, &cache, &distanceInput);
  248. // If the shapes are overlapped, we give up on continuous collision.
  249. if (distanceOutput.distance <= 0.0f)
  250. {
  251. // Failure!
  252. output->state = b2TOIOutput::e_overlapped;
  253. output->t = 0.0f;
  254. break;
  255. }
  256. if (distanceOutput.distance < target + tolerance)
  257. {
  258. // Victory!
  259. output->state = b2TOIOutput::e_touching;
  260. output->t = t1;
  261. break;
  262. }
  263. // Initialize the separating axis.
  264. b2SeparationFunction fcn;
  265. fcn.m_localPoint = b2Vec2(0,0);
  266. fcn.Initialize(&cache, proxyA, sweepA, proxyB, sweepB, t1);
  267. #if 0
  268. // Dump the curve seen by the root finder
  269. {
  270. const int32 N = 100;
  271. float32 dx = 1.0f / N;
  272. float32 xs[N+1];
  273. float32 fs[N+1];
  274. float32 x = 0.0f;
  275. for (int32 i = 0; i <= N; ++i)
  276. {
  277. sweepA.GetTransform(&xfA, x);
  278. sweepB.GetTransform(&xfB, x);
  279. float32 f = fcn.Evaluate(xfA, xfB) - target;
  280. printf("%g %g\n", x, f);
  281. xs[i] = x;
  282. fs[i] = f;
  283. x += dx;
  284. }
  285. }
  286. #endif
  287. // Compute the TOI on the separating axis. We do this by successively
  288. // resolving the deepest point. This loop is bounded by the number of vertices.
  289. bool done = false;
  290. float32 t2 = tMax;
  291. int32 pushBackIter = 0;
  292. for (;;)
  293. {
  294. // Find the deepest point at t2. Store the witness point indices.
  295. int32 indexA, indexB;
  296. float32 s2 = fcn.FindMinSeparation(&indexA, &indexB, t2);
  297. // Is the final configuration separated?
  298. if (s2 > target + tolerance)
  299. {
  300. // Victory!
  301. output->state = b2TOIOutput::e_separated;
  302. output->t = tMax;
  303. done = true;
  304. break;
  305. }
  306. // Has the separation reached tolerance?
  307. if (s2 > target - tolerance)
  308. {
  309. // Advance the sweeps
  310. t1 = t2;
  311. break;
  312. }
  313. // Compute the initial separation of the witness points.
  314. float32 s1 = fcn.Evaluate(indexA, indexB, t1);
  315. // Check for initial overlap. This might happen if the root finder
  316. // runs out of iterations.
  317. if (s1 < target - tolerance)
  318. {
  319. output->state = b2TOIOutput::e_failed;
  320. output->t = t1;
  321. done = true;
  322. break;
  323. }
  324. // Check for touching
  325. if (s1 <= target + tolerance)
  326. {
  327. // Victory! t1 should hold the TOI (could be 0.0).
  328. output->state = b2TOIOutput::e_touching;
  329. output->t = t1;
  330. done = true;
  331. break;
  332. }
  333. // Compute 1D root of: f(x) - target = 0
  334. int32 rootIterCount = 0;
  335. float32 a1 = t1, a2 = t2;
  336. for (;;)
  337. {
  338. // Use a mix of the secant rule and bisection.
  339. float32 t;
  340. if (rootIterCount & 1)
  341. {
  342. // Secant rule to improve convergence.
  343. t = a1 + (target - s1) * (a2 - a1) / (s2 - s1);
  344. }
  345. else
  346. {
  347. // Bisection to guarantee progress.
  348. t = 0.5f * (a1 + a2);
  349. }
  350. float32 s = fcn.Evaluate(indexA, indexB, t);
  351. if (b2Abs(s - target) < tolerance)
  352. {
  353. // t2 holds a tentative value for t1
  354. t2 = t;
  355. break;
  356. }
  357. // Ensure we continue to bracket the root.
  358. if (s > target)
  359. {
  360. a1 = t;
  361. s1 = s;
  362. }
  363. else
  364. {
  365. a2 = t;
  366. s2 = s;
  367. }
  368. ++rootIterCount;
  369. ++b2_toiRootIters;
  370. if (rootIterCount == 50)
  371. {
  372. break;
  373. }
  374. }
  375. b2_toiMaxRootIters = b2Max(b2_toiMaxRootIters, rootIterCount);
  376. ++pushBackIter;
  377. if (pushBackIter == b2_maxPolygonVertices)
  378. {
  379. break;
  380. }
  381. }
  382. ++iter;
  383. ++b2_toiIters;
  384. if (done)
  385. {
  386. break;
  387. }
  388. if (iter == k_maxIterations)
  389. {
  390. // Root finder got stuck. Semi-victory.
  391. output->state = b2TOIOutput::e_failed;
  392. output->t = t1;
  393. break;
  394. }
  395. }
  396. b2_toiMaxIters = b2Max(b2_toiMaxIters, iter);
  397. }