btSoftBodyConcaveCollisionAlgorithm.h 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  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. #ifndef BT_SOFT_BODY_CONCAVE_COLLISION_ALGORITHM_H
  14. #define BT_SOFT_BODY_CONCAVE_COLLISION_ALGORITHM_H
  15. #include "BulletCollision/BroadphaseCollision/btCollisionAlgorithm.h"
  16. #include "BulletCollision/BroadphaseCollision/btDispatcher.h"
  17. #include "BulletCollision/BroadphaseCollision/btBroadphaseInterface.h"
  18. #include "BulletCollision/CollisionShapes/btTriangleCallback.h"
  19. #include "BulletCollision/NarrowPhaseCollision/btPersistentManifold.h"
  20. class btDispatcher;
  21. #include "BulletCollision/BroadphaseCollision/btBroadphaseProxy.h"
  22. #include "BulletCollision/CollisionDispatch/btCollisionCreateFunc.h"
  23. class btSoftBody;
  24. class btCollisionShape;
  25. #include "LinearMath/btHashMap.h"
  26. #include "BulletCollision/BroadphaseCollision/btQuantizedBvh.h" //for definition of MAX_NUM_PARTS_IN_BITS
  27. struct btTriIndex
  28. {
  29. int m_PartIdTriangleIndex;
  30. class btCollisionShape* m_childShape;
  31. btTriIndex(int partId,int triangleIndex,btCollisionShape* shape)
  32. {
  33. m_PartIdTriangleIndex = (partId<<(31-MAX_NUM_PARTS_IN_BITS)) | triangleIndex;
  34. m_childShape = shape;
  35. }
  36. int getTriangleIndex() const
  37. {
  38. // Get only the lower bits where the triangle index is stored
  39. unsigned int x = 0;
  40. unsigned int y = (~(x&0))<<(31-MAX_NUM_PARTS_IN_BITS);
  41. return (m_PartIdTriangleIndex&~(y));
  42. }
  43. int getPartId() const
  44. {
  45. // Get only the highest bits where the part index is stored
  46. return (m_PartIdTriangleIndex>>(31-MAX_NUM_PARTS_IN_BITS));
  47. }
  48. int getUid() const
  49. {
  50. return m_PartIdTriangleIndex;
  51. }
  52. };
  53. ///For each triangle in the concave mesh that overlaps with the AABB of a soft body (m_softBody), processTriangle is called.
  54. class btSoftBodyTriangleCallback : public btTriangleCallback
  55. {
  56. btSoftBody* m_softBody;
  57. const btCollisionObject* m_triBody;
  58. btVector3 m_aabbMin;
  59. btVector3 m_aabbMax ;
  60. btManifoldResult* m_resultOut;
  61. btDispatcher* m_dispatcher;
  62. const btDispatcherInfo* m_dispatchInfoPtr;
  63. btScalar m_collisionMarginTriangle;
  64. btHashMap<btHashKey<btTriIndex>,btTriIndex> m_shapeCache;
  65. public:
  66. int m_triangleCount;
  67. // btPersistentManifold* m_manifoldPtr;
  68. btSoftBodyTriangleCallback(btDispatcher* dispatcher,const btCollisionObjectWrapper* body0Wrap,const btCollisionObjectWrapper* body1Wrap,bool isSwapped);
  69. void setTimeStepAndCounters(btScalar collisionMarginTriangle,const btCollisionObjectWrapper* triObjWrap,const btDispatcherInfo& dispatchInfo,btManifoldResult* resultOut);
  70. virtual ~btSoftBodyTriangleCallback();
  71. virtual void processTriangle(btVector3* triangle, int partId, int triangleIndex);
  72. void clearCache();
  73. SIMD_FORCE_INLINE const btVector3& getAabbMin() const
  74. {
  75. return m_aabbMin;
  76. }
  77. SIMD_FORCE_INLINE const btVector3& getAabbMax() const
  78. {
  79. return m_aabbMax;
  80. }
  81. };
  82. /// btSoftBodyConcaveCollisionAlgorithm supports collision between soft body shapes and (concave) trianges meshes.
  83. class btSoftBodyConcaveCollisionAlgorithm : public btCollisionAlgorithm
  84. {
  85. bool m_isSwapped;
  86. btSoftBodyTriangleCallback m_btSoftBodyTriangleCallback;
  87. public:
  88. btSoftBodyConcaveCollisionAlgorithm( const btCollisionAlgorithmConstructionInfo& ci,const btCollisionObjectWrapper* body0Wrap,const btCollisionObjectWrapper* body1Wrap,bool isSwapped);
  89. virtual ~btSoftBodyConcaveCollisionAlgorithm();
  90. virtual void processCollision (const btCollisionObjectWrapper* body0Wrap,const btCollisionObjectWrapper* body1Wrap,const btDispatcherInfo& dispatchInfo,btManifoldResult* resultOut);
  91. btScalar calculateTimeOfImpact(btCollisionObject* body0,btCollisionObject* body1,const btDispatcherInfo& dispatchInfo,btManifoldResult* resultOut);
  92. virtual void getAllContactManifolds(btManifoldArray& manifoldArray)
  93. {
  94. //we don't add any manifolds
  95. }
  96. void clearCache();
  97. struct CreateFunc :public btCollisionAlgorithmCreateFunc
  98. {
  99. virtual btCollisionAlgorithm* CreateCollisionAlgorithm(btCollisionAlgorithmConstructionInfo& ci, const btCollisionObjectWrapper* body0Wrap,const btCollisionObjectWrapper* body1Wrap)
  100. {
  101. void* mem = ci.m_dispatcher1->allocateCollisionAlgorithm(sizeof(btSoftBodyConcaveCollisionAlgorithm));
  102. return new(mem) btSoftBodyConcaveCollisionAlgorithm(ci,body0Wrap,body1Wrap,false);
  103. }
  104. };
  105. struct SwappedCreateFunc :public btCollisionAlgorithmCreateFunc
  106. {
  107. virtual btCollisionAlgorithm* CreateCollisionAlgorithm(btCollisionAlgorithmConstructionInfo& ci, const btCollisionObjectWrapper* body0Wrap,const btCollisionObjectWrapper* body1Wrap)
  108. {
  109. void* mem = ci.m_dispatcher1->allocateCollisionAlgorithm(sizeof(btSoftBodyConcaveCollisionAlgorithm));
  110. return new(mem) btSoftBodyConcaveCollisionAlgorithm(ci,body0Wrap,body1Wrap,true);
  111. }
  112. };
  113. };
  114. #endif //BT_SOFT_BODY_CONCAVE_COLLISION_ALGORITHM_H