btCompoundCollisionAlgorithm.cpp 14 KB

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