btCompoundCompoundCollisionAlgorithm.cpp 15 KB

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