b2TimeOfImpact.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487
  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. m_localPoint = b2Vec2_zero;
  62. return s;
  63. }
  64. else if (cache->indexA[0] == cache->indexA[1])
  65. {
  66. // Two points on B and one on A.
  67. m_type = e_faceB;
  68. b2Vec2 localPointB1 = proxyB->GetVertex(cache->indexB[0]);
  69. b2Vec2 localPointB2 = proxyB->GetVertex(cache->indexB[1]);
  70. m_axis = b2Cross(localPointB2 - localPointB1, 1.0f);
  71. m_axis.Normalize();
  72. b2Vec2 normal = b2Mul(xfB.q, m_axis);
  73. m_localPoint = 0.5f * (localPointB1 + localPointB2);
  74. b2Vec2 pointB = b2Mul(xfB, m_localPoint);
  75. b2Vec2 localPointA = proxyA->GetVertex(cache->indexA[0]);
  76. b2Vec2 pointA = b2Mul(xfA, localPointA);
  77. float32 s = b2Dot(pointA - pointB, normal);
  78. if (s < 0.0f)
  79. {
  80. m_axis = -m_axis;
  81. s = -s;
  82. }
  83. return s;
  84. }
  85. else
  86. {
  87. // Two points on A and one or two points on B.
  88. m_type = e_faceA;
  89. b2Vec2 localPointA1 = m_proxyA->GetVertex(cache->indexA[0]);
  90. b2Vec2 localPointA2 = m_proxyA->GetVertex(cache->indexA[1]);
  91. m_axis = b2Cross(localPointA2 - localPointA1, 1.0f);
  92. m_axis.Normalize();
  93. b2Vec2 normal = b2Mul(xfA.q, m_axis);
  94. m_localPoint = 0.5f * (localPointA1 + localPointA2);
  95. b2Vec2 pointA = b2Mul(xfA, m_localPoint);
  96. b2Vec2 localPointB = m_proxyB->GetVertex(cache->indexB[0]);
  97. b2Vec2 pointB = b2Mul(xfB, localPointB);
  98. float32 s = b2Dot(pointB - pointA, normal);
  99. if (s < 0.0f)
  100. {
  101. m_axis = -m_axis;
  102. s = -s;
  103. }
  104. return s;
  105. }
  106. }
  107. //
  108. float32 FindMinSeparation(int32* indexA, int32* indexB, float32 t) const
  109. {
  110. b2Transform xfA, xfB;
  111. m_sweepA.GetTransform(&xfA, t);
  112. m_sweepB.GetTransform(&xfB, t);
  113. switch (m_type)
  114. {
  115. case e_points:
  116. {
  117. b2Vec2 axisA = b2MulT(xfA.q, m_axis);
  118. b2Vec2 axisB = b2MulT(xfB.q, -m_axis);
  119. *indexA = m_proxyA->GetSupport(axisA);
  120. *indexB = m_proxyB->GetSupport(axisB);
  121. b2Vec2 localPointA = m_proxyA->GetVertex(*indexA);
  122. b2Vec2 localPointB = m_proxyB->GetVertex(*indexB);
  123. b2Vec2 pointA = b2Mul(xfA, localPointA);
  124. b2Vec2 pointB = b2Mul(xfB, localPointB);
  125. float32 separation = b2Dot(pointB - pointA, m_axis);
  126. return separation;
  127. }
  128. case e_faceA:
  129. {
  130. b2Vec2 normal = b2Mul(xfA.q, m_axis);
  131. b2Vec2 pointA = b2Mul(xfA, m_localPoint);
  132. b2Vec2 axisB = b2MulT(xfB.q, -normal);
  133. *indexA = -1;
  134. *indexB = m_proxyB->GetSupport(axisB);
  135. b2Vec2 localPointB = m_proxyB->GetVertex(*indexB);
  136. b2Vec2 pointB = b2Mul(xfB, localPointB);
  137. float32 separation = b2Dot(pointB - pointA, normal);
  138. return separation;
  139. }
  140. case e_faceB:
  141. {
  142. b2Vec2 normal = b2Mul(xfB.q, m_axis);
  143. b2Vec2 pointB = b2Mul(xfB, m_localPoint);
  144. b2Vec2 axisA = b2MulT(xfA.q, -normal);
  145. *indexB = -1;
  146. *indexA = m_proxyA->GetSupport(axisA);
  147. b2Vec2 localPointA = m_proxyA->GetVertex(*indexA);
  148. b2Vec2 pointA = b2Mul(xfA, localPointA);
  149. float32 separation = b2Dot(pointA - pointB, normal);
  150. return separation;
  151. }
  152. default:
  153. b2Assert(false);
  154. *indexA = -1;
  155. *indexB = -1;
  156. return 0.0f;
  157. }
  158. }
  159. //
  160. float32 Evaluate(int32 indexA, int32 indexB, float32 t) const
  161. {
  162. b2Transform xfA, xfB;
  163. m_sweepA.GetTransform(&xfA, t);
  164. m_sweepB.GetTransform(&xfB, t);
  165. switch (m_type)
  166. {
  167. case e_points:
  168. {
  169. b2Vec2 localPointA = m_proxyA->GetVertex(indexA);
  170. b2Vec2 localPointB = m_proxyB->GetVertex(indexB);
  171. b2Vec2 pointA = b2Mul(xfA, localPointA);
  172. b2Vec2 pointB = b2Mul(xfB, localPointB);
  173. float32 separation = b2Dot(pointB - pointA, m_axis);
  174. return separation;
  175. }
  176. case e_faceA:
  177. {
  178. b2Vec2 normal = b2Mul(xfA.q, m_axis);
  179. b2Vec2 pointA = b2Mul(xfA, m_localPoint);
  180. b2Vec2 localPointB = m_proxyB->GetVertex(indexB);
  181. b2Vec2 pointB = b2Mul(xfB, localPointB);
  182. float32 separation = b2Dot(pointB - pointA, normal);
  183. return separation;
  184. }
  185. case e_faceB:
  186. {
  187. b2Vec2 normal = b2Mul(xfB.q, m_axis);
  188. b2Vec2 pointB = b2Mul(xfB, m_localPoint);
  189. b2Vec2 localPointA = m_proxyA->GetVertex(indexA);
  190. b2Vec2 pointA = b2Mul(xfA, localPointA);
  191. float32 separation = b2Dot(pointA - pointB, normal);
  192. return separation;
  193. }
  194. default:
  195. b2Assert(false);
  196. return 0.0f;
  197. }
  198. }
  199. const b2DistanceProxy* m_proxyA;
  200. const b2DistanceProxy* m_proxyB;
  201. b2Sweep m_sweepA, m_sweepB;
  202. Type m_type;
  203. b2Vec2 m_localPoint;
  204. b2Vec2 m_axis;
  205. };
  206. // CCD via the local separating axis method. This seeks progression
  207. // by computing the largest time at which separation is maintained.
  208. void b2TimeOfImpact(b2TOIOutput* output, const b2TOIInput* input)
  209. {
  210. b2Timer timer;
  211. ++b2_toiCalls;
  212. output->state = b2TOIOutput::e_unknown;
  213. output->t = input->tMax;
  214. const b2DistanceProxy* proxyA = &input->proxyA;
  215. const b2DistanceProxy* proxyB = &input->proxyB;
  216. b2Sweep sweepA = input->sweepA;
  217. b2Sweep sweepB = input->sweepB;
  218. // Large rotations can make the root finder fail, so we normalize the
  219. // sweep angles.
  220. sweepA.Normalize();
  221. sweepB.Normalize();
  222. float32 tMax = input->tMax;
  223. float32 totalRadius = proxyA->m_radius + proxyB->m_radius;
  224. float32 target = b2Max(b2_linearSlop, totalRadius - 3.0f * b2_linearSlop);
  225. float32 tolerance = 0.25f * b2_linearSlop;
  226. b2Assert(target > tolerance);
  227. float32 t1 = 0.0f;
  228. const int32 k_maxIterations = 20; // TODO_ERIN b2Settings
  229. int32 iter = 0;
  230. // Prepare input for distance query.
  231. b2SimplexCache cache;
  232. cache.count = 0;
  233. b2DistanceInput distanceInput;
  234. distanceInput.proxyA = input->proxyA;
  235. distanceInput.proxyB = input->proxyB;
  236. distanceInput.useRadii = false;
  237. // The outer loop progressively attempts to compute new separating axes.
  238. // This loop terminates when an axis is repeated (no progress is made).
  239. for(;;)
  240. {
  241. b2Transform xfA, xfB;
  242. sweepA.GetTransform(&xfA, t1);
  243. sweepB.GetTransform(&xfB, t1);
  244. // Get the distance between shapes. We can also use the results
  245. // to get a separating axis.
  246. distanceInput.transformA = xfA;
  247. distanceInput.transformB = xfB;
  248. b2DistanceOutput distanceOutput;
  249. b2Distance(&distanceOutput, &cache, &distanceInput);
  250. // If the shapes are overlapped, we give up on continuous collision.
  251. if (distanceOutput.distance <= 0.0f)
  252. {
  253. // Failure!
  254. output->state = b2TOIOutput::e_overlapped;
  255. output->t = 0.0f;
  256. break;
  257. }
  258. if (distanceOutput.distance < target + tolerance)
  259. {
  260. // Victory!
  261. output->state = b2TOIOutput::e_touching;
  262. output->t = t1;
  263. break;
  264. }
  265. // Initialize the separating axis.
  266. b2SeparationFunction fcn;
  267. fcn.Initialize(&cache, proxyA, sweepA, proxyB, sweepB, t1);
  268. #if 0
  269. // Dump the curve seen by the root finder
  270. {
  271. const int32 N = 100;
  272. float32 dx = 1.0f / N;
  273. float32 xs[N+1];
  274. float32 fs[N+1];
  275. float32 x = 0.0f;
  276. for (int32 i = 0; i <= N; ++i)
  277. {
  278. sweepA.GetTransform(&xfA, x);
  279. sweepB.GetTransform(&xfB, x);
  280. float32 f = fcn.Evaluate(xfA, xfB) - target;
  281. printf("%g %g\n", x, f);
  282. xs[i] = x;
  283. fs[i] = f;
  284. x += dx;
  285. }
  286. }
  287. #endif
  288. // Compute the TOI on the separating axis. We do this by successively
  289. // resolving the deepest point. This loop is bounded by the number of vertices.
  290. bool done = false;
  291. float32 t2 = tMax;
  292. int32 pushBackIter = 0;
  293. for (;;)
  294. {
  295. // Find the deepest point at t2. Store the witness point indices.
  296. int32 indexA, indexB;
  297. float32 s2 = fcn.FindMinSeparation(&indexA, &indexB, t2);
  298. // Is the final configuration separated?
  299. if (s2 > target + tolerance)
  300. {
  301. // Victory!
  302. output->state = b2TOIOutput::e_separated;
  303. output->t = tMax;
  304. done = true;
  305. break;
  306. }
  307. // Has the separation reached tolerance?
  308. if (s2 > target - tolerance)
  309. {
  310. // Advance the sweeps
  311. t1 = t2;
  312. break;
  313. }
  314. // Compute the initial separation of the witness points.
  315. float32 s1 = fcn.Evaluate(indexA, indexB, t1);
  316. // Check for initial overlap. This might happen if the root finder
  317. // runs out of iterations.
  318. if (s1 < target - tolerance)
  319. {
  320. output->state = b2TOIOutput::e_failed;
  321. output->t = t1;
  322. done = true;
  323. break;
  324. }
  325. // Check for touching
  326. if (s1 <= target + tolerance)
  327. {
  328. // Victory! t1 should hold the TOI (could be 0.0).
  329. output->state = b2TOIOutput::e_touching;
  330. output->t = t1;
  331. done = true;
  332. break;
  333. }
  334. // Compute 1D root of: f(x) - target = 0
  335. int32 rootIterCount = 0;
  336. float32 a1 = t1, a2 = t2;
  337. for (;;)
  338. {
  339. // Use a mix of the secant rule and bisection.
  340. float32 t;
  341. if (rootIterCount & 1)
  342. {
  343. // Secant rule to improve convergence.
  344. t = a1 + (target - s1) * (a2 - a1) / (s2 - s1);
  345. }
  346. else
  347. {
  348. // Bisection to guarantee progress.
  349. t = 0.5f * (a1 + a2);
  350. }
  351. ++rootIterCount;
  352. ++b2_toiRootIters;
  353. float32 s = fcn.Evaluate(indexA, indexB, t);
  354. if (b2Abs(s - target) < tolerance)
  355. {
  356. // t2 holds a tentative value for t1
  357. t2 = t;
  358. break;
  359. }
  360. // Ensure we continue to bracket the root.
  361. if (s > target)
  362. {
  363. a1 = t;
  364. s1 = s;
  365. }
  366. else
  367. {
  368. a2 = t;
  369. s2 = s;
  370. }
  371. if (rootIterCount == 50)
  372. {
  373. break;
  374. }
  375. }
  376. b2_toiMaxRootIters = b2Max(b2_toiMaxRootIters, rootIterCount);
  377. ++pushBackIter;
  378. if (pushBackIter == b2_maxPolygonVertices)
  379. {
  380. break;
  381. }
  382. }
  383. ++iter;
  384. ++b2_toiIters;
  385. if (done)
  386. {
  387. break;
  388. }
  389. if (iter == k_maxIterations)
  390. {
  391. // Root finder got stuck. Semi-victory.
  392. output->state = b2TOIOutput::e_failed;
  393. output->t = t1;
  394. break;
  395. }
  396. }
  397. b2_toiMaxIters = b2Max(b2_toiMaxIters, iter);
  398. float32 time = timer.GetMilliseconds();
  399. b2_toiMaxTime = b2Max(b2_toiMaxTime, time);
  400. b2_toiTime += time;
  401. }