btMinkowskiPenetrationDepthSolver.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337
  1. /*
  2. Bullet Continuous Collision Detection and Physics Library
  3. Copyright (c) 2003-2006 Erwin Coumans http://continuousphysics.com/Bullet/
  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. #include "btMinkowskiPenetrationDepthSolver.h"
  14. #include "BulletCollision/NarrowPhaseCollision/btSubSimplexConvexCast.h"
  15. #include "BulletCollision/NarrowPhaseCollision/btVoronoiSimplexSolver.h"
  16. #include "BulletCollision/NarrowPhaseCollision/btGjkPairDetector.h"
  17. #include "BulletCollision/CollisionShapes/btConvexShape.h"
  18. #define NUM_UNITSPHERE_POINTS 42
  19. bool btMinkowskiPenetrationDepthSolver::calcPenDepth(btSimplexSolverInterface& simplexSolver,
  20. const btConvexShape* convexA, const btConvexShape* convexB,
  21. const btTransform& transA, const btTransform& transB,
  22. btVector3& v, btVector3& pa, btVector3& pb,
  23. class btIDebugDraw* debugDraw)
  24. {
  25. (void)v;
  26. bool check2d = convexA->isConvex2d() && convexB->isConvex2d();
  27. struct btIntermediateResult : public btDiscreteCollisionDetectorInterface::Result
  28. {
  29. btIntermediateResult() : m_hasResult(false)
  30. {
  31. }
  32. btVector3 m_normalOnBInWorld;
  33. btVector3 m_pointInWorld;
  34. btScalar m_depth;
  35. bool m_hasResult;
  36. virtual void setShapeIdentifiersA(int partId0, int index0)
  37. {
  38. (void)partId0;
  39. (void)index0;
  40. }
  41. virtual void setShapeIdentifiersB(int partId1, int index1)
  42. {
  43. (void)partId1;
  44. (void)index1;
  45. }
  46. void addContactPoint(const btVector3& normalOnBInWorld, const btVector3& pointInWorld, btScalar depth)
  47. {
  48. m_normalOnBInWorld = normalOnBInWorld;
  49. m_pointInWorld = pointInWorld;
  50. m_depth = depth;
  51. m_hasResult = true;
  52. }
  53. };
  54. //just take fixed number of orientation, and sample the penetration depth in that direction
  55. btScalar minProj = btScalar(BT_LARGE_FLOAT);
  56. btVector3 minNorm(btScalar(0.), btScalar(0.), btScalar(0.));
  57. btVector3 minA, minB;
  58. btVector3 separatingAxisInA, separatingAxisInB;
  59. btVector3 pInA, qInB, pWorld, qWorld, w;
  60. #ifndef __SPU__
  61. #define USE_BATCHED_SUPPORT 1
  62. #endif
  63. #ifdef USE_BATCHED_SUPPORT
  64. btVector3 supportVerticesABatch[NUM_UNITSPHERE_POINTS + MAX_PREFERRED_PENETRATION_DIRECTIONS * 2];
  65. btVector3 supportVerticesBBatch[NUM_UNITSPHERE_POINTS + MAX_PREFERRED_PENETRATION_DIRECTIONS * 2];
  66. btVector3 separatingAxisInABatch[NUM_UNITSPHERE_POINTS + MAX_PREFERRED_PENETRATION_DIRECTIONS * 2];
  67. btVector3 separatingAxisInBBatch[NUM_UNITSPHERE_POINTS + MAX_PREFERRED_PENETRATION_DIRECTIONS * 2];
  68. int i;
  69. int numSampleDirections = NUM_UNITSPHERE_POINTS;
  70. for (i = 0; i < numSampleDirections; i++)
  71. {
  72. btVector3 norm = getPenetrationDirections()[i];
  73. separatingAxisInABatch[i] = (-norm) * transA.getBasis();
  74. separatingAxisInBBatch[i] = norm * transB.getBasis();
  75. }
  76. {
  77. int numPDA = convexA->getNumPreferredPenetrationDirections();
  78. if (numPDA)
  79. {
  80. for (int i = 0; i < numPDA; i++)
  81. {
  82. btVector3 norm;
  83. convexA->getPreferredPenetrationDirection(i, norm);
  84. norm = transA.getBasis() * norm;
  85. getPenetrationDirections()[numSampleDirections] = norm;
  86. separatingAxisInABatch[numSampleDirections] = (-norm) * transA.getBasis();
  87. separatingAxisInBBatch[numSampleDirections] = norm * transB.getBasis();
  88. numSampleDirections++;
  89. }
  90. }
  91. }
  92. {
  93. int numPDB = convexB->getNumPreferredPenetrationDirections();
  94. if (numPDB)
  95. {
  96. for (int i = 0; i < numPDB; i++)
  97. {
  98. btVector3 norm;
  99. convexB->getPreferredPenetrationDirection(i, norm);
  100. norm = transB.getBasis() * norm;
  101. getPenetrationDirections()[numSampleDirections] = norm;
  102. separatingAxisInABatch[numSampleDirections] = (-norm) * transA.getBasis();
  103. separatingAxisInBBatch[numSampleDirections] = norm * transB.getBasis();
  104. numSampleDirections++;
  105. }
  106. }
  107. }
  108. convexA->batchedUnitVectorGetSupportingVertexWithoutMargin(separatingAxisInABatch, supportVerticesABatch, numSampleDirections);
  109. convexB->batchedUnitVectorGetSupportingVertexWithoutMargin(separatingAxisInBBatch, supportVerticesBBatch, numSampleDirections);
  110. for (i = 0; i < numSampleDirections; i++)
  111. {
  112. btVector3 norm = getPenetrationDirections()[i];
  113. if (check2d)
  114. {
  115. norm[2] = 0.f;
  116. }
  117. if (norm.length2() > 0.01)
  118. {
  119. separatingAxisInA = separatingAxisInABatch[i];
  120. separatingAxisInB = separatingAxisInBBatch[i];
  121. pInA = supportVerticesABatch[i];
  122. qInB = supportVerticesBBatch[i];
  123. pWorld = transA(pInA);
  124. qWorld = transB(qInB);
  125. if (check2d)
  126. {
  127. pWorld[2] = 0.f;
  128. qWorld[2] = 0.f;
  129. }
  130. w = qWorld - pWorld;
  131. btScalar delta = norm.dot(w);
  132. //find smallest delta
  133. if (delta < minProj)
  134. {
  135. minProj = delta;
  136. minNorm = norm;
  137. minA = pWorld;
  138. minB = qWorld;
  139. }
  140. }
  141. }
  142. #else
  143. int numSampleDirections = NUM_UNITSPHERE_POINTS;
  144. #ifndef __SPU__
  145. {
  146. int numPDA = convexA->getNumPreferredPenetrationDirections();
  147. if (numPDA)
  148. {
  149. for (int i = 0; i < numPDA; i++)
  150. {
  151. btVector3 norm;
  152. convexA->getPreferredPenetrationDirection(i, norm);
  153. norm = transA.getBasis() * norm;
  154. getPenetrationDirections()[numSampleDirections] = norm;
  155. numSampleDirections++;
  156. }
  157. }
  158. }
  159. {
  160. int numPDB = convexB->getNumPreferredPenetrationDirections();
  161. if (numPDB)
  162. {
  163. for (int i = 0; i < numPDB; i++)
  164. {
  165. btVector3 norm;
  166. convexB->getPreferredPenetrationDirection(i, norm);
  167. norm = transB.getBasis() * norm;
  168. getPenetrationDirections()[numSampleDirections] = norm;
  169. numSampleDirections++;
  170. }
  171. }
  172. }
  173. #endif // __SPU__
  174. for (int i = 0; i < numSampleDirections; i++)
  175. {
  176. const btVector3& norm = getPenetrationDirections()[i];
  177. separatingAxisInA = (-norm) * transA.getBasis();
  178. separatingAxisInB = norm * transB.getBasis();
  179. pInA = convexA->localGetSupportVertexWithoutMarginNonVirtual(separatingAxisInA);
  180. qInB = convexB->localGetSupportVertexWithoutMarginNonVirtual(separatingAxisInB);
  181. pWorld = transA(pInA);
  182. qWorld = transB(qInB);
  183. w = qWorld - pWorld;
  184. btScalar delta = norm.dot(w);
  185. //find smallest delta
  186. if (delta < minProj)
  187. {
  188. minProj = delta;
  189. minNorm = norm;
  190. minA = pWorld;
  191. minB = qWorld;
  192. }
  193. }
  194. #endif //USE_BATCHED_SUPPORT
  195. //add the margins
  196. minA += minNorm * convexA->getMarginNonVirtual();
  197. minB -= minNorm * convexB->getMarginNonVirtual();
  198. //no penetration
  199. if (minProj < btScalar(0.))
  200. return false;
  201. btScalar extraSeparation = 0.5f; ///scale dependent
  202. minProj += extraSeparation + (convexA->getMarginNonVirtual() + convexB->getMarginNonVirtual());
  203. //#define DEBUG_DRAW 1
  204. #ifdef DEBUG_DRAW
  205. if (debugDraw)
  206. {
  207. btVector3 color(0, 1, 0);
  208. debugDraw->drawLine(minA, minB, color);
  209. color = btVector3(1, 1, 1);
  210. btVector3 vec = minB - minA;
  211. btScalar prj2 = minNorm.dot(vec);
  212. debugDraw->drawLine(minA, minA + (minNorm * minProj), color);
  213. }
  214. #endif //DEBUG_DRAW
  215. btGjkPairDetector gjkdet(convexA, convexB, &simplexSolver, 0);
  216. btScalar offsetDist = minProj;
  217. btVector3 offset = minNorm * offsetDist;
  218. btGjkPairDetector::ClosestPointInput input;
  219. btVector3 newOrg = transA.getOrigin() + offset;
  220. btTransform displacedTrans = transA;
  221. displacedTrans.setOrigin(newOrg);
  222. input.m_transformA = displacedTrans;
  223. input.m_transformB = transB;
  224. input.m_maximumDistanceSquared = btScalar(BT_LARGE_FLOAT); //minProj;
  225. btIntermediateResult res;
  226. gjkdet.setCachedSeparatingAxis(-minNorm);
  227. gjkdet.getClosestPoints(input, res, debugDraw);
  228. btScalar correctedMinNorm = minProj - res.m_depth;
  229. //the penetration depth is over-estimated, relax it
  230. btScalar penetration_relaxation = btScalar(1.);
  231. minNorm *= penetration_relaxation;
  232. if (res.m_hasResult)
  233. {
  234. pa = res.m_pointInWorld - minNorm * correctedMinNorm;
  235. pb = res.m_pointInWorld;
  236. v = minNorm;
  237. #ifdef DEBUG_DRAW
  238. if (debugDraw)
  239. {
  240. btVector3 color(1, 0, 0);
  241. debugDraw->drawLine(pa, pb, color);
  242. }
  243. #endif //DEBUG_DRAW
  244. }
  245. return res.m_hasResult;
  246. }
  247. btVector3* btMinkowskiPenetrationDepthSolver::getPenetrationDirections()
  248. {
  249. static btVector3 sPenetrationDirections[NUM_UNITSPHERE_POINTS + MAX_PREFERRED_PENETRATION_DIRECTIONS * 2] =
  250. {
  251. btVector3(btScalar(0.000000), btScalar(-0.000000), btScalar(-1.000000)),
  252. btVector3(btScalar(0.723608), btScalar(-0.525725), btScalar(-0.447219)),
  253. btVector3(btScalar(-0.276388), btScalar(-0.850649), btScalar(-0.447219)),
  254. btVector3(btScalar(-0.894426), btScalar(-0.000000), btScalar(-0.447216)),
  255. btVector3(btScalar(-0.276388), btScalar(0.850649), btScalar(-0.447220)),
  256. btVector3(btScalar(0.723608), btScalar(0.525725), btScalar(-0.447219)),
  257. btVector3(btScalar(0.276388), btScalar(-0.850649), btScalar(0.447220)),
  258. btVector3(btScalar(-0.723608), btScalar(-0.525725), btScalar(0.447219)),
  259. btVector3(btScalar(-0.723608), btScalar(0.525725), btScalar(0.447219)),
  260. btVector3(btScalar(0.276388), btScalar(0.850649), btScalar(0.447219)),
  261. btVector3(btScalar(0.894426), btScalar(0.000000), btScalar(0.447216)),
  262. btVector3(btScalar(-0.000000), btScalar(0.000000), btScalar(1.000000)),
  263. btVector3(btScalar(0.425323), btScalar(-0.309011), btScalar(-0.850654)),
  264. btVector3(btScalar(-0.162456), btScalar(-0.499995), btScalar(-0.850654)),
  265. btVector3(btScalar(0.262869), btScalar(-0.809012), btScalar(-0.525738)),
  266. btVector3(btScalar(0.425323), btScalar(0.309011), btScalar(-0.850654)),
  267. btVector3(btScalar(0.850648), btScalar(-0.000000), btScalar(-0.525736)),
  268. btVector3(btScalar(-0.525730), btScalar(-0.000000), btScalar(-0.850652)),
  269. btVector3(btScalar(-0.688190), btScalar(-0.499997), btScalar(-0.525736)),
  270. btVector3(btScalar(-0.162456), btScalar(0.499995), btScalar(-0.850654)),
  271. btVector3(btScalar(-0.688190), btScalar(0.499997), btScalar(-0.525736)),
  272. btVector3(btScalar(0.262869), btScalar(0.809012), btScalar(-0.525738)),
  273. btVector3(btScalar(0.951058), btScalar(0.309013), btScalar(0.000000)),
  274. btVector3(btScalar(0.951058), btScalar(-0.309013), btScalar(0.000000)),
  275. btVector3(btScalar(0.587786), btScalar(-0.809017), btScalar(0.000000)),
  276. btVector3(btScalar(0.000000), btScalar(-1.000000), btScalar(0.000000)),
  277. btVector3(btScalar(-0.587786), btScalar(-0.809017), btScalar(0.000000)),
  278. btVector3(btScalar(-0.951058), btScalar(-0.309013), btScalar(-0.000000)),
  279. btVector3(btScalar(-0.951058), btScalar(0.309013), btScalar(-0.000000)),
  280. btVector3(btScalar(-0.587786), btScalar(0.809017), btScalar(-0.000000)),
  281. btVector3(btScalar(-0.000000), btScalar(1.000000), btScalar(-0.000000)),
  282. btVector3(btScalar(0.587786), btScalar(0.809017), btScalar(-0.000000)),
  283. btVector3(btScalar(0.688190), btScalar(-0.499997), btScalar(0.525736)),
  284. btVector3(btScalar(-0.262869), btScalar(-0.809012), btScalar(0.525738)),
  285. btVector3(btScalar(-0.850648), btScalar(0.000000), btScalar(0.525736)),
  286. btVector3(btScalar(-0.262869), btScalar(0.809012), btScalar(0.525738)),
  287. btVector3(btScalar(0.688190), btScalar(0.499997), btScalar(0.525736)),
  288. btVector3(btScalar(0.525730), btScalar(0.000000), btScalar(0.850652)),
  289. btVector3(btScalar(0.162456), btScalar(-0.499995), btScalar(0.850654)),
  290. btVector3(btScalar(-0.425323), btScalar(-0.309011), btScalar(0.850654)),
  291. btVector3(btScalar(-0.425323), btScalar(0.309011), btScalar(0.850654)),
  292. btVector3(btScalar(0.162456), btScalar(0.499995), btScalar(0.850654))};
  293. return sPenetrationDirections;
  294. }