b2TimeOfImpact.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486
  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 <Box2D/Common/b2Timer.h>
  24. #include <stdio.h>
  25. float32 b2_toiTime, b2_toiMaxTime;
  26. int32 b2_toiCalls, b2_toiIters, b2_toiMaxIters;
  27. int32 b2_toiRootIters, b2_toiMaxRootIters;
  28. //
  29. struct b2SeparationFunction
  30. {
  31. enum Type
  32. {
  33. e_points,
  34. e_faceA,
  35. e_faceB
  36. };
  37. // TODO_ERIN might not need to return the separation
  38. float32 Initialize(const b2SimplexCache* cache,
  39. const b2DistanceProxy* proxyA, const b2Sweep& sweepA,
  40. const b2DistanceProxy* proxyB, const b2Sweep& sweepB,
  41. float32 t1)
  42. {
  43. m_proxyA = proxyA;
  44. m_proxyB = proxyB;
  45. int32 count = cache->count;
  46. b2Assert(0 < count && count < 3);
  47. m_sweepA = sweepA;
  48. m_sweepB = sweepB;
  49. b2Transform xfA, xfB;
  50. m_sweepA.GetTransform(&xfA, t1);
  51. m_sweepB.GetTransform(&xfB, t1);
  52. if (count == 1)
  53. {
  54. m_type = e_points;
  55. b2Vec2 localPointA = m_proxyA->GetVertex(cache->indexA[0]);
  56. b2Vec2 localPointB = m_proxyB->GetVertex(cache->indexB[0]);
  57. b2Vec2 pointA = b2Mul(xfA, localPointA);
  58. b2Vec2 pointB = b2Mul(xfB, localPointB);
  59. m_axis = pointB - pointA;
  60. float32 s = m_axis.Normalize();
  61. return s;
  62. }
  63. else if (cache->indexA[0] == cache->indexA[1])
  64. {
  65. // Two points on B and one on A.
  66. m_type = e_faceB;
  67. b2Vec2 localPointB1 = proxyB->GetVertex(cache->indexB[0]);
  68. b2Vec2 localPointB2 = proxyB->GetVertex(cache->indexB[1]);
  69. m_axis = b2Cross(localPointB2 - localPointB1, 1.0f);
  70. m_axis.Normalize();
  71. b2Vec2 normal = b2Mul(xfB.q, m_axis);
  72. m_localPoint = 0.5f * (localPointB1 + localPointB2);
  73. b2Vec2 pointB = b2Mul(xfB, m_localPoint);
  74. b2Vec2 localPointA = proxyA->GetVertex(cache->indexA[0]);
  75. b2Vec2 pointA = b2Mul(xfA, localPointA);
  76. float32 s = b2Dot(pointA - pointB, normal);
  77. if (s < 0.0f)
  78. {
  79. m_axis = -m_axis;
  80. s = -s;
  81. }
  82. return s;
  83. }
  84. else
  85. {
  86. // Two points on A and one or two points on B.
  87. m_type = e_faceA;
  88. b2Vec2 localPointA1 = m_proxyA->GetVertex(cache->indexA[0]);
  89. b2Vec2 localPointA2 = m_proxyA->GetVertex(cache->indexA[1]);
  90. m_axis = b2Cross(localPointA2 - localPointA1, 1.0f);
  91. m_axis.Normalize();
  92. b2Vec2 normal = b2Mul(xfA.q, m_axis);
  93. m_localPoint = 0.5f * (localPointA1 + localPointA2);
  94. b2Vec2 pointA = b2Mul(xfA, m_localPoint);
  95. b2Vec2 localPointB = m_proxyB->GetVertex(cache->indexB[0]);
  96. b2Vec2 pointB = b2Mul(xfB, localPointB);
  97. float32 s = b2Dot(pointB - pointA, normal);
  98. if (s < 0.0f)
  99. {
  100. m_axis = -m_axis;
  101. s = -s;
  102. }
  103. return s;
  104. }
  105. }
  106. //
  107. float32 FindMinSeparation(int32* indexA, int32* indexB, float32 t) const
  108. {
  109. b2Transform xfA, xfB;
  110. m_sweepA.GetTransform(&xfA, t);
  111. m_sweepB.GetTransform(&xfB, t);
  112. switch (m_type)
  113. {
  114. case e_points:
  115. {
  116. b2Vec2 axisA = b2MulT(xfA.q, m_axis);
  117. b2Vec2 axisB = b2MulT(xfB.q, -m_axis);
  118. *indexA = m_proxyA->GetSupport(axisA);
  119. *indexB = m_proxyB->GetSupport(axisB);
  120. b2Vec2 localPointA = m_proxyA->GetVertex(*indexA);
  121. b2Vec2 localPointB = m_proxyB->GetVertex(*indexB);
  122. b2Vec2 pointA = b2Mul(xfA, localPointA);
  123. b2Vec2 pointB = b2Mul(xfB, localPointB);
  124. float32 separation = b2Dot(pointB - pointA, m_axis);
  125. return separation;
  126. }
  127. case e_faceA:
  128. {
  129. b2Vec2 normal = b2Mul(xfA.q, m_axis);
  130. b2Vec2 pointA = b2Mul(xfA, m_localPoint);
  131. b2Vec2 axisB = b2MulT(xfB.q, -normal);
  132. *indexA = -1;
  133. *indexB = m_proxyB->GetSupport(axisB);
  134. b2Vec2 localPointB = m_proxyB->GetVertex(*indexB);
  135. b2Vec2 pointB = b2Mul(xfB, localPointB);
  136. float32 separation = b2Dot(pointB - pointA, normal);
  137. return separation;
  138. }
  139. case e_faceB:
  140. {
  141. b2Vec2 normal = b2Mul(xfB.q, m_axis);
  142. b2Vec2 pointB = b2Mul(xfB, m_localPoint);
  143. b2Vec2 axisA = b2MulT(xfA.q, -normal);
  144. *indexB = -1;
  145. *indexA = m_proxyA->GetSupport(axisA);
  146. b2Vec2 localPointA = m_proxyA->GetVertex(*indexA);
  147. b2Vec2 pointA = b2Mul(xfA, localPointA);
  148. float32 separation = b2Dot(pointA - pointB, normal);
  149. return separation;
  150. }
  151. default:
  152. b2Assert(false);
  153. *indexA = -1;
  154. *indexB = -1;
  155. return 0.0f;
  156. }
  157. }
  158. //
  159. float32 Evaluate(int32 indexA, int32 indexB, float32 t) const
  160. {
  161. b2Transform xfA, xfB;
  162. m_sweepA.GetTransform(&xfA, t);
  163. m_sweepB.GetTransform(&xfB, t);
  164. switch (m_type)
  165. {
  166. case e_points:
  167. {
  168. b2Vec2 localPointA = m_proxyA->GetVertex(indexA);
  169. b2Vec2 localPointB = m_proxyB->GetVertex(indexB);
  170. b2Vec2 pointA = b2Mul(xfA, localPointA);
  171. b2Vec2 pointB = b2Mul(xfB, localPointB);
  172. float32 separation = b2Dot(pointB - pointA, m_axis);
  173. return separation;
  174. }
  175. case e_faceA:
  176. {
  177. b2Vec2 normal = b2Mul(xfA.q, m_axis);
  178. b2Vec2 pointA = b2Mul(xfA, m_localPoint);
  179. b2Vec2 localPointB = m_proxyB->GetVertex(indexB);
  180. b2Vec2 pointB = b2Mul(xfB, localPointB);
  181. float32 separation = b2Dot(pointB - pointA, normal);
  182. return separation;
  183. }
  184. case e_faceB:
  185. {
  186. b2Vec2 normal = b2Mul(xfB.q, m_axis);
  187. b2Vec2 pointB = b2Mul(xfB, m_localPoint);
  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. b2Timer timer;
  210. ++b2_toiCalls;
  211. output->state = b2TOIOutput::e_unknown;
  212. output->t = input->tMax;
  213. const b2DistanceProxy* proxyA = &input->proxyA;
  214. const b2DistanceProxy* proxyB = &input->proxyB;
  215. b2Sweep sweepA = input->sweepA;
  216. b2Sweep sweepB = input->sweepB;
  217. // Large rotations can make the root finder fail, so we normalize the
  218. // sweep angles.
  219. sweepA.Normalize();
  220. sweepB.Normalize();
  221. float32 tMax = input->tMax;
  222. float32 totalRadius = proxyA->m_radius + proxyB->m_radius;
  223. float32 target = b2Max(b2_linearSlop, totalRadius - 3.0f * b2_linearSlop);
  224. float32 tolerance = 0.25f * b2_linearSlop;
  225. b2Assert(target > tolerance);
  226. float32 t1 = 0.0f;
  227. const int32 k_maxIterations = 20; // TODO_ERIN b2Settings
  228. int32 iter = 0;
  229. // Prepare input for distance query.
  230. b2SimplexCache cache;
  231. cache.count = 0;
  232. b2DistanceInput distanceInput;
  233. distanceInput.proxyA = input->proxyA;
  234. distanceInput.proxyB = input->proxyB;
  235. distanceInput.useRadii = false;
  236. // The outer loop progressively attempts to compute new separating axes.
  237. // This loop terminates when an axis is repeated (no progress is made).
  238. for(;;)
  239. {
  240. b2Transform xfA, xfB;
  241. sweepA.GetTransform(&xfA, t1);
  242. sweepB.GetTransform(&xfB, t1);
  243. // Get the distance between shapes. We can also use the results
  244. // to get a separating axis.
  245. distanceInput.transformA = xfA;
  246. distanceInput.transformB = xfB;
  247. b2DistanceOutput distanceOutput;
  248. b2Distance(&distanceOutput, &cache, &distanceInput);
  249. // If the shapes are overlapped, we give up on continuous collision.
  250. if (distanceOutput.distance <= 0.0f)
  251. {
  252. // Failure!
  253. output->state = b2TOIOutput::e_overlapped;
  254. output->t = 0.0f;
  255. break;
  256. }
  257. if (distanceOutput.distance < target + tolerance)
  258. {
  259. // Victory!
  260. output->state = b2TOIOutput::e_touching;
  261. output->t = t1;
  262. break;
  263. }
  264. // Initialize the separating axis.
  265. b2SeparationFunction fcn;
  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. ++rootIterCount;
  351. ++b2_toiRootIters;
  352. float32 s = fcn.Evaluate(indexA, indexB, t);
  353. if (b2Abs(s - target) < tolerance)
  354. {
  355. // t2 holds a tentative value for t1
  356. t2 = t;
  357. break;
  358. }
  359. // Ensure we continue to bracket the root.
  360. if (s > target)
  361. {
  362. a1 = t;
  363. s1 = s;
  364. }
  365. else
  366. {
  367. a2 = t;
  368. s2 = s;
  369. }
  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. float32 time = timer.GetMilliseconds();
  398. b2_toiMaxTime = b2Max(b2_toiMaxTime, time);
  399. b2_toiTime += time;
  400. }