選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

311 行
9.2 KiB

  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 "btCollisionDispatcher.h"
  14. #include "BulletCollision/BroadphaseCollision/btCollisionAlgorithm.h"
  15. #include "BulletCollision/CollisionShapes/btCollisionShape.h"
  16. #include "BulletCollision/CollisionDispatch/btCollisionObject.h"
  17. #include "BulletCollision/BroadphaseCollision/btOverlappingPairCache.h"
  18. #include "LinearMath/btPoolAllocator.h"
  19. #include "BulletCollision/CollisionDispatch/btCollisionConfiguration.h"
  20. int gNumManifold = 0;
  21. #ifdef BT_DEBUG
  22. #include <stdio.h>
  23. #endif
  24. btCollisionDispatcher::btCollisionDispatcher (btCollisionConfiguration* collisionConfiguration):
  25. m_dispatcherFlags(btCollisionDispatcher::CD_USE_RELATIVE_CONTACT_BREAKING_THRESHOLD),
  26. m_collisionConfiguration(collisionConfiguration)
  27. {
  28. int i;
  29. setNearCallback(defaultNearCallback);
  30. m_collisionAlgorithmPoolAllocator = collisionConfiguration->getCollisionAlgorithmPool();
  31. m_persistentManifoldPoolAllocator = collisionConfiguration->getPersistentManifoldPool();
  32. for (i=0;i<MAX_BROADPHASE_COLLISION_TYPES;i++)
  33. {
  34. for (int j=0;j<MAX_BROADPHASE_COLLISION_TYPES;j++)
  35. {
  36. m_doubleDispatch[i][j] = m_collisionConfiguration->getCollisionAlgorithmCreateFunc(i,j);
  37. btAssert(m_doubleDispatch[i][j]);
  38. }
  39. }
  40. }
  41. void btCollisionDispatcher::registerCollisionCreateFunc(int proxyType0, int proxyType1, btCollisionAlgorithmCreateFunc *createFunc)
  42. {
  43. m_doubleDispatch[proxyType0][proxyType1] = createFunc;
  44. }
  45. btCollisionDispatcher::~btCollisionDispatcher()
  46. {
  47. }
  48. btPersistentManifold* btCollisionDispatcher::getNewManifold(void* b0,void* b1)
  49. {
  50. gNumManifold++;
  51. //btAssert(gNumManifold < 65535);
  52. btCollisionObject* body0 = (btCollisionObject*)b0;
  53. btCollisionObject* body1 = (btCollisionObject*)b1;
  54. //optional relative contact breaking threshold, turned on by default (use setDispatcherFlags to switch off feature for improved performance)
  55. btScalar contactBreakingThreshold = (m_dispatcherFlags & btCollisionDispatcher::CD_USE_RELATIVE_CONTACT_BREAKING_THRESHOLD) ?
  56. btMin(body0->getCollisionShape()->getContactBreakingThreshold(gContactBreakingThreshold) , body1->getCollisionShape()->getContactBreakingThreshold(gContactBreakingThreshold))
  57. : gContactBreakingThreshold ;
  58. btScalar contactProcessingThreshold = btMin(body0->getContactProcessingThreshold(),body1->getContactProcessingThreshold());
  59. void* mem = 0;
  60. if (m_persistentManifoldPoolAllocator->getFreeCount())
  61. {
  62. mem = m_persistentManifoldPoolAllocator->allocate(sizeof(btPersistentManifold));
  63. } else
  64. {
  65. //we got a pool memory overflow, by default we fallback to dynamically allocate memory. If we require a contiguous contact pool then assert.
  66. if ((m_dispatcherFlags&CD_DISABLE_CONTACTPOOL_DYNAMIC_ALLOCATION)==0)
  67. {
  68. mem = btAlignedAlloc(sizeof(btPersistentManifold),16);
  69. } else
  70. {
  71. btAssert(0);
  72. //make sure to increase the m_defaultMaxPersistentManifoldPoolSize in the btDefaultCollisionConstructionInfo/btDefaultCollisionConfiguration
  73. return 0;
  74. }
  75. }
  76. btPersistentManifold* manifold = new(mem) btPersistentManifold (body0,body1,0,contactBreakingThreshold,contactProcessingThreshold);
  77. manifold->m_index1a = m_manifoldsPtr.size();
  78. m_manifoldsPtr.push_back(manifold);
  79. return manifold;
  80. }
  81. void btCollisionDispatcher::clearManifold(btPersistentManifold* manifold)
  82. {
  83. manifold->clearManifold();
  84. }
  85. void btCollisionDispatcher::releaseManifold(btPersistentManifold* manifold)
  86. {
  87. gNumManifold--;
  88. //printf("releaseManifold: gNumManifold %d\n",gNumManifold);
  89. clearManifold(manifold);
  90. int findIndex = manifold->m_index1a;
  91. btAssert(findIndex < m_manifoldsPtr.size());
  92. m_manifoldsPtr.swap(findIndex,m_manifoldsPtr.size()-1);
  93. m_manifoldsPtr[findIndex]->m_index1a = findIndex;
  94. m_manifoldsPtr.pop_back();
  95. manifold->~btPersistentManifold();
  96. if (m_persistentManifoldPoolAllocator->validPtr(manifold))
  97. {
  98. m_persistentManifoldPoolAllocator->freeMemory(manifold);
  99. } else
  100. {
  101. btAlignedFree(manifold);
  102. }
  103. }
  104. btCollisionAlgorithm* btCollisionDispatcher::findAlgorithm(btCollisionObject* body0,btCollisionObject* body1,btPersistentManifold* sharedManifold)
  105. {
  106. btCollisionAlgorithmConstructionInfo ci;
  107. ci.m_dispatcher1 = this;
  108. ci.m_manifold = sharedManifold;
  109. btCollisionAlgorithm* algo = m_doubleDispatch[body0->getCollisionShape()->getShapeType()][body1->getCollisionShape()->getShapeType()]->CreateCollisionAlgorithm(ci,body0,body1);
  110. return algo;
  111. }
  112. bool btCollisionDispatcher::needsResponse(btCollisionObject* body0,btCollisionObject* body1)
  113. {
  114. //here you can do filtering
  115. bool hasResponse =
  116. (body0->hasContactResponse() && body1->hasContactResponse());
  117. //no response between two static/kinematic bodies:
  118. hasResponse = hasResponse &&
  119. ((!body0->isStaticOrKinematicObject()) ||(! body1->isStaticOrKinematicObject()));
  120. return hasResponse;
  121. }
  122. bool btCollisionDispatcher::needsCollision(btCollisionObject* body0,btCollisionObject* body1)
  123. {
  124. btAssert(body0);
  125. btAssert(body1);
  126. bool needsCollision = true;
  127. #ifdef BT_DEBUG
  128. if (!(m_dispatcherFlags & btCollisionDispatcher::CD_STATIC_STATIC_REPORTED))
  129. {
  130. //broadphase filtering already deals with this
  131. if (body0->isStaticOrKinematicObject() && body1->isStaticOrKinematicObject())
  132. {
  133. m_dispatcherFlags |= btCollisionDispatcher::CD_STATIC_STATIC_REPORTED;
  134. printf("warning btCollisionDispatcher::needsCollision: static-static collision!\n");
  135. }
  136. }
  137. #endif //BT_DEBUG
  138. if ((!body0->isActive()) && (!body1->isActive()))
  139. needsCollision = false;
  140. else if (!body0->checkCollideWith(body1))
  141. needsCollision = false;
  142. return needsCollision ;
  143. }
  144. ///interface for iterating all overlapping collision pairs, no matter how those pairs are stored (array, set, map etc)
  145. ///this is useful for the collision dispatcher.
  146. class btCollisionPairCallback : public btOverlapCallback
  147. {
  148. const btDispatcherInfo& m_dispatchInfo;
  149. btCollisionDispatcher* m_dispatcher;
  150. public:
  151. btCollisionPairCallback(const btDispatcherInfo& dispatchInfo,btCollisionDispatcher* dispatcher)
  152. :m_dispatchInfo(dispatchInfo),
  153. m_dispatcher(dispatcher)
  154. {
  155. }
  156. /*btCollisionPairCallback& operator=(btCollisionPairCallback& other)
  157. {
  158. m_dispatchInfo = other.m_dispatchInfo;
  159. m_dispatcher = other.m_dispatcher;
  160. return *this;
  161. }
  162. */
  163. virtual ~btCollisionPairCallback() {}
  164. virtual bool processOverlap(btBroadphasePair& pair)
  165. {
  166. (*m_dispatcher->getNearCallback())(pair,*m_dispatcher,m_dispatchInfo);
  167. return false;
  168. }
  169. };
  170. void btCollisionDispatcher::dispatchAllCollisionPairs(btOverlappingPairCache* pairCache,const btDispatcherInfo& dispatchInfo,btDispatcher* dispatcher)
  171. {
  172. //m_blockedForChanges = true;
  173. btCollisionPairCallback collisionCallback(dispatchInfo,this);
  174. pairCache->processAllOverlappingPairs(&collisionCallback,dispatcher);
  175. //m_blockedForChanges = false;
  176. }
  177. //by default, Bullet will use this near callback
  178. void btCollisionDispatcher::defaultNearCallback(btBroadphasePair& collisionPair, btCollisionDispatcher& dispatcher, const btDispatcherInfo& dispatchInfo)
  179. {
  180. btCollisionObject* colObj0 = (btCollisionObject*)collisionPair.m_pProxy0->m_clientObject;
  181. btCollisionObject* colObj1 = (btCollisionObject*)collisionPair.m_pProxy1->m_clientObject;
  182. if (dispatcher.needsCollision(colObj0,colObj1))
  183. {
  184. //dispatcher will keep algorithms persistent in the collision pair
  185. if (!collisionPair.m_algorithm)
  186. {
  187. collisionPair.m_algorithm = dispatcher.findAlgorithm(colObj0,colObj1);
  188. }
  189. if (collisionPair.m_algorithm)
  190. {
  191. btManifoldResult contactPointResult(colObj0,colObj1);
  192. if (dispatchInfo.m_dispatchFunc == btDispatcherInfo::DISPATCH_DISCRETE)
  193. {
  194. //discrete collision detection query
  195. collisionPair.m_algorithm->processCollision(colObj0,colObj1,dispatchInfo,&contactPointResult);
  196. } else
  197. {
  198. //continuous collision detection query, time of impact (toi)
  199. btScalar toi = collisionPair.m_algorithm->calculateTimeOfImpact(colObj0,colObj1,dispatchInfo,&contactPointResult);
  200. if (dispatchInfo.m_timeOfImpact > toi)
  201. dispatchInfo.m_timeOfImpact = toi;
  202. }
  203. }
  204. }
  205. }
  206. void* btCollisionDispatcher::allocateCollisionAlgorithm(int size)
  207. {
  208. if (m_collisionAlgorithmPoolAllocator->getFreeCount())
  209. {
  210. return m_collisionAlgorithmPoolAllocator->allocate(size);
  211. }
  212. //warn user for overflow?
  213. return btAlignedAlloc(static_cast<size_t>(size), 16);
  214. }
  215. void btCollisionDispatcher::freeCollisionAlgorithm(void* ptr)
  216. {
  217. if (m_collisionAlgorithmPoolAllocator->validPtr(ptr))
  218. {
  219. m_collisionAlgorithmPoolAllocator->freeMemory(ptr);
  220. } else
  221. {
  222. btAlignedFree(ptr);
  223. }
  224. }