btCompoundCompoundCollisionAlgorithm.cpp 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414
  1. /*
  2. Bullet Continuous Collision Detection and Physics Library
  3. Copyright (c) 2003-2013 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. #include "btCompoundCompoundCollisionAlgorithm.h"
  14. #include "LinearMath/btQuickprof.h"
  15. #include "BulletCollision/CollisionDispatch/btCollisionObject.h"
  16. #include "BulletCollision/CollisionShapes/btCompoundShape.h"
  17. #include "BulletCollision/BroadphaseCollision/btDbvt.h"
  18. #include "LinearMath/btIDebugDraw.h"
  19. #include "LinearMath/btAabbUtil2.h"
  20. #include "BulletCollision/CollisionDispatch/btManifoldResult.h"
  21. #include "BulletCollision/CollisionDispatch/btCollisionObjectWrapper.h"
  22. //USE_LOCAL_STACK will avoid most (often all) dynamic memory allocations due to resizing in processCollision and MycollideTT
  23. #define USE_LOCAL_STACK 1
  24. btShapePairCallback gCompoundCompoundChildShapePairCallback = 0;
  25. btCompoundCompoundCollisionAlgorithm::btCompoundCompoundCollisionAlgorithm(const btCollisionAlgorithmConstructionInfo& ci, const btCollisionObjectWrapper* body0Wrap, const btCollisionObjectWrapper* body1Wrap, bool isSwapped)
  26. : btCompoundCollisionAlgorithm(ci, body0Wrap, body1Wrap, isSwapped)
  27. {
  28. void* ptr = btAlignedAlloc(sizeof(btHashedSimplePairCache), 16);
  29. m_childCollisionAlgorithmCache = new (ptr) btHashedSimplePairCache();
  30. const btCollisionObjectWrapper* col0ObjWrap = body0Wrap;
  31. btAssert(col0ObjWrap->getCollisionShape()->isCompound());
  32. const btCollisionObjectWrapper* col1ObjWrap = body1Wrap;
  33. btAssert(col1ObjWrap->getCollisionShape()->isCompound());
  34. const btCompoundShape* compoundShape0 = static_cast<const btCompoundShape*>(col0ObjWrap->getCollisionShape());
  35. m_compoundShapeRevision0 = compoundShape0->getUpdateRevision();
  36. const btCompoundShape* compoundShape1 = static_cast<const btCompoundShape*>(col1ObjWrap->getCollisionShape());
  37. m_compoundShapeRevision1 = compoundShape1->getUpdateRevision();
  38. }
  39. btCompoundCompoundCollisionAlgorithm::~btCompoundCompoundCollisionAlgorithm()
  40. {
  41. removeChildAlgorithms();
  42. m_childCollisionAlgorithmCache->~btHashedSimplePairCache();
  43. btAlignedFree(m_childCollisionAlgorithmCache);
  44. }
  45. void btCompoundCompoundCollisionAlgorithm::getAllContactManifolds(btManifoldArray& manifoldArray)
  46. {
  47. int i;
  48. btSimplePairArray& pairs = m_childCollisionAlgorithmCache->getOverlappingPairArray();
  49. for (i = 0; i < pairs.size(); i++)
  50. {
  51. if (pairs[i].m_userPointer)
  52. {
  53. ((btCollisionAlgorithm*)pairs[i].m_userPointer)->getAllContactManifolds(manifoldArray);
  54. }
  55. }
  56. }
  57. void btCompoundCompoundCollisionAlgorithm::removeChildAlgorithms()
  58. {
  59. btSimplePairArray& pairs = m_childCollisionAlgorithmCache->getOverlappingPairArray();
  60. int numChildren = pairs.size();
  61. int i;
  62. for (i = 0; i < numChildren; i++)
  63. {
  64. if (pairs[i].m_userPointer)
  65. {
  66. btCollisionAlgorithm* algo = (btCollisionAlgorithm*)pairs[i].m_userPointer;
  67. algo->~btCollisionAlgorithm();
  68. m_dispatcher->freeCollisionAlgorithm(algo);
  69. }
  70. }
  71. m_childCollisionAlgorithmCache->removeAllPairs();
  72. }
  73. struct btCompoundCompoundLeafCallback : btDbvt::ICollide
  74. {
  75. int m_numOverlapPairs;
  76. const btCollisionObjectWrapper* m_compound0ColObjWrap;
  77. const btCollisionObjectWrapper* m_compound1ColObjWrap;
  78. btDispatcher* m_dispatcher;
  79. const btDispatcherInfo& m_dispatchInfo;
  80. btManifoldResult* m_resultOut;
  81. class btHashedSimplePairCache* m_childCollisionAlgorithmCache;
  82. btPersistentManifold* m_sharedManifold;
  83. btCompoundCompoundLeafCallback(const btCollisionObjectWrapper* compound1ObjWrap,
  84. const btCollisionObjectWrapper* compound0ObjWrap,
  85. btDispatcher* dispatcher,
  86. const btDispatcherInfo& dispatchInfo,
  87. btManifoldResult* resultOut,
  88. btHashedSimplePairCache* childAlgorithmsCache,
  89. btPersistentManifold* sharedManifold)
  90. : m_numOverlapPairs(0), m_compound0ColObjWrap(compound1ObjWrap), m_compound1ColObjWrap(compound0ObjWrap), m_dispatcher(dispatcher), m_dispatchInfo(dispatchInfo), m_resultOut(resultOut), m_childCollisionAlgorithmCache(childAlgorithmsCache), m_sharedManifold(sharedManifold)
  91. {
  92. }
  93. void Process(const btDbvtNode* leaf0, const btDbvtNode* leaf1)
  94. {
  95. BT_PROFILE("btCompoundCompoundLeafCallback::Process");
  96. m_numOverlapPairs++;
  97. int childIndex0 = leaf0->dataAsInt;
  98. int childIndex1 = leaf1->dataAsInt;
  99. btAssert(childIndex0 >= 0);
  100. btAssert(childIndex1 >= 0);
  101. const btCompoundShape* compoundShape0 = static_cast<const btCompoundShape*>(m_compound0ColObjWrap->getCollisionShape());
  102. btAssert(childIndex0 < compoundShape0->getNumChildShapes());
  103. const btCompoundShape* compoundShape1 = static_cast<const btCompoundShape*>(m_compound1ColObjWrap->getCollisionShape());
  104. btAssert(childIndex1 < compoundShape1->getNumChildShapes());
  105. const btCollisionShape* childShape0 = compoundShape0->getChildShape(childIndex0);
  106. const btCollisionShape* childShape1 = compoundShape1->getChildShape(childIndex1);
  107. //backup
  108. btTransform orgTrans0 = m_compound0ColObjWrap->getWorldTransform();
  109. const btTransform& childTrans0 = compoundShape0->getChildTransform(childIndex0);
  110. btTransform newChildWorldTrans0 = orgTrans0 * childTrans0;
  111. btTransform orgTrans1 = m_compound1ColObjWrap->getWorldTransform();
  112. const btTransform& childTrans1 = compoundShape1->getChildTransform(childIndex1);
  113. btTransform newChildWorldTrans1 = orgTrans1 * childTrans1;
  114. //perform an AABB check first
  115. btVector3 aabbMin0, aabbMax0, aabbMin1, aabbMax1;
  116. childShape0->getAabb(newChildWorldTrans0, aabbMin0, aabbMax0);
  117. childShape1->getAabb(newChildWorldTrans1, aabbMin1, aabbMax1);
  118. btVector3 thresholdVec(m_resultOut->m_closestPointDistanceThreshold, m_resultOut->m_closestPointDistanceThreshold, m_resultOut->m_closestPointDistanceThreshold);
  119. aabbMin0 -= thresholdVec;
  120. aabbMax0 += thresholdVec;
  121. if (gCompoundCompoundChildShapePairCallback)
  122. {
  123. if (!gCompoundCompoundChildShapePairCallback(childShape0, childShape1))
  124. return;
  125. }
  126. if (TestAabbAgainstAabb2(aabbMin0, aabbMax0, aabbMin1, aabbMax1))
  127. {
  128. btCollisionObjectWrapper compoundWrap0(this->m_compound0ColObjWrap, childShape0, m_compound0ColObjWrap->getCollisionObject(), newChildWorldTrans0, -1, childIndex0);
  129. btCollisionObjectWrapper compoundWrap1(this->m_compound1ColObjWrap, childShape1, m_compound1ColObjWrap->getCollisionObject(), newChildWorldTrans1, -1, childIndex1);
  130. btSimplePair* pair = m_childCollisionAlgorithmCache->findPair(childIndex0, childIndex1);
  131. bool removePair = false;
  132. btCollisionAlgorithm* colAlgo = 0;
  133. if (m_resultOut->m_closestPointDistanceThreshold > 0)
  134. {
  135. colAlgo = m_dispatcher->findAlgorithm(&compoundWrap0, &compoundWrap1, 0, BT_CLOSEST_POINT_ALGORITHMS);
  136. removePair = true;
  137. }
  138. else
  139. {
  140. if (pair)
  141. {
  142. colAlgo = (btCollisionAlgorithm*)pair->m_userPointer;
  143. }
  144. else
  145. {
  146. colAlgo = m_dispatcher->findAlgorithm(&compoundWrap0, &compoundWrap1, m_sharedManifold, BT_CONTACT_POINT_ALGORITHMS);
  147. pair = m_childCollisionAlgorithmCache->addOverlappingPair(childIndex0, childIndex1);
  148. btAssert(pair);
  149. pair->m_userPointer = colAlgo;
  150. }
  151. }
  152. btAssert(colAlgo);
  153. const btCollisionObjectWrapper* tmpWrap0 = 0;
  154. const btCollisionObjectWrapper* tmpWrap1 = 0;
  155. tmpWrap0 = m_resultOut->getBody0Wrap();
  156. tmpWrap1 = m_resultOut->getBody1Wrap();
  157. m_resultOut->setBody0Wrap(&compoundWrap0);
  158. m_resultOut->setBody1Wrap(&compoundWrap1);
  159. m_resultOut->setShapeIdentifiersA(-1, childIndex0);
  160. m_resultOut->setShapeIdentifiersB(-1, childIndex1);
  161. colAlgo->processCollision(&compoundWrap0, &compoundWrap1, m_dispatchInfo, m_resultOut);
  162. m_resultOut->setBody0Wrap(tmpWrap0);
  163. m_resultOut->setBody1Wrap(tmpWrap1);
  164. if (removePair)
  165. {
  166. colAlgo->~btCollisionAlgorithm();
  167. m_dispatcher->freeCollisionAlgorithm(colAlgo);
  168. }
  169. }
  170. }
  171. };
  172. static DBVT_INLINE bool MyIntersect(const btDbvtAabbMm& a,
  173. const btDbvtAabbMm& b, const btTransform& xform, btScalar distanceThreshold)
  174. {
  175. btVector3 newmin, newmax;
  176. btTransformAabb(b.Mins(), b.Maxs(), 0.f, xform, newmin, newmax);
  177. newmin -= btVector3(distanceThreshold, distanceThreshold, distanceThreshold);
  178. newmax += btVector3(distanceThreshold, distanceThreshold, distanceThreshold);
  179. btDbvtAabbMm newb = btDbvtAabbMm::FromMM(newmin, newmax);
  180. return Intersect(a, newb);
  181. }
  182. static inline void MycollideTT(const btDbvtNode* root0,
  183. const btDbvtNode* root1,
  184. const btTransform& xform,
  185. btCompoundCompoundLeafCallback* callback, btScalar distanceThreshold)
  186. {
  187. if (root0 && root1)
  188. {
  189. int depth = 1;
  190. int treshold = btDbvt::DOUBLE_STACKSIZE - 4;
  191. btAlignedObjectArray<btDbvt::sStkNN> stkStack;
  192. #ifdef USE_LOCAL_STACK
  193. ATTRIBUTE_ALIGNED16(btDbvt::sStkNN localStack[btDbvt::DOUBLE_STACKSIZE]);
  194. stkStack.initializeFromBuffer(&localStack, btDbvt::DOUBLE_STACKSIZE, btDbvt::DOUBLE_STACKSIZE);
  195. #else
  196. stkStack.resize(btDbvt::DOUBLE_STACKSIZE);
  197. #endif
  198. stkStack[0] = btDbvt::sStkNN(root0, root1);
  199. do
  200. {
  201. btDbvt::sStkNN p = stkStack[--depth];
  202. if (MyIntersect(p.a->volume, p.b->volume, xform, distanceThreshold))
  203. {
  204. if (depth > treshold)
  205. {
  206. stkStack.resize(stkStack.size() * 2);
  207. treshold = stkStack.size() - 4;
  208. }
  209. if (p.a->isinternal())
  210. {
  211. if (p.b->isinternal())
  212. {
  213. stkStack[depth++] = btDbvt::sStkNN(p.a->childs[0], p.b->childs[0]);
  214. stkStack[depth++] = btDbvt::sStkNN(p.a->childs[1], p.b->childs[0]);
  215. stkStack[depth++] = btDbvt::sStkNN(p.a->childs[0], p.b->childs[1]);
  216. stkStack[depth++] = btDbvt::sStkNN(p.a->childs[1], p.b->childs[1]);
  217. }
  218. else
  219. {
  220. stkStack[depth++] = btDbvt::sStkNN(p.a->childs[0], p.b);
  221. stkStack[depth++] = btDbvt::sStkNN(p.a->childs[1], p.b);
  222. }
  223. }
  224. else
  225. {
  226. if (p.b->isinternal())
  227. {
  228. stkStack[depth++] = btDbvt::sStkNN(p.a, p.b->childs[0]);
  229. stkStack[depth++] = btDbvt::sStkNN(p.a, p.b->childs[1]);
  230. }
  231. else
  232. {
  233. callback->Process(p.a, p.b);
  234. }
  235. }
  236. }
  237. } while (depth);
  238. }
  239. }
  240. void btCompoundCompoundCollisionAlgorithm::processCollision(const btCollisionObjectWrapper* body0Wrap, const btCollisionObjectWrapper* body1Wrap, const btDispatcherInfo& dispatchInfo, btManifoldResult* resultOut)
  241. {
  242. const btCollisionObjectWrapper* col0ObjWrap = body0Wrap;
  243. const btCollisionObjectWrapper* col1ObjWrap = body1Wrap;
  244. btAssert(col0ObjWrap->getCollisionShape()->isCompound());
  245. btAssert(col1ObjWrap->getCollisionShape()->isCompound());
  246. const btCompoundShape* compoundShape0 = static_cast<const btCompoundShape*>(col0ObjWrap->getCollisionShape());
  247. const btCompoundShape* compoundShape1 = static_cast<const btCompoundShape*>(col1ObjWrap->getCollisionShape());
  248. const btDbvt* tree0 = compoundShape0->getDynamicAabbTree();
  249. const btDbvt* tree1 = compoundShape1->getDynamicAabbTree();
  250. if (!tree0 || !tree1)
  251. {
  252. return btCompoundCollisionAlgorithm::processCollision(body0Wrap, body1Wrap, dispatchInfo, resultOut);
  253. }
  254. ///btCompoundShape might have changed:
  255. ////make sure the internal child collision algorithm caches are still valid
  256. if ((compoundShape0->getUpdateRevision() != m_compoundShapeRevision0) || (compoundShape1->getUpdateRevision() != m_compoundShapeRevision1))
  257. {
  258. ///clear all
  259. removeChildAlgorithms();
  260. m_compoundShapeRevision0 = compoundShape0->getUpdateRevision();
  261. m_compoundShapeRevision1 = compoundShape1->getUpdateRevision();
  262. }
  263. ///we need to refresh all contact manifolds
  264. ///note that we should actually recursively traverse all children, btCompoundShape can nested more then 1 level deep
  265. ///so we should add a 'refreshManifolds' in the btCollisionAlgorithm
  266. {
  267. int i;
  268. btManifoldArray manifoldArray;
  269. #ifdef USE_LOCAL_STACK
  270. btPersistentManifold localManifolds[4];
  271. manifoldArray.initializeFromBuffer(&localManifolds, 0, 4);
  272. #endif
  273. btSimplePairArray& pairs = m_childCollisionAlgorithmCache->getOverlappingPairArray();
  274. for (i = 0; i < pairs.size(); i++)
  275. {
  276. if (pairs[i].m_userPointer)
  277. {
  278. btCollisionAlgorithm* algo = (btCollisionAlgorithm*)pairs[i].m_userPointer;
  279. algo->getAllContactManifolds(manifoldArray);
  280. for (int m = 0; m < manifoldArray.size(); m++)
  281. {
  282. if (manifoldArray[m]->getNumContacts())
  283. {
  284. resultOut->setPersistentManifold(manifoldArray[m]);
  285. resultOut->refreshContactPoints();
  286. resultOut->setPersistentManifold(0);
  287. }
  288. }
  289. manifoldArray.resize(0);
  290. }
  291. }
  292. }
  293. btCompoundCompoundLeafCallback callback(col0ObjWrap, col1ObjWrap, this->m_dispatcher, dispatchInfo, resultOut, this->m_childCollisionAlgorithmCache, m_sharedManifold);
  294. const btTransform xform = col0ObjWrap->getWorldTransform().inverse() * col1ObjWrap->getWorldTransform();
  295. MycollideTT(tree0->m_root, tree1->m_root, xform, &callback, resultOut->m_closestPointDistanceThreshold);
  296. //printf("#compound-compound child/leaf overlap =%d \r",callback.m_numOverlapPairs);
  297. //remove non-overlapping child pairs
  298. {
  299. btAssert(m_removePairs.size() == 0);
  300. //iterate over all children, perform an AABB check inside ProcessChildShape
  301. btSimplePairArray& pairs = m_childCollisionAlgorithmCache->getOverlappingPairArray();
  302. int i;
  303. btManifoldArray manifoldArray;
  304. btVector3 aabbMin0, aabbMax0, aabbMin1, aabbMax1;
  305. for (i = 0; i < pairs.size(); i++)
  306. {
  307. if (pairs[i].m_userPointer)
  308. {
  309. btCollisionAlgorithm* algo = (btCollisionAlgorithm*)pairs[i].m_userPointer;
  310. {
  311. const btCollisionShape* childShape0 = 0;
  312. btTransform newChildWorldTrans0;
  313. childShape0 = compoundShape0->getChildShape(pairs[i].m_indexA);
  314. const btTransform& childTrans0 = compoundShape0->getChildTransform(pairs[i].m_indexA);
  315. newChildWorldTrans0 = col0ObjWrap->getWorldTransform() * childTrans0;
  316. childShape0->getAabb(newChildWorldTrans0, aabbMin0, aabbMax0);
  317. }
  318. btVector3 thresholdVec(resultOut->m_closestPointDistanceThreshold, resultOut->m_closestPointDistanceThreshold, resultOut->m_closestPointDistanceThreshold);
  319. aabbMin0 -= thresholdVec;
  320. aabbMax0 += thresholdVec;
  321. {
  322. const btCollisionShape* childShape1 = 0;
  323. btTransform newChildWorldTrans1;
  324. childShape1 = compoundShape1->getChildShape(pairs[i].m_indexB);
  325. const btTransform& childTrans1 = compoundShape1->getChildTransform(pairs[i].m_indexB);
  326. newChildWorldTrans1 = col1ObjWrap->getWorldTransform() * childTrans1;
  327. childShape1->getAabb(newChildWorldTrans1, aabbMin1, aabbMax1);
  328. }
  329. aabbMin1 -= thresholdVec;
  330. aabbMax1 += thresholdVec;
  331. if (!TestAabbAgainstAabb2(aabbMin0, aabbMax0, aabbMin1, aabbMax1))
  332. {
  333. algo->~btCollisionAlgorithm();
  334. m_dispatcher->freeCollisionAlgorithm(algo);
  335. m_removePairs.push_back(btSimplePair(pairs[i].m_indexA, pairs[i].m_indexB));
  336. }
  337. }
  338. }
  339. for (int i = 0; i < m_removePairs.size(); i++)
  340. {
  341. m_childCollisionAlgorithmCache->removeOverlappingPair(m_removePairs[i].m_indexA, m_removePairs[i].m_indexB);
  342. }
  343. m_removePairs.clear();
  344. }
  345. }
  346. btScalar btCompoundCompoundCollisionAlgorithm::calculateTimeOfImpact(btCollisionObject* body0, btCollisionObject* body1, const btDispatcherInfo& dispatchInfo, btManifoldResult* resultOut)
  347. {
  348. btAssert(0);
  349. return 0.f;
  350. }