btCompoundCollisionAlgorithm.cpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408
  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 "BulletCollision/CollisionDispatch/btCompoundCollisionAlgorithm.h"
  14. #include "BulletCollision/CollisionDispatch/btCollisionObject.h"
  15. #include "BulletCollision/CollisionShapes/btCompoundShape.h"
  16. #include "BulletCollision/BroadphaseCollision/btDbvt.h"
  17. #include "LinearMath/btIDebugDraw.h"
  18. #include "LinearMath/btAabbUtil2.h"
  19. #include "btManifoldResult.h"
  20. #include "BulletCollision/CollisionDispatch/btCollisionObjectWrapper.h"
  21. btShapePairCallback gCompoundChildShapePairCallback = 0;
  22. btCompoundCollisionAlgorithm::btCompoundCollisionAlgorithm( const btCollisionAlgorithmConstructionInfo& ci,const btCollisionObjectWrapper* body0Wrap,const btCollisionObjectWrapper* body1Wrap,bool isSwapped)
  23. :btActivatingCollisionAlgorithm(ci,body0Wrap,body1Wrap),
  24. m_isSwapped(isSwapped),
  25. m_sharedManifold(ci.m_manifold)
  26. {
  27. m_ownsManifold = false;
  28. const btCollisionObjectWrapper* colObjWrap = m_isSwapped? body1Wrap : body0Wrap;
  29. btAssert (colObjWrap->getCollisionShape()->isCompound());
  30. const btCompoundShape* compoundShape = static_cast<const btCompoundShape*>(colObjWrap->getCollisionShape());
  31. m_compoundShapeRevision = compoundShape->getUpdateRevision();
  32. preallocateChildAlgorithms(body0Wrap,body1Wrap);
  33. }
  34. void btCompoundCollisionAlgorithm::preallocateChildAlgorithms(const btCollisionObjectWrapper* body0Wrap,const btCollisionObjectWrapper* body1Wrap)
  35. {
  36. const btCollisionObjectWrapper* colObjWrap = m_isSwapped? body1Wrap : body0Wrap;
  37. const btCollisionObjectWrapper* otherObjWrap = m_isSwapped? body0Wrap : body1Wrap;
  38. btAssert (colObjWrap->getCollisionShape()->isCompound());
  39. const btCompoundShape* compoundShape = static_cast<const btCompoundShape*>(colObjWrap->getCollisionShape());
  40. int numChildren = compoundShape->getNumChildShapes();
  41. int i;
  42. m_childCollisionAlgorithms.resize(numChildren);
  43. for (i=0;i<numChildren;i++)
  44. {
  45. if (compoundShape->getDynamicAabbTree())
  46. {
  47. m_childCollisionAlgorithms[i] = 0;
  48. } else
  49. {
  50. const btCollisionShape* childShape = compoundShape->getChildShape(i);
  51. btCollisionObjectWrapper childWrap(colObjWrap,childShape,colObjWrap->getCollisionObject(),colObjWrap->getWorldTransform(),-1,i);//wrong child trans, but unused (hopefully)
  52. m_childCollisionAlgorithms[i] = m_dispatcher->findAlgorithm(&childWrap,otherObjWrap,m_sharedManifold, BT_CONTACT_POINT_ALGORITHMS);
  53. btAlignedObjectArray<btCollisionAlgorithm*> m_childCollisionAlgorithmsContact;
  54. btAlignedObjectArray<btCollisionAlgorithm*> m_childCollisionAlgorithmsClosestPoints;
  55. }
  56. }
  57. }
  58. void btCompoundCollisionAlgorithm::removeChildAlgorithms()
  59. {
  60. int numChildren = m_childCollisionAlgorithms.size();
  61. int i;
  62. for (i=0;i<numChildren;i++)
  63. {
  64. if (m_childCollisionAlgorithms[i])
  65. {
  66. m_childCollisionAlgorithms[i]->~btCollisionAlgorithm();
  67. m_dispatcher->freeCollisionAlgorithm(m_childCollisionAlgorithms[i]);
  68. }
  69. }
  70. }
  71. btCompoundCollisionAlgorithm::~btCompoundCollisionAlgorithm()
  72. {
  73. removeChildAlgorithms();
  74. }
  75. struct btCompoundLeafCallback : btDbvt::ICollide
  76. {
  77. public:
  78. const btCollisionObjectWrapper* m_compoundColObjWrap;
  79. const btCollisionObjectWrapper* m_otherObjWrap;
  80. btDispatcher* m_dispatcher;
  81. const btDispatcherInfo& m_dispatchInfo;
  82. btManifoldResult* m_resultOut;
  83. btCollisionAlgorithm** m_childCollisionAlgorithms;
  84. btPersistentManifold* m_sharedManifold;
  85. btCompoundLeafCallback (const btCollisionObjectWrapper* compoundObjWrap,const btCollisionObjectWrapper* otherObjWrap,btDispatcher* dispatcher,const btDispatcherInfo& dispatchInfo,btManifoldResult* resultOut,btCollisionAlgorithm** childCollisionAlgorithms,btPersistentManifold* sharedManifold)
  86. :m_compoundColObjWrap(compoundObjWrap),m_otherObjWrap(otherObjWrap),m_dispatcher(dispatcher),m_dispatchInfo(dispatchInfo),m_resultOut(resultOut),
  87. m_childCollisionAlgorithms(childCollisionAlgorithms),
  88. m_sharedManifold(sharedManifold)
  89. {
  90. }
  91. void ProcessChildShape(const btCollisionShape* childShape,int index)
  92. {
  93. btAssert(index>=0);
  94. const btCompoundShape* compoundShape = static_cast<const btCompoundShape*>(m_compoundColObjWrap->getCollisionShape());
  95. btAssert(index<compoundShape->getNumChildShapes());
  96. //backup
  97. btTransform orgTrans = m_compoundColObjWrap->getWorldTransform();
  98. const btTransform& childTrans = compoundShape->getChildTransform(index);
  99. btTransform newChildWorldTrans = orgTrans*childTrans ;
  100. //perform an AABB check first
  101. btVector3 aabbMin0,aabbMax0;
  102. childShape->getAabb(newChildWorldTrans,aabbMin0,aabbMax0);
  103. btVector3 extendAabb(m_resultOut->m_closestPointDistanceThreshold, m_resultOut->m_closestPointDistanceThreshold, m_resultOut->m_closestPointDistanceThreshold);
  104. aabbMin0 -= extendAabb;
  105. aabbMax0 += extendAabb;
  106. btVector3 aabbMin1, aabbMax1;
  107. m_otherObjWrap->getCollisionShape()->getAabb(m_otherObjWrap->getWorldTransform(),aabbMin1,aabbMax1);
  108. if (gCompoundChildShapePairCallback)
  109. {
  110. if (!gCompoundChildShapePairCallback(m_otherObjWrap->getCollisionShape(), childShape))
  111. return;
  112. }
  113. if (TestAabbAgainstAabb2(aabbMin0,aabbMax0,aabbMin1,aabbMax1))
  114. {
  115. btCollisionObjectWrapper compoundWrap(this->m_compoundColObjWrap,childShape,m_compoundColObjWrap->getCollisionObject(),newChildWorldTrans,-1,index);
  116. btCollisionAlgorithm* algo = 0;
  117. bool allocatedAlgorithm = false;
  118. if (m_resultOut->m_closestPointDistanceThreshold > 0)
  119. {
  120. algo = m_dispatcher->findAlgorithm(&compoundWrap, m_otherObjWrap, 0, BT_CLOSEST_POINT_ALGORITHMS);
  121. allocatedAlgorithm = true;
  122. }
  123. else
  124. {
  125. //the contactpoint is still projected back using the original inverted worldtrans
  126. if (!m_childCollisionAlgorithms[index])
  127. {
  128. m_childCollisionAlgorithms[index] = m_dispatcher->findAlgorithm(&compoundWrap, m_otherObjWrap, m_sharedManifold, BT_CONTACT_POINT_ALGORITHMS);
  129. }
  130. algo = m_childCollisionAlgorithms[index];
  131. }
  132. const btCollisionObjectWrapper* tmpWrap = 0;
  133. ///detect swapping case
  134. if (m_resultOut->getBody0Internal() == m_compoundColObjWrap->getCollisionObject())
  135. {
  136. tmpWrap = m_resultOut->getBody0Wrap();
  137. m_resultOut->setBody0Wrap(&compoundWrap);
  138. m_resultOut->setShapeIdentifiersA(-1,index);
  139. } else
  140. {
  141. tmpWrap = m_resultOut->getBody1Wrap();
  142. m_resultOut->setBody1Wrap(&compoundWrap);
  143. m_resultOut->setShapeIdentifiersB(-1,index);
  144. }
  145. algo->processCollision(&compoundWrap,m_otherObjWrap,m_dispatchInfo,m_resultOut);
  146. #if 0
  147. if (m_dispatchInfo.m_debugDraw && (m_dispatchInfo.m_debugDraw->getDebugMode() & btIDebugDraw::DBG_DrawAabb))
  148. {
  149. btVector3 worldAabbMin,worldAabbMax;
  150. m_dispatchInfo.m_debugDraw->drawAabb(aabbMin0,aabbMax0,btVector3(1,1,1));
  151. m_dispatchInfo.m_debugDraw->drawAabb(aabbMin1,aabbMax1,btVector3(1,1,1));
  152. }
  153. #endif
  154. if (m_resultOut->getBody0Internal() == m_compoundColObjWrap->getCollisionObject())
  155. {
  156. m_resultOut->setBody0Wrap(tmpWrap);
  157. } else
  158. {
  159. m_resultOut->setBody1Wrap(tmpWrap);
  160. }
  161. if(allocatedAlgorithm)
  162. {
  163. algo->~btCollisionAlgorithm();
  164. m_dispatcher->freeCollisionAlgorithm(algo);
  165. }
  166. }
  167. }
  168. void Process(const btDbvtNode* leaf)
  169. {
  170. int index = leaf->dataAsInt;
  171. const btCompoundShape* compoundShape = static_cast<const btCompoundShape*>(m_compoundColObjWrap->getCollisionShape());
  172. const btCollisionShape* childShape = compoundShape->getChildShape(index);
  173. #if 0
  174. if (m_dispatchInfo.m_debugDraw && (m_dispatchInfo.m_debugDraw->getDebugMode() & btIDebugDraw::DBG_DrawAabb))
  175. {
  176. btVector3 worldAabbMin,worldAabbMax;
  177. btTransform orgTrans = m_compoundColObjWrap->getWorldTransform();
  178. btTransformAabb(leaf->volume.Mins(),leaf->volume.Maxs(),0.,orgTrans,worldAabbMin,worldAabbMax);
  179. m_dispatchInfo.m_debugDraw->drawAabb(worldAabbMin,worldAabbMax,btVector3(1,0,0));
  180. }
  181. #endif
  182. ProcessChildShape(childShape,index);
  183. }
  184. };
  185. void btCompoundCollisionAlgorithm::processCollision (const btCollisionObjectWrapper* body0Wrap,const btCollisionObjectWrapper* body1Wrap,const btDispatcherInfo& dispatchInfo,btManifoldResult* resultOut)
  186. {
  187. const btCollisionObjectWrapper* colObjWrap = m_isSwapped? body1Wrap : body0Wrap;
  188. const btCollisionObjectWrapper* otherObjWrap = m_isSwapped? body0Wrap : body1Wrap;
  189. btAssert (colObjWrap->getCollisionShape()->isCompound());
  190. const btCompoundShape* compoundShape = static_cast<const btCompoundShape*>(colObjWrap->getCollisionShape());
  191. ///btCompoundShape might have changed:
  192. ////make sure the internal child collision algorithm caches are still valid
  193. if (compoundShape->getUpdateRevision() != m_compoundShapeRevision)
  194. {
  195. ///clear and update all
  196. removeChildAlgorithms();
  197. preallocateChildAlgorithms(body0Wrap,body1Wrap);
  198. m_compoundShapeRevision = compoundShape->getUpdateRevision();
  199. }
  200. if (m_childCollisionAlgorithms.size()==0)
  201. return;
  202. const btDbvt* tree = compoundShape->getDynamicAabbTree();
  203. //use a dynamic aabb tree to cull potential child-overlaps
  204. btCompoundLeafCallback callback(colObjWrap,otherObjWrap,m_dispatcher,dispatchInfo,resultOut,&m_childCollisionAlgorithms[0],m_sharedManifold);
  205. ///we need to refresh all contact manifolds
  206. ///note that we should actually recursively traverse all children, btCompoundShape can nested more then 1 level deep
  207. ///so we should add a 'refreshManifolds' in the btCollisionAlgorithm
  208. {
  209. int i;
  210. manifoldArray.resize(0);
  211. for (i=0;i<m_childCollisionAlgorithms.size();i++)
  212. {
  213. if (m_childCollisionAlgorithms[i])
  214. {
  215. m_childCollisionAlgorithms[i]->getAllContactManifolds(manifoldArray);
  216. for (int m=0;m<manifoldArray.size();m++)
  217. {
  218. if (manifoldArray[m]->getNumContacts())
  219. {
  220. resultOut->setPersistentManifold(manifoldArray[m]);
  221. resultOut->refreshContactPoints();
  222. resultOut->setPersistentManifold(0);//??necessary?
  223. }
  224. }
  225. manifoldArray.resize(0);
  226. }
  227. }
  228. }
  229. if (tree)
  230. {
  231. btVector3 localAabbMin,localAabbMax;
  232. btTransform otherInCompoundSpace;
  233. otherInCompoundSpace = colObjWrap->getWorldTransform().inverse() * otherObjWrap->getWorldTransform();
  234. otherObjWrap->getCollisionShape()->getAabb(otherInCompoundSpace,localAabbMin,localAabbMax);
  235. btVector3 extraExtends(resultOut->m_closestPointDistanceThreshold, resultOut->m_closestPointDistanceThreshold, resultOut->m_closestPointDistanceThreshold);
  236. localAabbMin -= extraExtends;
  237. localAabbMax += extraExtends;
  238. const ATTRIBUTE_ALIGNED16(btDbvtVolume) bounds=btDbvtVolume::FromMM(localAabbMin,localAabbMax);
  239. //process all children, that overlap with the given AABB bounds
  240. tree->collideTVNoStackAlloc(tree->m_root,bounds,stack2,callback);
  241. } else
  242. {
  243. //iterate over all children, perform an AABB check inside ProcessChildShape
  244. int numChildren = m_childCollisionAlgorithms.size();
  245. int i;
  246. for (i=0;i<numChildren;i++)
  247. {
  248. callback.ProcessChildShape(compoundShape->getChildShape(i),i);
  249. }
  250. }
  251. {
  252. //iterate over all children, perform an AABB check inside ProcessChildShape
  253. int numChildren = m_childCollisionAlgorithms.size();
  254. int i;
  255. manifoldArray.resize(0);
  256. const btCollisionShape* childShape = 0;
  257. btTransform orgTrans;
  258. btTransform newChildWorldTrans;
  259. btVector3 aabbMin0,aabbMax0,aabbMin1,aabbMax1;
  260. for (i=0;i<numChildren;i++)
  261. {
  262. if (m_childCollisionAlgorithms[i])
  263. {
  264. childShape = compoundShape->getChildShape(i);
  265. //if not longer overlapping, remove the algorithm
  266. orgTrans = colObjWrap->getWorldTransform();
  267. const btTransform& childTrans = compoundShape->getChildTransform(i);
  268. newChildWorldTrans = orgTrans*childTrans ;
  269. //perform an AABB check first
  270. childShape->getAabb(newChildWorldTrans,aabbMin0,aabbMax0);
  271. otherObjWrap->getCollisionShape()->getAabb(otherObjWrap->getWorldTransform(),aabbMin1,aabbMax1);
  272. if (!TestAabbAgainstAabb2(aabbMin0,aabbMax0,aabbMin1,aabbMax1))
  273. {
  274. m_childCollisionAlgorithms[i]->~btCollisionAlgorithm();
  275. m_dispatcher->freeCollisionAlgorithm(m_childCollisionAlgorithms[i]);
  276. m_childCollisionAlgorithms[i] = 0;
  277. }
  278. }
  279. }
  280. }
  281. }
  282. btScalar btCompoundCollisionAlgorithm::calculateTimeOfImpact(btCollisionObject* body0,btCollisionObject* body1,const btDispatcherInfo& dispatchInfo,btManifoldResult* resultOut)
  283. {
  284. btAssert(0);
  285. //needs to be fixed, using btCollisionObjectWrapper and NOT modifying internal data structures
  286. btCollisionObject* colObj = m_isSwapped? body1 : body0;
  287. btCollisionObject* otherObj = m_isSwapped? body0 : body1;
  288. btAssert (colObj->getCollisionShape()->isCompound());
  289. btCompoundShape* compoundShape = static_cast<btCompoundShape*>(colObj->getCollisionShape());
  290. //We will use the OptimizedBVH, AABB tree to cull potential child-overlaps
  291. //If both proxies are Compound, we will deal with that directly, by performing sequential/parallel tree traversals
  292. //given Proxy0 and Proxy1, if both have a tree, Tree0 and Tree1, this means:
  293. //determine overlapping nodes of Proxy1 using Proxy0 AABB against Tree1
  294. //then use each overlapping node AABB against Tree0
  295. //and vise versa.
  296. btScalar hitFraction = btScalar(1.);
  297. int numChildren = m_childCollisionAlgorithms.size();
  298. int i;
  299. btTransform orgTrans;
  300. btScalar frac;
  301. for (i=0;i<numChildren;i++)
  302. {
  303. //btCollisionShape* childShape = compoundShape->getChildShape(i);
  304. //backup
  305. orgTrans = colObj->getWorldTransform();
  306. const btTransform& childTrans = compoundShape->getChildTransform(i);
  307. //btTransform newChildWorldTrans = orgTrans*childTrans ;
  308. colObj->setWorldTransform( orgTrans*childTrans );
  309. //btCollisionShape* tmpShape = colObj->getCollisionShape();
  310. //colObj->internalSetTemporaryCollisionShape( childShape );
  311. frac = m_childCollisionAlgorithms[i]->calculateTimeOfImpact(colObj,otherObj,dispatchInfo,resultOut);
  312. if (frac<hitFraction)
  313. {
  314. hitFraction = frac;
  315. }
  316. //revert back
  317. //colObj->internalSetTemporaryCollisionShape( tmpShape);
  318. colObj->setWorldTransform( orgTrans);
  319. }
  320. return hitFraction;
  321. }