| 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 |  | 
|---|
| 17 | #include "btManifoldResult.h" | 
|---|
| 18 | #include "BulletCollision/NarrowPhaseCollision/btPersistentManifold.h" | 
|---|
| 19 | #include "BulletCollision/CollisionDispatch/btCollisionObject.h" | 
|---|
| 20 |  | 
|---|
| 21 |  | 
|---|
| 22 | ///This is to allow MaterialCombiner/Custom Friction/Restitution values | 
|---|
| 23 | ContactAddedCallback            gContactAddedCallback=0; | 
|---|
| 24 |  | 
|---|
| 25 | ///User can override this material combiner by implementing gContactAddedCallback and setting body0->m_collisionFlags |= btCollisionObject::customMaterialCallback; | 
|---|
| 26 | inline btScalar calculateCombinedFriction(const btCollisionObject* body0,const btCollisionObject* body1) | 
|---|
| 27 | { | 
|---|
| 28 | btScalar friction = body0->getFriction() * body1->getFriction(); | 
|---|
| 29 |  | 
|---|
| 30 | const btScalar MAX_FRICTION  = btScalar(10.); | 
|---|
| 31 | if (friction < -MAX_FRICTION) | 
|---|
| 32 | friction = -MAX_FRICTION; | 
|---|
| 33 | if (friction > MAX_FRICTION) | 
|---|
| 34 | friction = MAX_FRICTION; | 
|---|
| 35 | return friction; | 
|---|
| 36 |  | 
|---|
| 37 | } | 
|---|
| 38 |  | 
|---|
| 39 | inline btScalar calculateCombinedRestitution(const btCollisionObject* body0,const btCollisionObject* body1) | 
|---|
| 40 | { | 
|---|
| 41 | return body0->getRestitution() * body1->getRestitution(); | 
|---|
| 42 | } | 
|---|
| 43 |  | 
|---|
| 44 |  | 
|---|
| 45 |  | 
|---|
| 46 | btManifoldResult::btManifoldResult(btCollisionObject* body0,btCollisionObject* body1) | 
|---|
| 47 | :m_manifoldPtr(0), | 
|---|
| 48 | m_body0(body0), | 
|---|
| 49 | m_body1(body1) | 
|---|
| 50 | #ifdef DEBUG_PART_INDEX | 
|---|
| 51 | ,m_partId0(-1), | 
|---|
| 52 | m_partId1(-1), | 
|---|
| 53 | m_index0(-1), | 
|---|
| 54 | m_index1(-1) | 
|---|
| 55 | #endif //DEBUG_PART_INDEX | 
|---|
| 56 | { | 
|---|
| 57 | m_rootTransA = body0->getWorldTransform(); | 
|---|
| 58 | m_rootTransB = body1->getWorldTransform(); | 
|---|
| 59 | } | 
|---|
| 60 |  | 
|---|
| 61 |  | 
|---|
| 62 | void btManifoldResult::addContactPoint(const btVector3& normalOnBInWorld,const btVector3& pointInWorld,btScalar depth) | 
|---|
| 63 | { | 
|---|
| 64 | btAssert(m_manifoldPtr); | 
|---|
| 65 | //order in manifold needs to match | 
|---|
| 66 |  | 
|---|
| 67 | //      if (depth > m_manifoldPtr->getContactBreakingThreshold()) | 
|---|
| 68 | if (depth > m_manifoldPtr->getContactProcessingThreshold()) | 
|---|
| 69 | return; | 
|---|
| 70 |  | 
|---|
| 71 | bool isSwapped = m_manifoldPtr->getBody0() != m_body0; | 
|---|
| 72 |  | 
|---|
| 73 | btVector3 pointA = pointInWorld + normalOnBInWorld * depth; | 
|---|
| 74 |  | 
|---|
| 75 | btVector3 localA; | 
|---|
| 76 | btVector3 localB; | 
|---|
| 77 |  | 
|---|
| 78 | if (isSwapped) | 
|---|
| 79 | { | 
|---|
| 80 | localA = m_rootTransB.invXform(pointA ); | 
|---|
| 81 | localB = m_rootTransA.invXform(pointInWorld); | 
|---|
| 82 | } else | 
|---|
| 83 | { | 
|---|
| 84 | localA = m_rootTransA.invXform(pointA ); | 
|---|
| 85 | localB = m_rootTransB.invXform(pointInWorld); | 
|---|
| 86 | } | 
|---|
| 87 |  | 
|---|
| 88 | btManifoldPoint newPt(localA,localB,normalOnBInWorld,depth); | 
|---|
| 89 | newPt.m_positionWorldOnA = pointA; | 
|---|
| 90 | newPt.m_positionWorldOnB = pointInWorld; | 
|---|
| 91 |  | 
|---|
| 92 | int insertIndex = m_manifoldPtr->getCacheEntry(newPt); | 
|---|
| 93 |  | 
|---|
| 94 | newPt.m_combinedFriction = calculateCombinedFriction(m_body0,m_body1); | 
|---|
| 95 | newPt.m_combinedRestitution = calculateCombinedRestitution(m_body0,m_body1); | 
|---|
| 96 |  | 
|---|
| 97 | //BP mod, store contact triangles. | 
|---|
| 98 | if (isSwapped) | 
|---|
| 99 | { | 
|---|
| 100 | newPt.m_partId0 = m_partId1; | 
|---|
| 101 | newPt.m_partId1 = m_partId0; | 
|---|
| 102 | newPt.m_index0  = m_index1; | 
|---|
| 103 | newPt.m_index1  = m_index0; | 
|---|
| 104 | } else | 
|---|
| 105 | { | 
|---|
| 106 | newPt.m_partId0 = m_partId0; | 
|---|
| 107 | newPt.m_partId1 = m_partId1; | 
|---|
| 108 | newPt.m_index0  = m_index0; | 
|---|
| 109 | newPt.m_index1  = m_index1; | 
|---|
| 110 | } | 
|---|
| 111 | //printf("depth=%f\n",depth); | 
|---|
| 112 | ///@todo, check this for any side effects | 
|---|
| 113 | if (insertIndex >= 0) | 
|---|
| 114 | { | 
|---|
| 115 | //const btManifoldPoint& oldPoint = m_manifoldPtr->getContactPoint(insertIndex); | 
|---|
| 116 | m_manifoldPtr->replaceContactPoint(newPt,insertIndex); | 
|---|
| 117 | } else | 
|---|
| 118 | { | 
|---|
| 119 | insertIndex = m_manifoldPtr->addManifoldPoint(newPt); | 
|---|
| 120 | } | 
|---|
| 121 |  | 
|---|
| 122 | //User can override friction and/or restitution | 
|---|
| 123 | if (gContactAddedCallback && | 
|---|
| 124 | //and if either of the two bodies requires custom material | 
|---|
| 125 | ((m_body0->getCollisionFlags() & btCollisionObject::CF_CUSTOM_MATERIAL_CALLBACK) || | 
|---|
| 126 | (m_body1->getCollisionFlags() & btCollisionObject::CF_CUSTOM_MATERIAL_CALLBACK))) | 
|---|
| 127 | { | 
|---|
| 128 | //experimental feature info, for per-triangle material etc. | 
|---|
| 129 | btCollisionObject* obj0 = isSwapped? m_body1 : m_body0; | 
|---|
| 130 | btCollisionObject* obj1 = isSwapped? m_body0 : m_body1; | 
|---|
| 131 | (*gContactAddedCallback)(m_manifoldPtr->getContactPoint(insertIndex),obj0,newPt.m_partId0,newPt.m_index0,obj1,newPt.m_partId1,newPt.m_index1); | 
|---|
| 132 | } | 
|---|
| 133 |  | 
|---|
| 134 | } | 
|---|
| 135 |  | 
|---|