btSoftBodyRigidBodyCollisionConfiguration.cpp 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  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 "btSoftBodyRigidBodyCollisionConfiguration.h"
  14. #include "btSoftRigidCollisionAlgorithm.h"
  15. #include "btSoftBodyConcaveCollisionAlgorithm.h"
  16. #include "btSoftSoftCollisionAlgorithm.h"
  17. #include "LinearMath/btPoolAllocator.h"
  18. #define ENABLE_SOFTBODY_CONCAVE_COLLISIONS 1
  19. btSoftBodyRigidBodyCollisionConfiguration::btSoftBodyRigidBodyCollisionConfiguration(const btDefaultCollisionConstructionInfo& constructionInfo)
  20. :btDefaultCollisionConfiguration(constructionInfo)
  21. {
  22. void* mem;
  23. mem = btAlignedAlloc(sizeof(btSoftSoftCollisionAlgorithm::CreateFunc),16);
  24. m_softSoftCreateFunc = new(mem) btSoftSoftCollisionAlgorithm::CreateFunc;
  25. mem = btAlignedAlloc(sizeof(btSoftRigidCollisionAlgorithm::CreateFunc),16);
  26. m_softRigidConvexCreateFunc = new(mem) btSoftRigidCollisionAlgorithm::CreateFunc;
  27. mem = btAlignedAlloc(sizeof(btSoftRigidCollisionAlgorithm::CreateFunc),16);
  28. m_swappedSoftRigidConvexCreateFunc = new(mem) btSoftRigidCollisionAlgorithm::CreateFunc;
  29. m_swappedSoftRigidConvexCreateFunc->m_swapped=true;
  30. #ifdef ENABLE_SOFTBODY_CONCAVE_COLLISIONS
  31. mem = btAlignedAlloc(sizeof(btSoftBodyConcaveCollisionAlgorithm::CreateFunc),16);
  32. m_softRigidConcaveCreateFunc = new(mem) btSoftBodyConcaveCollisionAlgorithm::CreateFunc;
  33. mem = btAlignedAlloc(sizeof(btSoftBodyConcaveCollisionAlgorithm::CreateFunc),16);
  34. m_swappedSoftRigidConcaveCreateFunc = new(mem) btSoftBodyConcaveCollisionAlgorithm::SwappedCreateFunc;
  35. m_swappedSoftRigidConcaveCreateFunc->m_swapped=true;
  36. #endif
  37. //replace pool by a new one, with potential larger size
  38. if (m_ownsCollisionAlgorithmPool && m_collisionAlgorithmPool)
  39. {
  40. int curElemSize = m_collisionAlgorithmPool->getElementSize();
  41. ///calculate maximum element size, big enough to fit any collision algorithm in the memory pool
  42. int maxSize0 = sizeof(btSoftSoftCollisionAlgorithm);
  43. int maxSize1 = sizeof(btSoftRigidCollisionAlgorithm);
  44. int maxSize2 = sizeof(btSoftBodyConcaveCollisionAlgorithm);
  45. int collisionAlgorithmMaxElementSize = btMax(maxSize0,maxSize1);
  46. collisionAlgorithmMaxElementSize = btMax(collisionAlgorithmMaxElementSize,maxSize2);
  47. if (collisionAlgorithmMaxElementSize > curElemSize)
  48. {
  49. m_collisionAlgorithmPool->~btPoolAllocator();
  50. btAlignedFree(m_collisionAlgorithmPool);
  51. void* mem = btAlignedAlloc(sizeof(btPoolAllocator),16);
  52. m_collisionAlgorithmPool = new(mem) btPoolAllocator(collisionAlgorithmMaxElementSize,constructionInfo.m_defaultMaxCollisionAlgorithmPoolSize);
  53. }
  54. }
  55. }
  56. btSoftBodyRigidBodyCollisionConfiguration::~btSoftBodyRigidBodyCollisionConfiguration()
  57. {
  58. m_softSoftCreateFunc->~btCollisionAlgorithmCreateFunc();
  59. btAlignedFree( m_softSoftCreateFunc);
  60. m_softRigidConvexCreateFunc->~btCollisionAlgorithmCreateFunc();
  61. btAlignedFree( m_softRigidConvexCreateFunc);
  62. m_swappedSoftRigidConvexCreateFunc->~btCollisionAlgorithmCreateFunc();
  63. btAlignedFree( m_swappedSoftRigidConvexCreateFunc);
  64. #ifdef ENABLE_SOFTBODY_CONCAVE_COLLISIONS
  65. m_softRigidConcaveCreateFunc->~btCollisionAlgorithmCreateFunc();
  66. btAlignedFree( m_softRigidConcaveCreateFunc);
  67. m_swappedSoftRigidConcaveCreateFunc->~btCollisionAlgorithmCreateFunc();
  68. btAlignedFree( m_swappedSoftRigidConcaveCreateFunc);
  69. #endif
  70. }
  71. ///creation of soft-soft and soft-rigid, and otherwise fallback to base class implementation
  72. btCollisionAlgorithmCreateFunc* btSoftBodyRigidBodyCollisionConfiguration::getCollisionAlgorithmCreateFunc(int proxyType0,int proxyType1)
  73. {
  74. ///try to handle the softbody interactions first
  75. if ((proxyType0 == SOFTBODY_SHAPE_PROXYTYPE ) && (proxyType1==SOFTBODY_SHAPE_PROXYTYPE))
  76. {
  77. return m_softSoftCreateFunc;
  78. }
  79. ///softbody versus convex
  80. if (proxyType0 == SOFTBODY_SHAPE_PROXYTYPE && btBroadphaseProxy::isConvex(proxyType1))
  81. {
  82. return m_softRigidConvexCreateFunc;
  83. }
  84. ///convex versus soft body
  85. if (btBroadphaseProxy::isConvex(proxyType0) && proxyType1 == SOFTBODY_SHAPE_PROXYTYPE )
  86. {
  87. return m_swappedSoftRigidConvexCreateFunc;
  88. }
  89. #ifdef ENABLE_SOFTBODY_CONCAVE_COLLISIONS
  90. ///softbody versus convex
  91. if (proxyType0 == SOFTBODY_SHAPE_PROXYTYPE && btBroadphaseProxy::isConcave(proxyType1))
  92. {
  93. return m_softRigidConcaveCreateFunc;
  94. }
  95. ///convex versus soft body
  96. if (btBroadphaseProxy::isConcave(proxyType0) && proxyType1 == SOFTBODY_SHAPE_PROXYTYPE )
  97. {
  98. return m_swappedSoftRigidConcaveCreateFunc;
  99. }
  100. #endif
  101. ///fallback to the regular rigid collision shape
  102. return btDefaultCollisionConfiguration::getCollisionAlgorithmCreateFunc(proxyType0,proxyType1);
  103. }