| 1 | /* | 
|---|
| 2 | Bullet Continuous Collision Detection and Physics Library | 
|---|
| 3 | Copyright (c) 2003-2006 Erwin Coumans  http://continuousphysics.com/Bullet/ | 
|---|
| 4 |  | 
|---|
| 5 | This software is provided 'as-is', without any express or implied warranty. | 
|---|
| 6 | In no event will the authors be held liable for any damages arising from the use of this software. | 
|---|
| 7 | Permission is granted to anyone to use this software for any purpose,  | 
|---|
| 8 | including commercial applications, and to alter it and redistribute it freely,  | 
|---|
| 9 | subject to the following restrictions: | 
|---|
| 10 |  | 
|---|
| 11 | 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. | 
|---|
| 12 | 2. Altered source versions must be plainly marked as such, and must not be misrepresented as being the original software. | 
|---|
| 13 | 3. This notice may not be removed or altered from any source distribution. | 
|---|
| 14 | */ | 
|---|
| 15 |  | 
|---|
| 16 | #ifndef SOFT_BODY_CONCAVE_COLLISION_ALGORITHM_H | 
|---|
| 17 | #define SOFT_BODY_CONCAVE_COLLISION_ALGORITHM_H | 
|---|
| 18 |  | 
|---|
| 19 | #include "BulletCollision/BroadphaseCollision/btCollisionAlgorithm.h" | 
|---|
| 20 | #include "BulletCollision/BroadphaseCollision/btDispatcher.h" | 
|---|
| 21 | #include "BulletCollision/BroadphaseCollision/btBroadphaseInterface.h" | 
|---|
| 22 | #include "BulletCollision/CollisionShapes/btTriangleCallback.h" | 
|---|
| 23 | #include "BulletCollision/NarrowPhaseCollision/btPersistentManifold.h" | 
|---|
| 24 | class btDispatcher; | 
|---|
| 25 | #include "BulletCollision/BroadphaseCollision/btBroadphaseProxy.h" | 
|---|
| 26 | #include "BulletCollision/CollisionDispatch/btCollisionCreateFunc.h" | 
|---|
| 27 | class btSoftBody; | 
|---|
| 28 | class btCollisionShape; | 
|---|
| 29 |  | 
|---|
| 30 | #include "LinearMath/btHashMap.h" | 
|---|
| 31 |  | 
|---|
| 32 | #include "BulletCollision/BroadphaseCollision/btQuantizedBvh.h" //for definition of MAX_NUM_PARTS_IN_BITS | 
|---|
| 33 |  | 
|---|
| 34 | struct btTriIndex | 
|---|
| 35 | { | 
|---|
| 36 |         int m_PartIdTriangleIndex; | 
|---|
| 37 |         class btCollisionShape* m_childShape; | 
|---|
| 38 |          | 
|---|
| 39 |         btTriIndex(int partId,int triangleIndex,btCollisionShape* shape) | 
|---|
| 40 |         { | 
|---|
| 41 |                 m_PartIdTriangleIndex = (partId<<(31-MAX_NUM_PARTS_IN_BITS)) | triangleIndex; | 
|---|
| 42 |                 m_childShape = shape; | 
|---|
| 43 |         } | 
|---|
| 44 |  | 
|---|
| 45 |         int     getTriangleIndex() const | 
|---|
| 46 |         { | 
|---|
| 47 |                 // Get only the lower bits where the triangle index is stored | 
|---|
| 48 |                 return (m_PartIdTriangleIndex&~((~0)<<(31-MAX_NUM_PARTS_IN_BITS))); | 
|---|
| 49 |         } | 
|---|
| 50 |         int     getPartId() const | 
|---|
| 51 |         { | 
|---|
| 52 |                 // Get only the highest bits where the part index is stored | 
|---|
| 53 |                 return (m_PartIdTriangleIndex>>(31-MAX_NUM_PARTS_IN_BITS)); | 
|---|
| 54 |         } | 
|---|
| 55 |         int     getUid() const | 
|---|
| 56 |         { | 
|---|
| 57 |                 return m_PartIdTriangleIndex; | 
|---|
| 58 |         } | 
|---|
| 59 | }; | 
|---|
| 60 |  | 
|---|
| 61 |  | 
|---|
| 62 | ///For each triangle in the concave mesh that overlaps with the AABB of a soft body (m_softBody), processTriangle is called. | 
|---|
| 63 | class btSoftBodyTriangleCallback : public btTriangleCallback | 
|---|
| 64 | { | 
|---|
| 65 |         btSoftBody* m_softBody; | 
|---|
| 66 |         btCollisionObject* m_triBody; | 
|---|
| 67 |  | 
|---|
| 68 |         btVector3       m_aabbMin; | 
|---|
| 69 |         btVector3       m_aabbMax ; | 
|---|
| 70 |  | 
|---|
| 71 |         btManifoldResult* m_resultOut; | 
|---|
| 72 |  | 
|---|
| 73 |         btDispatcher*   m_dispatcher; | 
|---|
| 74 |         const btDispatcherInfo* m_dispatchInfoPtr; | 
|---|
| 75 |         btScalar m_collisionMarginTriangle; | 
|---|
| 76 |  | 
|---|
| 77 |         btHashMap<btHashKey<btTriIndex>,btTriIndex> m_shapeCache; | 
|---|
| 78 |          | 
|---|
| 79 | public: | 
|---|
| 80 | int     m_triangleCount; | 
|---|
| 81 |          | 
|---|
| 82 | //      btPersistentManifold*   m_manifoldPtr; | 
|---|
| 83 |  | 
|---|
| 84 |         btSoftBodyTriangleCallback(btDispatcher* dispatcher,btCollisionObject* body0,btCollisionObject* body1,bool isSwapped); | 
|---|
| 85 |  | 
|---|
| 86 |         void    setTimeStepAndCounters(btScalar collisionMarginTriangle,const btDispatcherInfo& dispatchInfo,btManifoldResult* resultOut); | 
|---|
| 87 |  | 
|---|
| 88 |         virtual ~btSoftBodyTriangleCallback(); | 
|---|
| 89 |  | 
|---|
| 90 |         virtual void processTriangle(btVector3* triangle, int partId, int triangleIndex); | 
|---|
| 91 |          | 
|---|
| 92 |         void clearCache(); | 
|---|
| 93 |  | 
|---|
| 94 |         SIMD_FORCE_INLINE const btVector3& getAabbMin() const | 
|---|
| 95 |         { | 
|---|
| 96 |                 return m_aabbMin; | 
|---|
| 97 |         } | 
|---|
| 98 |         SIMD_FORCE_INLINE const btVector3& getAabbMax() const | 
|---|
| 99 |         { | 
|---|
| 100 |                 return m_aabbMax; | 
|---|
| 101 |         } | 
|---|
| 102 |  | 
|---|
| 103 | }; | 
|---|
| 104 |  | 
|---|
| 105 |  | 
|---|
| 106 |  | 
|---|
| 107 |  | 
|---|
| 108 | /// btSoftBodyConcaveCollisionAlgorithm  supports collision between soft body shapes and (concave) trianges meshes. | 
|---|
| 109 | class btSoftBodyConcaveCollisionAlgorithm  : public btCollisionAlgorithm | 
|---|
| 110 | { | 
|---|
| 111 |  | 
|---|
| 112 |         bool    m_isSwapped; | 
|---|
| 113 |  | 
|---|
| 114 |         btSoftBodyTriangleCallback m_btSoftBodyTriangleCallback; | 
|---|
| 115 |  | 
|---|
| 116 | public: | 
|---|
| 117 |  | 
|---|
| 118 |         btSoftBodyConcaveCollisionAlgorithm( const btCollisionAlgorithmConstructionInfo& ci,btCollisionObject* body0,btCollisionObject* body1,bool isSwapped); | 
|---|
| 119 |  | 
|---|
| 120 |         virtual ~btSoftBodyConcaveCollisionAlgorithm(); | 
|---|
| 121 |  | 
|---|
| 122 |         virtual void processCollision (btCollisionObject* body0,btCollisionObject* body1,const btDispatcherInfo& dispatchInfo,btManifoldResult* resultOut); | 
|---|
| 123 |  | 
|---|
| 124 |         btScalar        calculateTimeOfImpact(btCollisionObject* body0,btCollisionObject* body1,const btDispatcherInfo& dispatchInfo,btManifoldResult* resultOut); | 
|---|
| 125 |  | 
|---|
| 126 |         virtual void    getAllContactManifolds(btManifoldArray& manifoldArray) | 
|---|
| 127 |         { | 
|---|
| 128 |                 //we don't add any manifolds | 
|---|
| 129 |         } | 
|---|
| 130 |  | 
|---|
| 131 |         void    clearCache(); | 
|---|
| 132 |  | 
|---|
| 133 |         struct CreateFunc :public       btCollisionAlgorithmCreateFunc | 
|---|
| 134 |         { | 
|---|
| 135 |                 virtual btCollisionAlgorithm* CreateCollisionAlgorithm(btCollisionAlgorithmConstructionInfo& ci, btCollisionObject* body0,btCollisionObject* body1) | 
|---|
| 136 |                 { | 
|---|
| 137 |                         void* mem = ci.m_dispatcher1->allocateCollisionAlgorithm(sizeof(btSoftBodyConcaveCollisionAlgorithm)); | 
|---|
| 138 |                         return new(mem) btSoftBodyConcaveCollisionAlgorithm(ci,body0,body1,false); | 
|---|
| 139 |                 } | 
|---|
| 140 |         }; | 
|---|
| 141 |  | 
|---|
| 142 |         struct SwappedCreateFunc :public        btCollisionAlgorithmCreateFunc | 
|---|
| 143 |         { | 
|---|
| 144 |                 virtual btCollisionAlgorithm* CreateCollisionAlgorithm(btCollisionAlgorithmConstructionInfo& ci, btCollisionObject* body0,btCollisionObject* body1) | 
|---|
| 145 |                 { | 
|---|
| 146 |                         void* mem = ci.m_dispatcher1->allocateCollisionAlgorithm(sizeof(btSoftBodyConcaveCollisionAlgorithm)); | 
|---|
| 147 |                         return new(mem) btSoftBodyConcaveCollisionAlgorithm(ci,body0,body1,true); | 
|---|
| 148 |                 } | 
|---|
| 149 |         }; | 
|---|
| 150 |  | 
|---|
| 151 | }; | 
|---|
| 152 |  | 
|---|
| 153 | #endif //SOFT_BODY_CONCAVE_COLLISION_ALGORITHM_H | 
|---|