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