You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

310 lines
12 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 "btDefaultCollisionConfiguration.h"
  14. #include "BulletCollision/CollisionDispatch/btConvexConvexAlgorithm.h"
  15. #include "BulletCollision/CollisionDispatch/btEmptyCollisionAlgorithm.h"
  16. #include "BulletCollision/CollisionDispatch/btConvexConcaveCollisionAlgorithm.h"
  17. #include "BulletCollision/CollisionDispatch/btCompoundCollisionAlgorithm.h"
  18. #include "BulletCollision/CollisionDispatch/btConvexPlaneCollisionAlgorithm.h"
  19. #include "BulletCollision/CollisionDispatch/btBoxBoxCollisionAlgorithm.h"
  20. #include "BulletCollision/CollisionDispatch/btSphereSphereCollisionAlgorithm.h"
  21. #ifdef USE_BUGGY_SPHERE_BOX_ALGORITHM
  22. #include "BulletCollision/CollisionDispatch/btSphereBoxCollisionAlgorithm.h"
  23. #endif //USE_BUGGY_SPHERE_BOX_ALGORITHM
  24. #include "BulletCollision/CollisionDispatch/btSphereTriangleCollisionAlgorithm.h"
  25. #include "BulletCollision/NarrowPhaseCollision/btGjkEpaPenetrationDepthSolver.h"
  26. #include "BulletCollision/NarrowPhaseCollision/btMinkowskiPenetrationDepthSolver.h"
  27. #include "BulletCollision/NarrowPhaseCollision/btVoronoiSimplexSolver.h"
  28. #include "LinearMath/btStackAlloc.h"
  29. #include "LinearMath/btPoolAllocator.h"
  30. btDefaultCollisionConfiguration::btDefaultCollisionConfiguration(const btDefaultCollisionConstructionInfo& constructionInfo)
  31. //btDefaultCollisionConfiguration::btDefaultCollisionConfiguration(btStackAlloc* stackAlloc,btPoolAllocator* persistentManifoldPool,btPoolAllocator* collisionAlgorithmPool)
  32. {
  33. void* mem = btAlignedAlloc(sizeof(btVoronoiSimplexSolver),16);
  34. m_simplexSolver = new (mem)btVoronoiSimplexSolver();
  35. if (constructionInfo.m_useEpaPenetrationAlgorithm)
  36. {
  37. mem = btAlignedAlloc(sizeof(btGjkEpaPenetrationDepthSolver),16);
  38. m_pdSolver = new (mem)btGjkEpaPenetrationDepthSolver;
  39. }else
  40. {
  41. mem = btAlignedAlloc(sizeof(btMinkowskiPenetrationDepthSolver),16);
  42. m_pdSolver = new (mem)btMinkowskiPenetrationDepthSolver;
  43. }
  44. //default CreationFunctions, filling the m_doubleDispatch table
  45. mem = btAlignedAlloc(sizeof(btConvexConvexAlgorithm::CreateFunc),16);
  46. m_convexConvexCreateFunc = new(mem) btConvexConvexAlgorithm::CreateFunc(m_simplexSolver,m_pdSolver);
  47. mem = btAlignedAlloc(sizeof(btConvexConcaveCollisionAlgorithm::CreateFunc),16);
  48. m_convexConcaveCreateFunc = new (mem)btConvexConcaveCollisionAlgorithm::CreateFunc;
  49. mem = btAlignedAlloc(sizeof(btConvexConcaveCollisionAlgorithm::CreateFunc),16);
  50. m_swappedConvexConcaveCreateFunc = new (mem)btConvexConcaveCollisionAlgorithm::SwappedCreateFunc;
  51. mem = btAlignedAlloc(sizeof(btCompoundCollisionAlgorithm::CreateFunc),16);
  52. m_compoundCreateFunc = new (mem)btCompoundCollisionAlgorithm::CreateFunc;
  53. mem = btAlignedAlloc(sizeof(btCompoundCollisionAlgorithm::SwappedCreateFunc),16);
  54. m_swappedCompoundCreateFunc = new (mem)btCompoundCollisionAlgorithm::SwappedCreateFunc;
  55. mem = btAlignedAlloc(sizeof(btEmptyAlgorithm::CreateFunc),16);
  56. m_emptyCreateFunc = new(mem) btEmptyAlgorithm::CreateFunc;
  57. mem = btAlignedAlloc(sizeof(btSphereSphereCollisionAlgorithm::CreateFunc),16);
  58. m_sphereSphereCF = new(mem) btSphereSphereCollisionAlgorithm::CreateFunc;
  59. #ifdef USE_BUGGY_SPHERE_BOX_ALGORITHM
  60. mem = btAlignedAlloc(sizeof(btSphereBoxCollisionAlgorithm::CreateFunc),16);
  61. m_sphereBoxCF = new(mem) btSphereBoxCollisionAlgorithm::CreateFunc;
  62. mem = btAlignedAlloc(sizeof(btSphereBoxCollisionAlgorithm::CreateFunc),16);
  63. m_boxSphereCF = new (mem)btSphereBoxCollisionAlgorithm::CreateFunc;
  64. m_boxSphereCF->m_swapped = true;
  65. #endif //USE_BUGGY_SPHERE_BOX_ALGORITHM
  66. mem = btAlignedAlloc(sizeof(btSphereTriangleCollisionAlgorithm::CreateFunc),16);
  67. m_sphereTriangleCF = new (mem)btSphereTriangleCollisionAlgorithm::CreateFunc;
  68. mem = btAlignedAlloc(sizeof(btSphereTriangleCollisionAlgorithm::CreateFunc),16);
  69. m_triangleSphereCF = new (mem)btSphereTriangleCollisionAlgorithm::CreateFunc;
  70. m_triangleSphereCF->m_swapped = true;
  71. mem = btAlignedAlloc(sizeof(btBoxBoxCollisionAlgorithm::CreateFunc),16);
  72. m_boxBoxCF = new(mem)btBoxBoxCollisionAlgorithm::CreateFunc;
  73. //convex versus plane
  74. mem = btAlignedAlloc (sizeof(btConvexPlaneCollisionAlgorithm::CreateFunc),16);
  75. m_convexPlaneCF = new (mem) btConvexPlaneCollisionAlgorithm::CreateFunc;
  76. mem = btAlignedAlloc (sizeof(btConvexPlaneCollisionAlgorithm::CreateFunc),16);
  77. m_planeConvexCF = new (mem) btConvexPlaneCollisionAlgorithm::CreateFunc;
  78. m_planeConvexCF->m_swapped = true;
  79. ///calculate maximum element size, big enough to fit any collision algorithm in the memory pool
  80. int maxSize = sizeof(btConvexConvexAlgorithm);
  81. int maxSize2 = sizeof(btConvexConcaveCollisionAlgorithm);
  82. int maxSize3 = sizeof(btCompoundCollisionAlgorithm);
  83. int sl = sizeof(btConvexSeparatingDistanceUtil);
  84. sl = sizeof(btGjkPairDetector);
  85. int collisionAlgorithmMaxElementSize = btMax(maxSize,constructionInfo.m_customCollisionAlgorithmMaxElementSize);
  86. collisionAlgorithmMaxElementSize = btMax(collisionAlgorithmMaxElementSize,maxSize2);
  87. collisionAlgorithmMaxElementSize = btMax(collisionAlgorithmMaxElementSize,maxSize3);
  88. if (constructionInfo.m_stackAlloc)
  89. {
  90. m_ownsStackAllocator = false;
  91. this->m_stackAlloc = constructionInfo.m_stackAlloc;
  92. } else
  93. {
  94. m_ownsStackAllocator = true;
  95. void* mem = btAlignedAlloc(sizeof(btStackAlloc),16);
  96. m_stackAlloc = new(mem)btStackAlloc(constructionInfo.m_defaultStackAllocatorSize);
  97. }
  98. if (constructionInfo.m_persistentManifoldPool)
  99. {
  100. m_ownsPersistentManifoldPool = false;
  101. m_persistentManifoldPool = constructionInfo.m_persistentManifoldPool;
  102. } else
  103. {
  104. m_ownsPersistentManifoldPool = true;
  105. void* mem = btAlignedAlloc(sizeof(btPoolAllocator),16);
  106. m_persistentManifoldPool = new (mem) btPoolAllocator(sizeof(btPersistentManifold),constructionInfo.m_defaultMaxPersistentManifoldPoolSize);
  107. }
  108. if (constructionInfo.m_collisionAlgorithmPool)
  109. {
  110. m_ownsCollisionAlgorithmPool = false;
  111. m_collisionAlgorithmPool = constructionInfo.m_collisionAlgorithmPool;
  112. } else
  113. {
  114. m_ownsCollisionAlgorithmPool = true;
  115. void* mem = btAlignedAlloc(sizeof(btPoolAllocator),16);
  116. m_collisionAlgorithmPool = new(mem) btPoolAllocator(collisionAlgorithmMaxElementSize,constructionInfo.m_defaultMaxCollisionAlgorithmPoolSize);
  117. }
  118. }
  119. btDefaultCollisionConfiguration::~btDefaultCollisionConfiguration()
  120. {
  121. if (m_ownsStackAllocator)
  122. {
  123. m_stackAlloc->destroy();
  124. m_stackAlloc->~btStackAlloc();
  125. btAlignedFree(m_stackAlloc);
  126. }
  127. if (m_ownsCollisionAlgorithmPool)
  128. {
  129. m_collisionAlgorithmPool->~btPoolAllocator();
  130. btAlignedFree(m_collisionAlgorithmPool);
  131. }
  132. if (m_ownsPersistentManifoldPool)
  133. {
  134. m_persistentManifoldPool->~btPoolAllocator();
  135. btAlignedFree(m_persistentManifoldPool);
  136. }
  137. m_convexConvexCreateFunc->~btCollisionAlgorithmCreateFunc();
  138. btAlignedFree( m_convexConvexCreateFunc);
  139. m_convexConcaveCreateFunc->~btCollisionAlgorithmCreateFunc();
  140. btAlignedFree( m_convexConcaveCreateFunc);
  141. m_swappedConvexConcaveCreateFunc->~btCollisionAlgorithmCreateFunc();
  142. btAlignedFree( m_swappedConvexConcaveCreateFunc);
  143. m_compoundCreateFunc->~btCollisionAlgorithmCreateFunc();
  144. btAlignedFree( m_compoundCreateFunc);
  145. m_swappedCompoundCreateFunc->~btCollisionAlgorithmCreateFunc();
  146. btAlignedFree( m_swappedCompoundCreateFunc);
  147. m_emptyCreateFunc->~btCollisionAlgorithmCreateFunc();
  148. btAlignedFree( m_emptyCreateFunc);
  149. m_sphereSphereCF->~btCollisionAlgorithmCreateFunc();
  150. btAlignedFree( m_sphereSphereCF);
  151. #ifdef USE_BUGGY_SPHERE_BOX_ALGORITHM
  152. m_sphereBoxCF->~btCollisionAlgorithmCreateFunc();
  153. btAlignedFree( m_sphereBoxCF);
  154. m_boxSphereCF->~btCollisionAlgorithmCreateFunc();
  155. btAlignedFree( m_boxSphereCF);
  156. #endif //USE_BUGGY_SPHERE_BOX_ALGORITHM
  157. m_sphereTriangleCF->~btCollisionAlgorithmCreateFunc();
  158. btAlignedFree( m_sphereTriangleCF);
  159. m_triangleSphereCF->~btCollisionAlgorithmCreateFunc();
  160. btAlignedFree( m_triangleSphereCF);
  161. m_boxBoxCF->~btCollisionAlgorithmCreateFunc();
  162. btAlignedFree( m_boxBoxCF);
  163. m_convexPlaneCF->~btCollisionAlgorithmCreateFunc();
  164. btAlignedFree( m_convexPlaneCF);
  165. m_planeConvexCF->~btCollisionAlgorithmCreateFunc();
  166. btAlignedFree( m_planeConvexCF);
  167. m_simplexSolver->~btVoronoiSimplexSolver();
  168. btAlignedFree(m_simplexSolver);
  169. m_pdSolver->~btConvexPenetrationDepthSolver();
  170. btAlignedFree(m_pdSolver);
  171. }
  172. btCollisionAlgorithmCreateFunc* btDefaultCollisionConfiguration::getCollisionAlgorithmCreateFunc(int proxyType0,int proxyType1)
  173. {
  174. if ((proxyType0 == SPHERE_SHAPE_PROXYTYPE) && (proxyType1==SPHERE_SHAPE_PROXYTYPE))
  175. {
  176. return m_sphereSphereCF;
  177. }
  178. #ifdef USE_BUGGY_SPHERE_BOX_ALGORITHM
  179. if ((proxyType0 == SPHERE_SHAPE_PROXYTYPE) && (proxyType1==BOX_SHAPE_PROXYTYPE))
  180. {
  181. return m_sphereBoxCF;
  182. }
  183. if ((proxyType0 == BOX_SHAPE_PROXYTYPE ) && (proxyType1==SPHERE_SHAPE_PROXYTYPE))
  184. {
  185. return m_boxSphereCF;
  186. }
  187. #endif //USE_BUGGY_SPHERE_BOX_ALGORITHM
  188. if ((proxyType0 == SPHERE_SHAPE_PROXYTYPE ) && (proxyType1==TRIANGLE_SHAPE_PROXYTYPE))
  189. {
  190. return m_sphereTriangleCF;
  191. }
  192. if ((proxyType0 == TRIANGLE_SHAPE_PROXYTYPE ) && (proxyType1==SPHERE_SHAPE_PROXYTYPE))
  193. {
  194. return m_triangleSphereCF;
  195. }
  196. if ((proxyType0 == BOX_SHAPE_PROXYTYPE) && (proxyType1 == BOX_SHAPE_PROXYTYPE))
  197. {
  198. return m_boxBoxCF;
  199. }
  200. if (btBroadphaseProxy::isConvex(proxyType0) && (proxyType1 == STATIC_PLANE_PROXYTYPE))
  201. {
  202. return m_convexPlaneCF;
  203. }
  204. if (btBroadphaseProxy::isConvex(proxyType1) && (proxyType0 == STATIC_PLANE_PROXYTYPE))
  205. {
  206. return m_planeConvexCF;
  207. }
  208. if (btBroadphaseProxy::isConvex(proxyType0) && btBroadphaseProxy::isConvex(proxyType1))
  209. {
  210. return m_convexConvexCreateFunc;
  211. }
  212. if (btBroadphaseProxy::isConvex(proxyType0) && btBroadphaseProxy::isConcave(proxyType1))
  213. {
  214. return m_convexConcaveCreateFunc;
  215. }
  216. if (btBroadphaseProxy::isConvex(proxyType1) && btBroadphaseProxy::isConcave(proxyType0))
  217. {
  218. return m_swappedConvexConcaveCreateFunc;
  219. }
  220. if (btBroadphaseProxy::isCompound(proxyType0))
  221. {
  222. return m_compoundCreateFunc;
  223. } else
  224. {
  225. if (btBroadphaseProxy::isCompound(proxyType1))
  226. {
  227. return m_swappedCompoundCreateFunc;
  228. }
  229. }
  230. //failed to find an algorithm
  231. return m_emptyCreateFunc;
  232. }
  233. void btDefaultCollisionConfiguration::setConvexConvexMultipointIterations(int numPerturbationIterations, int minimumPointsPerturbationThreshold)
  234. {
  235. btConvexConvexAlgorithm::CreateFunc* convexConvex = (btConvexConvexAlgorithm::CreateFunc*) m_convexConvexCreateFunc;
  236. convexConvex->m_numPerturbationIterations = numPerturbationIterations;
  237. convexConvex->m_minimumPointsPerturbationThreshold = minimumPointsPerturbationThreshold;
  238. }
  239. void btDefaultCollisionConfiguration::setPlaneConvexMultipointIterations(int numPerturbationIterations, int minimumPointsPerturbationThreshold)
  240. {
  241. btConvexPlaneCollisionAlgorithm::CreateFunc* cpCF = (btConvexPlaneCollisionAlgorithm::CreateFunc*)m_convexPlaneCF;
  242. cpCF->m_numPerturbationIterations = numPerturbationIterations;
  243. cpCF->m_minimumPointsPerturbationThreshold = minimumPointsPerturbationThreshold;
  244. btConvexPlaneCollisionAlgorithm::CreateFunc* pcCF = (btConvexPlaneCollisionAlgorithm::CreateFunc*)m_planeConvexCF;
  245. pcCF->m_numPerturbationIterations = numPerturbationIterations;
  246. pcCF->m_minimumPointsPerturbationThreshold = minimumPointsPerturbationThreshold;
  247. }