Back to index

supertuxkart  0.5+dfsg1
btManifoldResult.cpp
Go to the documentation of this file.
00001 /*
00002 Bullet Continuous Collision Detection and Physics Library
00003 Copyright (c) 2003-2006 Erwin Coumans  http://continuousphysics.com/Bullet/
00004 
00005 This software is provided 'as-is', without any express or implied warranty.
00006 In no event will the authors be held liable for any damages arising from the use of this software.
00007 Permission is granted to anyone to use this software for any purpose, 
00008 including commercial applications, and to alter it and redistribute it freely, 
00009 subject to the following restrictions:
00010 
00011 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.
00012 2. Altered source versions must be plainly marked as such, and must not be misrepresented as being the original software.
00013 3. This notice may not be removed or altered from any source distribution.
00014 */
00015 
00016 
00017 #include "btManifoldResult.h"
00018 #include "BulletCollision/NarrowPhaseCollision/btPersistentManifold.h"
00019 #include "BulletCollision/CollisionDispatch/btCollisionObject.h"
00020 
00021 
00023 ContactAddedCallback        gContactAddedCallback=0;
00024 
00026 inline btScalar      calculateCombinedFriction(const btCollisionObject* body0,const btCollisionObject* body1)
00027 {
00028        btScalar friction = body0->getFriction() * body1->getFriction();
00029 
00030        const btScalar MAX_FRICTION  = btScalar(10.);
00031        if (friction < -MAX_FRICTION)
00032               friction = -MAX_FRICTION;
00033        if (friction > MAX_FRICTION)
00034               friction = MAX_FRICTION;
00035        return friction;
00036 
00037 }
00038 
00039 inline btScalar      calculateCombinedRestitution(const btCollisionObject* body0,const btCollisionObject* body1)
00040 {
00041        return body0->getRestitution() * body1->getRestitution();
00042 }
00043 
00044 
00045 
00046 btManifoldResult::btManifoldResult(btCollisionObject* body0,btCollisionObject* body1)
00047               :m_manifoldPtr(0),
00048               m_body0(body0),
00049               m_body1(body1)
00050 {
00051        m_rootTransA = body0->getWorldTransform();
00052        m_rootTransB = body1->getWorldTransform();
00053 }
00054 
00055 
00056 void btManifoldResult::addContactPoint(const btVector3& normalOnBInWorld,const btVector3& pointInWorld,btScalar depth)
00057 {
00058        assert(m_manifoldPtr);
00059        //order in manifold needs to match
00060        
00061        if (depth > m_manifoldPtr->getContactBreakingThreshold())
00062               return;
00063 
00064        bool isSwapped = m_manifoldPtr->getBody0() != m_body0;
00065 
00066        btVector3 pointA = pointInWorld + normalOnBInWorld * depth;
00067 
00068        btVector3 localA;
00069        btVector3 localB;
00070        
00071        if (isSwapped)
00072        {
00073               localA = m_rootTransB.invXform(pointA );
00074               localB = m_rootTransA.invXform(pointInWorld);
00075        } else
00076        {
00077               localA = m_rootTransA.invXform(pointA );
00078               localB = m_rootTransB.invXform(pointInWorld);
00079        }
00080 
00081        btManifoldPoint newPt(localA,localB,normalOnBInWorld,depth);
00082        newPt.m_positionWorldOnA = pointA;
00083        newPt.m_positionWorldOnB = pointInWorld;
00084        
00085        int insertIndex = m_manifoldPtr->getCacheEntry(newPt);
00086 
00087        newPt.m_combinedFriction = calculateCombinedFriction(m_body0,m_body1);
00088        newPt.m_combinedRestitution = calculateCombinedRestitution(m_body0,m_body1);
00089 
00090    //BP mod, store contact triangles.
00091    newPt.m_partId0 = m_partId0;
00092    newPt.m_partId1 = m_partId1;
00093    newPt.m_index0  = m_index0;
00094    newPt.m_index1  = m_index1;
00095        
00097        if (insertIndex >= 0)
00098        {
00099               //const btManifoldPoint& oldPoint = m_manifoldPtr->getContactPoint(insertIndex);
00100               m_manifoldPtr->replaceContactPoint(newPt,insertIndex);
00101        } else
00102        {
00103               m_manifoldPtr->AddManifoldPoint(newPt);
00104        }
00105 
00106        //User can override friction and/or restitution
00107        if (gContactAddedCallback &&
00108               //and if either of the two bodies requires custom material
00109                ((m_body0->getCollisionFlags() & btCollisionObject::CF_CUSTOM_MATERIAL_CALLBACK) ||
00110                  (m_body1->getCollisionFlags() & btCollisionObject::CF_CUSTOM_MATERIAL_CALLBACK)))
00111        {
00112               //experimental feature info, for per-triangle material etc.
00113               btCollisionObject* obj0 = isSwapped? m_body1 : m_body0;
00114               btCollisionObject* obj1 = isSwapped? m_body0 : m_body1;
00115               (*gContactAddedCallback)(newPt,obj0,m_partId0,m_index0,obj1,m_partId1,m_index1);
00116        }
00117 
00118 }
00119