btCompoundCollisionAlgorithm.cpp 13 KB

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