btCompoundCollisionAlgorithm.cpp 13 KB

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