btComputeGjkEpaPenetration.h 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355
  1. /*
  2. Bullet Continuous Collision Detection and Physics Library
  3. Copyright (c) 2003-2014 Erwin Coumans http://bulletphysics.org
  4. This software is provided 'as-is', without any express or implied warranty.
  5. In no event will the authors be held liable for any damages arising from the use of this software.
  6. Permission is granted to anyone to use this software for any purpose,
  7. including commercial applications, and to alter it and redistribute it freely,
  8. subject to the following restrictions:
  9. 1. The origin of this software must not be misrepresented; you must not claim that you wrote the original software. If you use this software in a product, an acknowledgment in the product documentation would be appreciated but is not required.
  10. 2. Altered source versions must be plainly marked as such, and must not be misrepresented as being the original software.
  11. 3. This notice may not be removed or altered from any source distribution.
  12. */
  13. #ifndef BT_GJK_EPA_PENETATION_CONVEX_COLLISION_H
  14. #define BT_GJK_EPA_PENETATION_CONVEX_COLLISION_H
  15. #include "LinearMath/btTransform.h" // Note that btVector3 might be double precision...
  16. #include "btGjkEpa3.h"
  17. #include "btGjkCollisionDescription.h"
  18. #include "BulletCollision/NarrowPhaseCollision/btVoronoiSimplexSolver.h"
  19. template <typename btConvexTemplate>
  20. bool btGjkEpaCalcPenDepth(const btConvexTemplate& a, const btConvexTemplate& b,
  21. const btGjkCollisionDescription& colDesc,
  22. btVector3& v, btVector3& wWitnessOnA, btVector3& wWitnessOnB)
  23. {
  24. (void)v;
  25. // const btScalar radialmargin(btScalar(0.));
  26. btVector3 guessVector(b.getWorldTransform().getOrigin() - a.getWorldTransform().getOrigin()); //?? why not use the GJK input?
  27. btGjkEpaSolver3::sResults results;
  28. if (btGjkEpaSolver3_Penetration(a, b, guessVector, results))
  29. {
  30. // debugDraw->drawLine(results.witnesses[1],results.witnesses[1]+results.normal,btVector3(255,0,0));
  31. //resultOut->addContactPoint(results.normal,results.witnesses[1],-results.depth);
  32. wWitnessOnA = results.witnesses[0];
  33. wWitnessOnB = results.witnesses[1];
  34. v = results.normal;
  35. return true;
  36. }
  37. else
  38. {
  39. if (btGjkEpaSolver3_Distance(a, b, guessVector, results))
  40. {
  41. wWitnessOnA = results.witnesses[0];
  42. wWitnessOnB = results.witnesses[1];
  43. v = results.normal;
  44. return false;
  45. }
  46. }
  47. return false;
  48. }
  49. template <typename btConvexTemplate, typename btGjkDistanceTemplate>
  50. int btComputeGjkEpaPenetration(const btConvexTemplate& a, const btConvexTemplate& b, const btGjkCollisionDescription& colDesc, btVoronoiSimplexSolver& simplexSolver, btGjkDistanceTemplate* distInfo)
  51. {
  52. bool m_catchDegeneracies = true;
  53. btScalar m_cachedSeparatingDistance = 0.f;
  54. btScalar distance = btScalar(0.);
  55. btVector3 normalInB(btScalar(0.), btScalar(0.), btScalar(0.));
  56. btVector3 pointOnA, pointOnB;
  57. btTransform localTransA = a.getWorldTransform();
  58. btTransform localTransB = b.getWorldTransform();
  59. btScalar marginA = a.getMargin();
  60. btScalar marginB = b.getMargin();
  61. int m_curIter = 0;
  62. int gGjkMaxIter = colDesc.m_maxGjkIterations; //this is to catch invalid input, perhaps check for #NaN?
  63. btVector3 m_cachedSeparatingAxis = colDesc.m_firstDir;
  64. bool isValid = false;
  65. bool checkSimplex = false;
  66. bool checkPenetration = true;
  67. int m_degenerateSimplex = 0;
  68. int m_lastUsedMethod = -1;
  69. {
  70. btScalar squaredDistance = BT_LARGE_FLOAT;
  71. btScalar delta = btScalar(0.);
  72. btScalar margin = marginA + marginB;
  73. simplexSolver.reset();
  74. for (;;)
  75. //while (true)
  76. {
  77. btVector3 separatingAxisInA = (-m_cachedSeparatingAxis) * localTransA.getBasis();
  78. btVector3 separatingAxisInB = m_cachedSeparatingAxis * localTransB.getBasis();
  79. btVector3 pInA = a.getLocalSupportWithoutMargin(separatingAxisInA);
  80. btVector3 qInB = b.getLocalSupportWithoutMargin(separatingAxisInB);
  81. btVector3 pWorld = localTransA(pInA);
  82. btVector3 qWorld = localTransB(qInB);
  83. btVector3 w = pWorld - qWorld;
  84. delta = m_cachedSeparatingAxis.dot(w);
  85. // potential exit, they don't overlap
  86. if ((delta > btScalar(0.0)) && (delta * delta > squaredDistance * colDesc.m_maximumDistanceSquared))
  87. {
  88. m_degenerateSimplex = 10;
  89. checkSimplex = true;
  90. //checkPenetration = false;
  91. break;
  92. }
  93. //exit 0: the new point is already in the simplex, or we didn't come any closer
  94. if (simplexSolver.inSimplex(w))
  95. {
  96. m_degenerateSimplex = 1;
  97. checkSimplex = true;
  98. break;
  99. }
  100. // are we getting any closer ?
  101. btScalar f0 = squaredDistance - delta;
  102. btScalar f1 = squaredDistance * colDesc.m_gjkRelError2;
  103. if (f0 <= f1)
  104. {
  105. if (f0 <= btScalar(0.))
  106. {
  107. m_degenerateSimplex = 2;
  108. }
  109. else
  110. {
  111. m_degenerateSimplex = 11;
  112. }
  113. checkSimplex = true;
  114. break;
  115. }
  116. //add current vertex to simplex
  117. simplexSolver.addVertex(w, pWorld, qWorld);
  118. btVector3 newCachedSeparatingAxis;
  119. //calculate the closest point to the origin (update vector v)
  120. if (!simplexSolver.closest(newCachedSeparatingAxis))
  121. {
  122. m_degenerateSimplex = 3;
  123. checkSimplex = true;
  124. break;
  125. }
  126. if (newCachedSeparatingAxis.length2() < colDesc.m_gjkRelError2)
  127. {
  128. m_cachedSeparatingAxis = newCachedSeparatingAxis;
  129. m_degenerateSimplex = 6;
  130. checkSimplex = true;
  131. break;
  132. }
  133. btScalar previousSquaredDistance = squaredDistance;
  134. squaredDistance = newCachedSeparatingAxis.length2();
  135. #if 0
  136. ///warning: this termination condition leads to some problems in 2d test case see Bullet/Demos/Box2dDemo
  137. if (squaredDistance>previousSquaredDistance)
  138. {
  139. m_degenerateSimplex = 7;
  140. squaredDistance = previousSquaredDistance;
  141. checkSimplex = false;
  142. break;
  143. }
  144. #endif //
  145. //redundant m_simplexSolver->compute_points(pointOnA, pointOnB);
  146. //are we getting any closer ?
  147. if (previousSquaredDistance - squaredDistance <= SIMD_EPSILON * previousSquaredDistance)
  148. {
  149. // m_simplexSolver->backup_closest(m_cachedSeparatingAxis);
  150. checkSimplex = true;
  151. m_degenerateSimplex = 12;
  152. break;
  153. }
  154. m_cachedSeparatingAxis = newCachedSeparatingAxis;
  155. //degeneracy, this is typically due to invalid/uninitialized worldtransforms for a btCollisionObject
  156. if (m_curIter++ > gGjkMaxIter)
  157. {
  158. #if defined(DEBUG) || defined(_DEBUG)
  159. printf("btGjkPairDetector maxIter exceeded:%i\n", m_curIter);
  160. printf("sepAxis=(%f,%f,%f), squaredDistance = %f\n",
  161. m_cachedSeparatingAxis.getX(),
  162. m_cachedSeparatingAxis.getY(),
  163. m_cachedSeparatingAxis.getZ(),
  164. squaredDistance);
  165. #endif
  166. break;
  167. }
  168. bool check = (!simplexSolver.fullSimplex());
  169. //bool check = (!m_simplexSolver->fullSimplex() && squaredDistance > SIMD_EPSILON * m_simplexSolver->maxVertex());
  170. if (!check)
  171. {
  172. //do we need this backup_closest here ?
  173. // m_simplexSolver->backup_closest(m_cachedSeparatingAxis);
  174. m_degenerateSimplex = 13;
  175. break;
  176. }
  177. }
  178. if (checkSimplex)
  179. {
  180. simplexSolver.compute_points(pointOnA, pointOnB);
  181. normalInB = m_cachedSeparatingAxis;
  182. btScalar lenSqr = m_cachedSeparatingAxis.length2();
  183. //valid normal
  184. if (lenSqr < 0.0001)
  185. {
  186. m_degenerateSimplex = 5;
  187. }
  188. if (lenSqr > SIMD_EPSILON * SIMD_EPSILON)
  189. {
  190. btScalar rlen = btScalar(1.) / btSqrt(lenSqr);
  191. normalInB *= rlen; //normalize
  192. btScalar s = btSqrt(squaredDistance);
  193. btAssert(s > btScalar(0.0));
  194. pointOnA -= m_cachedSeparatingAxis * (marginA / s);
  195. pointOnB += m_cachedSeparatingAxis * (marginB / s);
  196. distance = ((btScalar(1.) / rlen) - margin);
  197. isValid = true;
  198. m_lastUsedMethod = 1;
  199. }
  200. else
  201. {
  202. m_lastUsedMethod = 2;
  203. }
  204. }
  205. bool catchDegeneratePenetrationCase =
  206. (m_catchDegeneracies && m_degenerateSimplex && ((distance + margin) < 0.01));
  207. //if (checkPenetration && !isValid)
  208. if (checkPenetration && (!isValid || catchDegeneratePenetrationCase))
  209. {
  210. //penetration case
  211. //if there is no way to handle penetrations, bail out
  212. // Penetration depth case.
  213. btVector3 tmpPointOnA, tmpPointOnB;
  214. m_cachedSeparatingAxis.setZero();
  215. bool isValid2 = btGjkEpaCalcPenDepth(a, b,
  216. colDesc,
  217. m_cachedSeparatingAxis, tmpPointOnA, tmpPointOnB);
  218. if (isValid2)
  219. {
  220. btVector3 tmpNormalInB = tmpPointOnB - tmpPointOnA;
  221. btScalar lenSqr = tmpNormalInB.length2();
  222. if (lenSqr <= (SIMD_EPSILON * SIMD_EPSILON))
  223. {
  224. tmpNormalInB = m_cachedSeparatingAxis;
  225. lenSqr = m_cachedSeparatingAxis.length2();
  226. }
  227. if (lenSqr > (SIMD_EPSILON * SIMD_EPSILON))
  228. {
  229. tmpNormalInB /= btSqrt(lenSqr);
  230. btScalar distance2 = -(tmpPointOnA - tmpPointOnB).length();
  231. //only replace valid penetrations when the result is deeper (check)
  232. if (!isValid || (distance2 < distance))
  233. {
  234. distance = distance2;
  235. pointOnA = tmpPointOnA;
  236. pointOnB = tmpPointOnB;
  237. normalInB = tmpNormalInB;
  238. isValid = true;
  239. m_lastUsedMethod = 3;
  240. }
  241. else
  242. {
  243. m_lastUsedMethod = 8;
  244. }
  245. }
  246. else
  247. {
  248. m_lastUsedMethod = 9;
  249. }
  250. }
  251. else
  252. {
  253. ///this is another degenerate case, where the initial GJK calculation reports a degenerate case
  254. ///EPA reports no penetration, and the second GJK (using the supporting vector without margin)
  255. ///reports a valid positive distance. Use the results of the second GJK instead of failing.
  256. ///thanks to Jacob.Langford for the reproduction case
  257. ///http://code.google.com/p/bullet/issues/detail?id=250
  258. if (m_cachedSeparatingAxis.length2() > btScalar(0.))
  259. {
  260. btScalar distance2 = (tmpPointOnA - tmpPointOnB).length() - margin;
  261. //only replace valid distances when the distance is less
  262. if (!isValid || (distance2 < distance))
  263. {
  264. distance = distance2;
  265. pointOnA = tmpPointOnA;
  266. pointOnB = tmpPointOnB;
  267. pointOnA -= m_cachedSeparatingAxis * marginA;
  268. pointOnB += m_cachedSeparatingAxis * marginB;
  269. normalInB = m_cachedSeparatingAxis;
  270. normalInB.normalize();
  271. isValid = true;
  272. m_lastUsedMethod = 6;
  273. }
  274. else
  275. {
  276. m_lastUsedMethod = 5;
  277. }
  278. }
  279. }
  280. }
  281. }
  282. if (isValid && ((distance < 0) || (distance * distance < colDesc.m_maximumDistanceSquared)))
  283. {
  284. m_cachedSeparatingAxis = normalInB;
  285. m_cachedSeparatingDistance = distance;
  286. distInfo->m_distance = distance;
  287. distInfo->m_normalBtoA = normalInB;
  288. distInfo->m_pointOnB = pointOnB;
  289. distInfo->m_pointOnA = pointOnB + normalInB * distance;
  290. return 0;
  291. }
  292. return -m_lastUsedMethod;
  293. }
  294. #endif //BT_GJK_EPA_PENETATION_CONVEX_COLLISION_H