Bullet Collision Detection & Physics Library
btManifoldResult.cpp
Go to the documentation of this file.
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"
21 
24 
31 
33 {
34  btScalar friction = body0->getRollingFriction() * body1->getFriction() + body1->getRollingFriction() * body0->getFriction();
35 
36  const btScalar MAX_FRICTION = btScalar(10.);
37  if (friction < -MAX_FRICTION)
38  friction = -MAX_FRICTION;
39  if (friction > MAX_FRICTION)
40  friction = MAX_FRICTION;
41  return friction;
42 
43 }
44 
46 {
47  btScalar friction = body0->getSpinningFriction() * body1->getFriction() + body1->getSpinningFriction() * body0->getFriction();
48 
49  const btScalar MAX_FRICTION = btScalar(10.);
50  if (friction < -MAX_FRICTION)
51  friction = -MAX_FRICTION;
52  if (friction > MAX_FRICTION)
53  friction = MAX_FRICTION;
54  return friction;
55 }
56 
59 {
60  btScalar friction = body0->getFriction() * body1->getFriction();
61 
62  const btScalar MAX_FRICTION = btScalar(10.);
63  if (friction < -MAX_FRICTION)
64  friction = -MAX_FRICTION;
65  if (friction > MAX_FRICTION)
66  friction = MAX_FRICTION;
67  return friction;
68 
69 }
70 
72 {
73  return body0->getRestitution() * body1->getRestitution();
74 }
75 
77 {
78  return body0->getContactDamping() + body1->getContactDamping();
79 }
80 
82 {
83 
84  btScalar s0 = body0->getContactStiffness();
85  btScalar s1 = body1->getContactStiffness();
86 
87  btScalar tmp0 = btScalar(1)/s0;
88  btScalar tmp1 = btScalar(1)/s1;
89  btScalar combinedStiffness = btScalar(1) / (tmp0+tmp1);
90  return combinedStiffness;
91 }
92 
93 
95  :m_manifoldPtr(0),
96  m_body0Wrap(body0Wrap),
97  m_body1Wrap(body1Wrap)
98 #ifdef DEBUG_PART_INDEX
99  ,m_partId0(-1),
100  m_partId1(-1),
101  m_index0(-1),
102  m_index1(-1)
103 #endif //DEBUG_PART_INDEX
105 {
106 }
107 
108 
109 void btManifoldResult::addContactPoint(const btVector3& normalOnBInWorld,const btVector3& pointInWorld,btScalar depth)
110 {
112  //order in manifold needs to match
113 
115 // if (depth > m_manifoldPtr->getContactProcessingThreshold())
116  return;
117 
118  bool isSwapped = m_manifoldPtr->getBody0() != m_body0Wrap->getCollisionObject();
119  bool isNewCollision = m_manifoldPtr->getNumContacts() == 0;
120 
121  btVector3 pointA = pointInWorld + normalOnBInWorld * depth;
122 
123  btVector3 localA;
124  btVector3 localB;
125 
126  if (isSwapped)
127  {
129  localB = m_body0Wrap->getCollisionObject()->getWorldTransform().invXform(pointInWorld);
130  } else
131  {
133  localB = m_body1Wrap->getCollisionObject()->getWorldTransform().invXform(pointInWorld);
134  }
135 
136  btManifoldPoint newPt(localA,localB,normalOnBInWorld,depth);
137  newPt.m_positionWorldOnA = pointA;
138  newPt.m_positionWorldOnB = pointInWorld;
139 
140  int insertIndex = m_manifoldPtr->getCacheEntry(newPt);
141 
146 
149  {
153  }
154 
157  {
159  }
160 
162 
163 
164 
165  //BP mod, store contact triangles.
166  if (isSwapped)
167  {
168  newPt.m_partId0 = m_partId1;
169  newPt.m_partId1 = m_partId0;
170  newPt.m_index0 = m_index1;
171  newPt.m_index1 = m_index0;
172  } else
173  {
174  newPt.m_partId0 = m_partId0;
175  newPt.m_partId1 = m_partId1;
176  newPt.m_index0 = m_index0;
177  newPt.m_index1 = m_index1;
178  }
179  //printf("depth=%f\n",depth);
181  if (insertIndex >= 0)
182  {
183  //const btManifoldPoint& oldPoint = m_manifoldPtr->getContactPoint(insertIndex);
184  m_manifoldPtr->replaceContactPoint(newPt,insertIndex);
185  } else
186  {
187  insertIndex = m_manifoldPtr->addManifoldPoint(newPt);
188  }
189 
190  //User can override friction and/or restitution
191  if (gContactAddedCallback &&
192  //and if either of the two bodies requires custom material
195  {
196  //experimental feature info, for per-triangle material etc.
197  const btCollisionObjectWrapper* obj0Wrap = isSwapped? m_body1Wrap : m_body0Wrap;
198  const btCollisionObjectWrapper* obj1Wrap = isSwapped? m_body0Wrap : m_body1Wrap;
199  (*gContactAddedCallback)(m_manifoldPtr->getContactPoint(insertIndex),obj0Wrap,newPt.m_partId0,newPt.m_index0,obj1Wrap,newPt.m_partId1,newPt.m_index1);
200  }
201 
202  if (gContactStartedCallback && isNewCollision)
203  {
205  }
206 }
207 
ContactStartedCallback gContactStartedCallback
int getCacheEntry(const btManifoldPoint &newPoint) const
btScalar m_combinedContactStiffness1
btVector3 m_lateralFrictionDir1
static btScalar calculateCombinedContactDamping(const btCollisionObject *body0, const btCollisionObject *body1)
btScalar getContactBreakingThreshold() const
btPersistentManifold * m_manifoldPtr
btScalar m_combinedRestitution
static btScalar calculateCombinedRollingFriction(const btCollisionObject *body0, const btCollisionObject *body1)
void btPlaneSpace1(const T &n, T &p, T &q)
Definition: btVector3.h:1284
CalculateCombinedCallback gCalculateCombinedRestitutionCallback
#define btAssert(x)
Definition: btScalar.h:131
static btScalar calculateCombinedContactStiffness(const btCollisionObject *body0, const btCollisionObject *body1)
ManifoldContactPoint collects and maintains persistent contactpoints.
int getCollisionFlags() const
const btCollisionObject * getBody0() const
btScalar(* CalculateCombinedCallback)(const btCollisionObject *body0, const btCollisionObject *body1)
These callbacks are used to customize the algorith that combine restitution, friction, damping, Stiffness.
const btCollisionObjectWrapper * m_body1Wrap
btScalar getSpinningFriction() const
virtual void addContactPoint(const btVector3 &normalOnBInWorld, const btVector3 &pointInWorld, btScalar depth)
btScalar m_combinedRollingFriction
btTransform & getWorldTransform()
btVector3 m_normalWorldOnB
btScalar m_closestPointDistanceThreshold
btVector3 m_positionWorldOnB
CalculateCombinedCallback gCalculateCombinedContactDampingCallback
btScalar getContactDamping() const
btCollisionObject can be used to manage collision detection objects.
btVector3 m_positionWorldOnA
m_positionWorldOnA is redundant information, see getPositionWorldOnA(), but for clarity ...
btScalar getRestitution() const
const btManifoldPoint & getContactPoint(int index) const
static btScalar calculateCombinedFriction(const btCollisionObject *body0, const btCollisionObject *body1)
User can override this material combiner by implementing gContactAddedCallback and setting body0->m_c...
btVector3 invXform(const btVector3 &inVec) const
Definition: btTransform.h:223
btVector3 can be used to represent 3D points and vectors.
Definition: btVector3.h:83
bool(* ContactAddedCallback)(btManifoldPoint &cp, const btCollisionObjectWrapper *colObj0Wrap, int partId0, int index0, const btCollisionObjectWrapper *colObj1Wrap, int partId1, int index1)
btScalar m_combinedContactDamping1
CalculateCombinedCallback gCalculateCombinedSpinningFrictionCallback
btScalar getRollingFriction() const
static btScalar calculateCombinedSpinningFriction(const btCollisionObject *body0, const btCollisionObject *body1)
btScalar getFriction() const
ContactAddedCallback gContactAddedCallback
This is to allow MaterialCombiner/Custom Friction/Restitution values.
btScalar m_combinedFriction
const btCollisionObjectWrapper * m_body0Wrap
CalculateCombinedCallback gCalculateCombinedContactStiffnessCallback
btVector3 m_lateralFrictionDir2
void replaceContactPoint(const btManifoldPoint &newPoint, int insertIndex)
static btScalar calculateCombinedRestitution(const btCollisionObject *body0, const btCollisionObject *body1)
in the future we can let the user override the methods to combine restitution and friction ...
CalculateCombinedCallback gCalculateCombinedFrictionCallback
CalculateCombinedCallback gCalculateCombinedRollingFrictionCallback
int addManifoldPoint(const btManifoldPoint &newPoint, bool isPredictive=false)
float btScalar
The btScalar type abstracts floating point numbers, to easily switch between double and single floati...
Definition: btScalar.h:292
btScalar m_combinedSpinningFriction
btScalar getContactStiffness() const
const btCollisionObject * getCollisionObject() const