The JUCE cross-platform C++ framework, with DISTRHO/KXStudio specific changes
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

477 lines
12KB

  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 "b2Collision.h"
  19. #include "b2Distance.h"
  20. #include "b2TimeOfImpact.h"
  21. #include "Shapes/b2CircleShape.h"
  22. #include "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 localPointA = m_proxyA->GetVertex(indexA);
  165. b2Vec2 localPointB = m_proxyB->GetVertex(indexB);
  166. b2Vec2 pointA = b2Mul(xfA, localPointA);
  167. b2Vec2 pointB = b2Mul(xfB, localPointB);
  168. float32 separation = b2Dot(pointB - pointA, m_axis);
  169. return separation;
  170. }
  171. case e_faceA:
  172. {
  173. b2Vec2 normal = b2Mul(xfA.q, m_axis);
  174. b2Vec2 pointA = b2Mul(xfA, m_localPoint);
  175. b2Vec2 localPointB = m_proxyB->GetVertex(indexB);
  176. b2Vec2 pointB = b2Mul(xfB, localPointB);
  177. float32 separation = b2Dot(pointB - pointA, normal);
  178. return separation;
  179. }
  180. case e_faceB:
  181. {
  182. b2Vec2 normal = b2Mul(xfB.q, m_axis);
  183. b2Vec2 pointB = b2Mul(xfB, m_localPoint);
  184. b2Vec2 localPointA = m_proxyA->GetVertex(indexA);
  185. b2Vec2 pointA = b2Mul(xfA, localPointA);
  186. float32 separation = b2Dot(pointA - pointB, normal);
  187. return separation;
  188. }
  189. default:
  190. b2Assert(false);
  191. return 0.0f;
  192. }
  193. }
  194. const b2DistanceProxy* m_proxyA;
  195. const b2DistanceProxy* m_proxyB;
  196. b2Sweep m_sweepA, m_sweepB;
  197. Type m_type;
  198. b2Vec2 m_localPoint;
  199. b2Vec2 m_axis;
  200. };
  201. // CCD via the local separating axis method. This seeks progression
  202. // by computing the largest time at which separation is maintained.
  203. void b2TimeOfImpact(b2TOIOutput* output, const b2TOIInput* input)
  204. {
  205. ++b2_toiCalls;
  206. output->state = b2TOIOutput::e_unknown;
  207. output->t = input->tMax;
  208. const b2DistanceProxy* proxyA = &input->proxyA;
  209. const b2DistanceProxy* proxyB = &input->proxyB;
  210. b2Sweep sweepA = input->sweepA;
  211. b2Sweep sweepB = input->sweepB;
  212. // Large rotations can make the root finder fail, so we normalize the
  213. // sweep angles.
  214. sweepA.Normalize();
  215. sweepB.Normalize();
  216. float32 tMax = input->tMax;
  217. float32 totalRadius = proxyA->m_radius + proxyB->m_radius;
  218. float32 target = b2Max(b2_linearSlop, totalRadius - 3.0f * b2_linearSlop);
  219. float32 tolerance = 0.25f * b2_linearSlop;
  220. b2Assert(target > tolerance);
  221. float32 t1 = 0.0f;
  222. const int32 k_maxIterations = 20; // TODO_ERIN b2Settings
  223. int32 iter = 0;
  224. // Prepare input for distance query.
  225. b2SimplexCache cache;
  226. cache.count = 0;
  227. b2DistanceInput distanceInput;
  228. distanceInput.proxyA = input->proxyA;
  229. distanceInput.proxyB = input->proxyB;
  230. distanceInput.useRadii = false;
  231. // The outer loop progressively attempts to compute new separating axes.
  232. // This loop terminates when an axis is repeated (no progress is made).
  233. for(;;)
  234. {
  235. b2Transform xfA, xfB;
  236. sweepA.GetTransform(&xfA, t1);
  237. sweepB.GetTransform(&xfB, t1);
  238. // Get the distance between shapes. We can also use the results
  239. // to get a separating axis.
  240. distanceInput.transformA = xfA;
  241. distanceInput.transformB = xfB;
  242. b2DistanceOutput distanceOutput;
  243. b2Distance(&distanceOutput, &cache, &distanceInput);
  244. // If the shapes are overlapped, we give up on continuous collision.
  245. if (distanceOutput.distance <= 0.0f)
  246. {
  247. // Failure!
  248. output->state = b2TOIOutput::e_overlapped;
  249. output->t = 0.0f;
  250. break;
  251. }
  252. if (distanceOutput.distance < target + tolerance)
  253. {
  254. // Victory!
  255. output->state = b2TOIOutput::e_touching;
  256. output->t = t1;
  257. break;
  258. }
  259. // Initialize the separating axis.
  260. b2SeparationFunction fcn;
  261. fcn.Initialize(&cache, proxyA, sweepA, proxyB, sweepB, t1);
  262. #if 0
  263. // Dump the curve seen by the root finder
  264. {
  265. const int32 N = 100;
  266. float32 dx = 1.0f / N;
  267. float32 xs[N+1];
  268. float32 fs[N+1];
  269. float32 x = 0.0f;
  270. for (int32 i = 0; i <= N; ++i)
  271. {
  272. sweepA.GetTransform(&xfA, x);
  273. sweepB.GetTransform(&xfB, x);
  274. float32 f = fcn.Evaluate(xfA, xfB) - target;
  275. printf("%g %g\n", x, f);
  276. xs[i] = x;
  277. fs[i] = f;
  278. x += dx;
  279. }
  280. }
  281. #endif
  282. // Compute the TOI on the separating axis. We do this by successively
  283. // resolving the deepest point. This loop is bounded by the number of vertices.
  284. bool done = false;
  285. float32 t2 = tMax;
  286. int32 pushBackIter = 0;
  287. for (;;)
  288. {
  289. // Find the deepest point at t2. Store the witness point indices.
  290. int32 indexA, indexB;
  291. float32 s2 = fcn.FindMinSeparation(&indexA, &indexB, t2);
  292. // Is the final configuration separated?
  293. if (s2 > target + tolerance)
  294. {
  295. // Victory!
  296. output->state = b2TOIOutput::e_separated;
  297. output->t = tMax;
  298. done = true;
  299. break;
  300. }
  301. // Has the separation reached tolerance?
  302. if (s2 > target - tolerance)
  303. {
  304. // Advance the sweeps
  305. t1 = t2;
  306. break;
  307. }
  308. // Compute the initial separation of the witness points.
  309. float32 s1 = fcn.Evaluate(indexA, indexB, t1);
  310. // Check for initial overlap. This might happen if the root finder
  311. // runs out of iterations.
  312. if (s1 < target - tolerance)
  313. {
  314. output->state = b2TOIOutput::e_failed;
  315. output->t = t1;
  316. done = true;
  317. break;
  318. }
  319. // Check for touching
  320. if (s1 <= target + tolerance)
  321. {
  322. // Victory! t1 should hold the TOI (could be 0.0).
  323. output->state = b2TOIOutput::e_touching;
  324. output->t = t1;
  325. done = true;
  326. break;
  327. }
  328. // Compute 1D root of: f(x) - target = 0
  329. int32 rootIterCount = 0;
  330. float32 a1 = t1, a2 = t2;
  331. for (;;)
  332. {
  333. // Use a mix of the secant rule and bisection.
  334. float32 t;
  335. if (rootIterCount & 1)
  336. {
  337. // Secant rule to improve convergence.
  338. t = a1 + (target - s1) * (a2 - a1) / (s2 - s1);
  339. }
  340. else
  341. {
  342. // Bisection to guarantee progress.
  343. t = 0.5f * (a1 + a2);
  344. }
  345. float32 s = fcn.Evaluate(indexA, indexB, t);
  346. if (b2Abs(s - target) < tolerance)
  347. {
  348. // t2 holds a tentative value for t1
  349. t2 = t;
  350. break;
  351. }
  352. // Ensure we continue to bracket the root.
  353. if (s > target)
  354. {
  355. a1 = t;
  356. s1 = s;
  357. }
  358. else
  359. {
  360. a2 = t;
  361. s2 = s;
  362. }
  363. ++rootIterCount;
  364. ++b2_toiRootIters;
  365. if (rootIterCount == 50)
  366. {
  367. break;
  368. }
  369. }
  370. b2_toiMaxRootIters = b2Max(b2_toiMaxRootIters, rootIterCount);
  371. ++pushBackIter;
  372. if (pushBackIter == b2_maxPolygonVertices)
  373. {
  374. break;
  375. }
  376. }
  377. ++iter;
  378. ++b2_toiIters;
  379. if (done)
  380. {
  381. break;
  382. }
  383. if (iter == k_maxIterations)
  384. {
  385. // Root finder got stuck. Semi-victory.
  386. output->state = b2TOIOutput::e_failed;
  387. output->t = t1;
  388. break;
  389. }
  390. }
  391. b2_toiMaxIters = b2Max(b2_toiMaxIters, iter);
  392. }