btCompoundCompoundCollisionAlgorithm.cpp 14 KB

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