btCompoundCollisionAlgorithm.cpp 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391
  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. btTransform preTransform = childTrans;
  115. if (this->m_compoundColObjWrap->m_preTransform)
  116. {
  117. preTransform = preTransform *(*(this->m_compoundColObjWrap->m_preTransform));
  118. }
  119. btCollisionObjectWrapper compoundWrap(this->m_compoundColObjWrap, childShape, m_compoundColObjWrap->getCollisionObject(), newChildWorldTrans, preTransform, -1, index);
  120. btCollisionAlgorithm* algo = 0;
  121. bool allocatedAlgorithm = false;
  122. if (m_resultOut->m_closestPointDistanceThreshold > 0)
  123. {
  124. algo = m_dispatcher->findAlgorithm(&compoundWrap, m_otherObjWrap, 0, BT_CLOSEST_POINT_ALGORITHMS);
  125. allocatedAlgorithm = true;
  126. }
  127. else
  128. {
  129. //the contactpoint is still projected back using the original inverted worldtrans
  130. if (!m_childCollisionAlgorithms[index])
  131. {
  132. m_childCollisionAlgorithms[index] = m_dispatcher->findAlgorithm(&compoundWrap, m_otherObjWrap, m_sharedManifold, BT_CONTACT_POINT_ALGORITHMS);
  133. }
  134. algo = m_childCollisionAlgorithms[index];
  135. }
  136. const btCollisionObjectWrapper* tmpWrap = 0;
  137. ///detect swapping case
  138. if (m_resultOut->getBody0Internal() == m_compoundColObjWrap->getCollisionObject())
  139. {
  140. tmpWrap = m_resultOut->getBody0Wrap();
  141. m_resultOut->setBody0Wrap(&compoundWrap);
  142. m_resultOut->setShapeIdentifiersA(-1, index);
  143. }
  144. else
  145. {
  146. tmpWrap = m_resultOut->getBody1Wrap();
  147. m_resultOut->setBody1Wrap(&compoundWrap);
  148. m_resultOut->setShapeIdentifiersB(-1, index);
  149. }
  150. algo->processCollision(&compoundWrap, m_otherObjWrap, m_dispatchInfo, m_resultOut);
  151. #if 0
  152. if (m_dispatchInfo.m_debugDraw && (m_dispatchInfo.m_debugDraw->getDebugMode() & btIDebugDraw::DBG_DrawAabb))
  153. {
  154. btVector3 worldAabbMin,worldAabbMax;
  155. m_dispatchInfo.m_debugDraw->drawAabb(aabbMin0,aabbMax0,btVector3(1,1,1));
  156. m_dispatchInfo.m_debugDraw->drawAabb(aabbMin1,aabbMax1,btVector3(1,1,1));
  157. }
  158. #endif
  159. if (m_resultOut->getBody0Internal() == m_compoundColObjWrap->getCollisionObject())
  160. {
  161. m_resultOut->setBody0Wrap(tmpWrap);
  162. }
  163. else
  164. {
  165. m_resultOut->setBody1Wrap(tmpWrap);
  166. }
  167. if (allocatedAlgorithm)
  168. {
  169. algo->~btCollisionAlgorithm();
  170. m_dispatcher->freeCollisionAlgorithm(algo);
  171. }
  172. }
  173. }
  174. void Process(const btDbvtNode* leaf)
  175. {
  176. int index = leaf->dataAsInt;
  177. const btCompoundShape* compoundShape = static_cast<const btCompoundShape*>(m_compoundColObjWrap->getCollisionShape());
  178. const btCollisionShape* childShape = compoundShape->getChildShape(index);
  179. #if 0
  180. if (m_dispatchInfo.m_debugDraw && (m_dispatchInfo.m_debugDraw->getDebugMode() & btIDebugDraw::DBG_DrawAabb))
  181. {
  182. btVector3 worldAabbMin,worldAabbMax;
  183. btTransform orgTrans = m_compoundColObjWrap->getWorldTransform();
  184. btTransformAabb(leaf->volume.Mins(),leaf->volume.Maxs(),0.,orgTrans,worldAabbMin,worldAabbMax);
  185. m_dispatchInfo.m_debugDraw->drawAabb(worldAabbMin,worldAabbMax,btVector3(1,0,0));
  186. }
  187. #endif
  188. ProcessChildShape(childShape, index);
  189. }
  190. };
  191. void btCompoundCollisionAlgorithm::processCollision(const btCollisionObjectWrapper* body0Wrap, const btCollisionObjectWrapper* body1Wrap, const btDispatcherInfo& dispatchInfo, btManifoldResult* resultOut)
  192. {
  193. const btCollisionObjectWrapper* colObjWrap = m_isSwapped ? body1Wrap : body0Wrap;
  194. const btCollisionObjectWrapper* otherObjWrap = m_isSwapped ? body0Wrap : body1Wrap;
  195. btAssert(colObjWrap->getCollisionShape()->isCompound());
  196. const btCompoundShape* compoundShape = static_cast<const btCompoundShape*>(colObjWrap->getCollisionShape());
  197. ///btCompoundShape might have changed:
  198. ////make sure the internal child collision algorithm caches are still valid
  199. if (compoundShape->getUpdateRevision() != m_compoundShapeRevision)
  200. {
  201. ///clear and update all
  202. removeChildAlgorithms();
  203. preallocateChildAlgorithms(body0Wrap, body1Wrap);
  204. m_compoundShapeRevision = compoundShape->getUpdateRevision();
  205. }
  206. if (m_childCollisionAlgorithms.size() == 0)
  207. return;
  208. const btDbvt* tree = compoundShape->getDynamicAabbTree();
  209. //use a dynamic aabb tree to cull potential child-overlaps
  210. btCompoundLeafCallback callback(colObjWrap, otherObjWrap, m_dispatcher, dispatchInfo, resultOut, &m_childCollisionAlgorithms[0], m_sharedManifold);
  211. ///we need to refresh all contact manifolds
  212. ///note that we should actually recursively traverse all children, btCompoundShape can nested more then 1 level deep
  213. ///so we should add a 'refreshManifolds' in the btCollisionAlgorithm
  214. {
  215. int i;
  216. manifoldArray.resize(0);
  217. for (i = 0; i < m_childCollisionAlgorithms.size(); i++)
  218. {
  219. if (m_childCollisionAlgorithms[i])
  220. {
  221. m_childCollisionAlgorithms[i]->getAllContactManifolds(manifoldArray);
  222. for (int m = 0; m < manifoldArray.size(); m++)
  223. {
  224. if (manifoldArray[m]->getNumContacts())
  225. {
  226. resultOut->setPersistentManifold(manifoldArray[m]);
  227. resultOut->refreshContactPoints();
  228. resultOut->setPersistentManifold(0); //??necessary?
  229. }
  230. }
  231. manifoldArray.resize(0);
  232. }
  233. }
  234. }
  235. if (tree)
  236. {
  237. btVector3 localAabbMin, localAabbMax;
  238. btTransform otherInCompoundSpace;
  239. otherInCompoundSpace = colObjWrap->getWorldTransform().inverse() * otherObjWrap->getWorldTransform();
  240. otherObjWrap->getCollisionShape()->getAabb(otherInCompoundSpace, localAabbMin, localAabbMax);
  241. btVector3 extraExtends(resultOut->m_closestPointDistanceThreshold, resultOut->m_closestPointDistanceThreshold, resultOut->m_closestPointDistanceThreshold);
  242. localAabbMin -= extraExtends;
  243. localAabbMax += extraExtends;
  244. const ATTRIBUTE_ALIGNED16(btDbvtVolume) bounds = btDbvtVolume::FromMM(localAabbMin, localAabbMax);
  245. //process all children, that overlap with the given AABB bounds
  246. tree->collideTVNoStackAlloc(tree->m_root, bounds, stack2, callback);
  247. }
  248. else
  249. {
  250. //iterate over all children, perform an AABB check inside ProcessChildShape
  251. int numChildren = m_childCollisionAlgorithms.size();
  252. int i;
  253. for (i = 0; i < numChildren; i++)
  254. {
  255. callback.ProcessChildShape(compoundShape->getChildShape(i), i);
  256. }
  257. }
  258. {
  259. //iterate over all children, perform an AABB check inside ProcessChildShape
  260. int numChildren = m_childCollisionAlgorithms.size();
  261. int i;
  262. manifoldArray.resize(0);
  263. const btCollisionShape* childShape = 0;
  264. btTransform orgTrans;
  265. btTransform newChildWorldTrans;
  266. btVector3 aabbMin0, aabbMax0, aabbMin1, aabbMax1;
  267. for (i = 0; i < numChildren; i++)
  268. {
  269. if (m_childCollisionAlgorithms[i])
  270. {
  271. childShape = compoundShape->getChildShape(i);
  272. //if not longer overlapping, remove the algorithm
  273. orgTrans = colObjWrap->getWorldTransform();
  274. const btTransform& childTrans = compoundShape->getChildTransform(i);
  275. newChildWorldTrans = orgTrans * childTrans;
  276. //perform an AABB check first
  277. childShape->getAabb(newChildWorldTrans, aabbMin0, aabbMax0);
  278. otherObjWrap->getCollisionShape()->getAabb(otherObjWrap->getWorldTransform(), aabbMin1, aabbMax1);
  279. if (!TestAabbAgainstAabb2(aabbMin0, aabbMax0, aabbMin1, aabbMax1))
  280. {
  281. m_childCollisionAlgorithms[i]->~btCollisionAlgorithm();
  282. m_dispatcher->freeCollisionAlgorithm(m_childCollisionAlgorithms[i]);
  283. m_childCollisionAlgorithms[i] = 0;
  284. }
  285. }
  286. }
  287. }
  288. }
  289. btScalar btCompoundCollisionAlgorithm::calculateTimeOfImpact(btCollisionObject* body0, btCollisionObject* body1, const btDispatcherInfo& dispatchInfo, btManifoldResult* resultOut)
  290. {
  291. btAssert(0);
  292. //needs to be fixed, using btCollisionObjectWrapper and NOT modifying internal data structures
  293. btCollisionObject* colObj = m_isSwapped ? body1 : body0;
  294. btCollisionObject* otherObj = m_isSwapped ? body0 : body1;
  295. btAssert(colObj->getCollisionShape()->isCompound());
  296. btCompoundShape* compoundShape = static_cast<btCompoundShape*>(colObj->getCollisionShape());
  297. //We will use the OptimizedBVH, AABB tree to cull potential child-overlaps
  298. //If both proxies are Compound, we will deal with that directly, by performing sequential/parallel tree traversals
  299. //given Proxy0 and Proxy1, if both have a tree, Tree0 and Tree1, this means:
  300. //determine overlapping nodes of Proxy1 using Proxy0 AABB against Tree1
  301. //then use each overlapping node AABB against Tree0
  302. //and vise versa.
  303. btScalar hitFraction = btScalar(1.);
  304. int numChildren = m_childCollisionAlgorithms.size();
  305. int i;
  306. btTransform orgTrans;
  307. btScalar frac;
  308. for (i = 0; i < numChildren; i++)
  309. {
  310. //btCollisionShape* childShape = compoundShape->getChildShape(i);
  311. //backup
  312. orgTrans = colObj->getWorldTransform();
  313. const btTransform& childTrans = compoundShape->getChildTransform(i);
  314. //btTransform newChildWorldTrans = orgTrans*childTrans ;
  315. colObj->setWorldTransform(orgTrans * childTrans);
  316. //btCollisionShape* tmpShape = colObj->getCollisionShape();
  317. //colObj->internalSetTemporaryCollisionShape( childShape );
  318. frac = m_childCollisionAlgorithms[i]->calculateTimeOfImpact(colObj, otherObj, dispatchInfo, resultOut);
  319. if (frac < hitFraction)
  320. {
  321. hitFraction = frac;
  322. }
  323. //revert back
  324. //colObj->internalSetTemporaryCollisionShape( tmpShape);
  325. colObj->setWorldTransform(orgTrans);
  326. }
  327. return hitFraction;
  328. }