Bullet Collision Detection & Physics Library: btManifoldResult.cpp Source File

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"
18 #include "BulletCollision/NarrowPhaseCollision/btPersistentManifold.h"
19 #include "BulletCollision/CollisionDispatch/btCollisionObject.h"
20 #include "BulletCollision/CollisionDispatch/btCollisionObjectWrapper.h"
21
23 ContactAddedCallback gContactAddedCallback=0;
24
25
26
27 btScalar btManifoldResult::calculateCombinedRollingFriction(const btCollisionObject* body0,const btCollisionObject* body1)
28 {
29 btScalar friction = body0->getRollingFriction() * body1->getFriction() + body1->getRollingFriction() * body0->getFriction();
30
31 const btScalar MAX_FRICTION = btScalar(10.);
32 if (friction < -MAX_FRICTION)
33 friction = -MAX_FRICTION;
34 if (friction > MAX_FRICTION)
35 friction = MAX_FRICTION;
36 return friction;
37
38 }
39
40 btScalar btManifoldResult::calculateCombinedSpinningFriction(const btCollisionObject* body0,const btCollisionObject* body1)
41 {
42 btScalar friction = body0->getSpinningFriction() * body1->getFriction() + body1->getSpinningFriction() * body0->getFriction();
43
44 const btScalar MAX_FRICTION = btScalar(10.);
45 if (friction < -MAX_FRICTION)
46 friction = -MAX_FRICTION;
47 if (friction > MAX_FRICTION)
48 friction = MAX_FRICTION;
49 return friction;
50 }
51
53 btScalar btManifoldResult::calculateCombinedFriction(const btCollisionObject* body0,const btCollisionObject* body1)
54 {
55 btScalar friction = body0->getFriction() * body1->getFriction();
56
57 const btScalar MAX_FRICTION = btScalar(10.);
58 if (friction < -MAX_FRICTION)
59 friction = -MAX_FRICTION;
60 if (friction > MAX_FRICTION)
61 friction = MAX_FRICTION;
62 return friction;
63
64 }
65
66 btScalar btManifoldResult::calculateCombinedRestitution(const btCollisionObject* body0,const btCollisionObject* body1)
67 {
68 return body0->getRestitution() * body1->getRestitution();
69 }
70
71 btScalar btManifoldResult::calculateCombinedContactDamping(const btCollisionObject* body0,const btCollisionObject* body1)
72 {
73 return body0->getContactDamping() + body1->getContactDamping();
74 }
75
76 btScalar btManifoldResult::calculateCombinedContactStiffness(const btCollisionObject* body0,const btCollisionObject* body1)
77 {
78
79 btScalar s0 = body0->getContactStiffness();
80 btScalar s1 = body1->getContactStiffness();
81
82 btScalar tmp0 = btScalar(1)/s0;
83 btScalar tmp1 = btScalar(1)/s1;
84 btScalar combinedStiffness = btScalar(1) / (tmp0+tmp1);
85 return combinedStiffness;
86 }
87
88
89 btManifoldResult::btManifoldResult(const btCollisionObjectWrapper* body0Wrap,const btCollisionObjectWrapper* body1Wrap)
90 :m_manifoldPtr(0),
91 m_body0Wrap(body0Wrap),
92 m_body1Wrap(body1Wrap)
93 #ifdef DEBUG_PART_INDEX
94 ,m_partId0(-1),
95 m_partId1(-1),
96 m_index0(-1),
97 m_index1(-1)
98 #endif //DEBUG_PART_INDEX
99 {
100 }
101
102
103 void btManifoldResult::addContactPoint(const btVector3& normalOnBInWorld,const btVector3& pointInWorld,btScalar depth)
104 {
105 btAssert(m_manifoldPtr);
106 //order in manifold needs to match
107
108 if (depth > m_manifoldPtr->getContactBreakingThreshold())
109 // if (depth > m_manifoldPtr->getContactProcessingThreshold())
110 return;
111
112 bool isSwapped = m_manifoldPtr->getBody0() != m_body0Wrap->getCollisionObject();
113
114 btVector3 pointA = pointInWorld + normalOnBInWorld * depth;
115
116 btVector3 localA;
117 btVector3 localB;
118
119 if (isSwapped)
120 {
121 localA = m_body1Wrap->getCollisionObject()->getWorldTransform().invXform(pointA );
122 localB = m_body0Wrap->getCollisionObject()->getWorldTransform().invXform(pointInWorld);
123 } else
124 {
125 localA = m_body0Wrap->getCollisionObject()->getWorldTransform().invXform(pointA );
126 localB = m_body1Wrap->getCollisionObject()->getWorldTransform().invXform(pointInWorld);
127 }
128
129 btManifoldPoint newPt(localA,localB,normalOnBInWorld,depth);
130 newPt.m_positionWorldOnA = pointA;
131 newPt.m_positionWorldOnB = pointInWorld;
132
133 int insertIndex = m_manifoldPtr->getCacheEntry(newPt);
134
135 newPt.m_combinedFriction = calculateCombinedFriction(m_body0Wrap->getCollisionObject(),m_body1Wrap->getCollisionObject());
136 newPt.m_combinedRestitution = calculateCombinedRestitution(m_body0Wrap->getCollisionObject(),m_body1Wrap->getCollisionObject());
137 newPt.m_combinedRollingFriction = calculateCombinedRollingFriction(m_body0Wrap->getCollisionObject(),m_body1Wrap->getCollisionObject());
138 newPt.m_combinedSpinningFriction = calculateCombinedSpinningFriction(m_body0Wrap->getCollisionObject(),m_body1Wrap->getCollisionObject());
139
140 if ( (m_body0Wrap->getCollisionObject()->getCollisionFlags()& btCollisionObject::CF_HAS_CONTACT_STIFFNESS_DAMPING) ||
141 (m_body1Wrap->getCollisionObject()->getCollisionFlags()& btCollisionObject::CF_HAS_CONTACT_STIFFNESS_DAMPING))
142 {
143 newPt.m_combinedContactDamping1 = calculateCombinedContactDamping(m_body0Wrap->getCollisionObject(),m_body1Wrap->getCollisionObject());
144 newPt.m_combinedContactStiffness1 = calculateCombinedContactStiffness(m_body0Wrap->getCollisionObject(),m_body1Wrap->getCollisionObject());
145 newPt.m_contactPointFlags |= BT_CONTACT_FLAG_CONTACT_STIFFNESS_DAMPING;
146 }
147
148 btPlaneSpace1(newPt.m_normalWorldOnB,newPt.m_lateralFrictionDir1,newPt.m_lateralFrictionDir2);
149
150
151
152 //BP mod, store contact triangles.
153 if (isSwapped)
154 {
155 newPt.m_partId0 = m_partId1;
156 newPt.m_partId1 = m_partId0;
157 newPt.m_index0 = m_index1;
158 newPt.m_index1 = m_index0;
159 } else
160 {
161 newPt.m_partId0 = m_partId0;
162 newPt.m_partId1 = m_partId1;
163 newPt.m_index0 = m_index0;
164 newPt.m_index1 = m_index1;
165 }
166 //printf("depth=%f\n",depth);
168 if (insertIndex >= 0)
169 {
170 //const btManifoldPoint& oldPoint = m_manifoldPtr->getContactPoint(insertIndex);
171 m_manifoldPtr->replaceContactPoint(newPt,insertIndex);
172 } else
173 {
174 insertIndex = m_manifoldPtr->addManifoldPoint(newPt);
175 }
176
177 //User can override friction and/or restitution
178 if (gContactAddedCallback &&
179 //and if either of the two bodies requires custom material
180 ((m_body0Wrap->getCollisionObject()->getCollisionFlags() & btCollisionObject::CF_CUSTOM_MATERIAL_CALLBACK) ||
181 (m_body1Wrap->getCollisionObject()->getCollisionFlags() & btCollisionObject::CF_CUSTOM_MATERIAL_CALLBACK)))
182 {
183 //experimental feature info, for per-triangle material etc.
184 const btCollisionObjectWrapper* obj0Wrap = isSwapped? m_body1Wrap : m_body0Wrap;
185 const btCollisionObjectWrapper* obj1Wrap = isSwapped? m_body0Wrap : m_body1Wrap;
186 (*gContactAddedCallback)(m_manifoldPtr->getContactPoint(insertIndex),obj0Wrap,newPt.m_partId0,newPt.m_index0,obj1Wrap,newPt.m_partId1,newPt.m_index1);
187 }
188
189 }
190
btManifoldPoint::m_contactPointFlags
int m_contactPointFlags
Definition: btManifoldPoint.h:115
btPersistentManifold::getContactBreakingThreshold
btScalar getContactBreakingThreshold() const
Definition: btPersistentManifold.cpp:240
btManifoldPoint::m_combinedContactStiffness1
btScalar m_combinedContactStiffness1
Definition: btManifoldPoint.h:126
btManifoldPoint::m_partId1
int m_partId1
Definition: btManifoldPoint.h:109
btManifoldPoint::m_lateralFrictionDir1
btVector3 m_lateralFrictionDir1
Definition: btManifoldPoint.h:139
btCollisionObject::getContactDamping
btScalar getContactDamping() const
Definition: btCollisionObject.h:356
btManifoldResult::calculateCombinedContactDamping
static btScalar calculateCombinedContactDamping(const btCollisionObject *body0, const btCollisionObject *body1)
Definition: btManifoldResult.cpp:71
btManifoldResult::m_manifoldPtr
btPersistentManifold * m_manifoldPtr
Definition: btManifoldResult.h:43
btManifoldResult.h
btManifoldPoint::m_combinedRestitution
btScalar m_combinedRestitution
Definition: btManifoldPoint.h:105
BT_CONTACT_FLAG_CONTACT_STIFFNESS_DAMPING
Definition: btManifoldPoint.h:43
btManifoldResult::calculateCombinedRollingFriction
static btScalar calculateCombinedRollingFriction(const btCollisionObject *body0, const btCollisionObject *body1)
Definition: btManifoldResult.cpp:27
btCollisionObject::getContactStiffness
btScalar getContactStiffness() const
Definition: btCollisionObject.h:351
btPlaneSpace1
void btPlaneSpace1(const T &n, T &p, T &q)
Definition: btVector3.h:1282
btAssert
#define btAssert(x)
Definition: btScalar.h:114
btManifoldResult::calculateCombinedContactStiffness
static btScalar calculateCombinedContactStiffness(const btCollisionObject *body0, const btCollisionObject *body1)
Definition: btManifoldResult.cpp:76
btPersistentManifold.h
btManifoldPoint
ManifoldContactPoint collects and maintains persistent contactpoints.
Definition: btManifoldPoint.h:48
btManifoldResult::m_body1Wrap
const btCollisionObjectWrapper * m_body1Wrap
Definition: btManifoldResult.h:46
btPersistentManifold::getContactPoint
const btManifoldPoint & getContactPoint(int index) const
Definition: btPersistentManifold.h:117
btManifoldResult::addContactPoint
virtual void addContactPoint(const btVector3 &normalOnBInWorld, const btVector3 &pointInWorld, btScalar depth)
Definition: btManifoldResult.cpp:103
btManifoldPoint::m_combinedRollingFriction
btScalar m_combinedRollingFriction
Definition: btManifoldPoint.h:103
btCollisionObject::getWorldTransform
btTransform & getWorldTransform()
Definition: btCollisionObject.h:367
btManifoldPoint::m_normalWorldOnB
btVector3 m_normalWorldOnB
Definition: btManifoldPoint.h:99
btManifoldPoint::m_positionWorldOnB
btVector3 m_positionWorldOnB
Definition: btManifoldPoint.h:96
btPersistentManifold::getBody0
const btCollisionObject * getBody0() const
Definition: btPersistentManifold.h:94
btCollisionObjectWrapper::getCollisionObject
const btCollisionObject * getCollisionObject() const
Definition: btCollisionObjectWrapper.h:39
btCollisionObject::getRestitution
btScalar getRestitution() const
Definition: btCollisionObject.h:304
btCollisionObject
btCollisionObject can be used to manage collision detection objects.
Definition: btCollisionObject.h:49
btManifoldResult::btManifoldResult
btManifoldResult()
Definition: btManifoldResult.h:55
btManifoldPoint::m_positionWorldOnA
btVector3 m_positionWorldOnA
m_positionWorldOnA is redundant information, see getPositionWorldOnA(), but for clarity ...
Definition: btManifoldPoint.h:98
btCollisionObjectWrapper.h
btTransform::invXform
btVector3 invXform(const btVector3 &inVec) const
Definition: btTransform.h:223
btCollisionObject::getRollingFriction
btScalar getRollingFriction() const
Definition: btCollisionObject.h:323
btCollisionObject::getCollisionFlags
int getCollisionFlags() const
Definition: btCollisionObject.h:469
btManifoldResult::calculateCombinedFriction
static btScalar calculateCombinedFriction(const btCollisionObject *body0, const btCollisionObject *body1)
User can override this material combiner by implementing gContactAddedCallback and setting body0->m_c...
Definition: btManifoldResult.cpp:53
btVector3
btVector3 can be used to represent 3D points and vectors.
Definition: btVector3.h:83
ContactAddedCallback
bool(* ContactAddedCallback)(btManifoldPoint &cp, const btCollisionObjectWrapper *colObj0Wrap, int partId0, int index0, const btCollisionObjectWrapper *colObj1Wrap, int partId1, int index1)
Definition: btManifoldResult.h:32
btManifoldPoint::m_index1
int m_index1
Definition: btManifoldPoint.h:111
btManifoldPoint::m_combinedContactDamping1
btScalar m_combinedContactDamping1
Definition: btManifoldPoint.h:132
btCollisionObject::getSpinningFriction
btScalar getSpinningFriction() const
Definition: btCollisionObject.h:332
btCollisionObject::CF_HAS_CONTACT_STIFFNESS_DAMPING
Definition: btCollisionObject.h:138
btManifoldResult::calculateCombinedSpinningFriction
static btScalar calculateCombinedSpinningFriction(const btCollisionObject *body0, const btCollisionObject *body1)
Definition: btManifoldResult.cpp:40
btManifoldResult::m_index0
int m_index0
Definition: btManifoldResult.h:49
btCollisionObject.h
gContactAddedCallback
ContactAddedCallback gContactAddedCallback
This is to allow MaterialCombiner/Custom Friction/Restitution values.
Definition: btManifoldResult.cpp:23
btManifoldPoint::m_combinedFriction
btScalar m_combinedFriction
Definition: btManifoldPoint.h:102
btManifoldResult::m_body0Wrap
const btCollisionObjectWrapper * m_body0Wrap
Definition: btManifoldResult.h:45
btManifoldResult::m_index1
int m_index1
Definition: btManifoldResult.h:50
btManifoldResult::m_partId0
int m_partId0
Definition: btManifoldResult.h:47
btManifoldPoint::m_lateralFrictionDir2
btVector3 m_lateralFrictionDir2
Definition: btManifoldPoint.h:140
btPersistentManifold::replaceContactPoint
void replaceContactPoint(const btManifoldPoint &newPoint, int insertIndex)
Definition: btPersistentManifold.h:175
btManifoldResult::calculateCombinedRestitution
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 ...
Definition: btManifoldResult.cpp:66
btCollisionObject::getFriction
btScalar getFriction() const
Definition: btCollisionObject.h:313
btCollisionObjectWrapper
Definition: btCollisionObjectWrapper.h:17
btCollisionObject::CF_CUSTOM_MATERIAL_CALLBACK
Definition: btCollisionObject.h:134
btPersistentManifold::addManifoldPoint
int addManifoldPoint(const btManifoldPoint &newPoint, bool isPredictive=false)
Definition: btPersistentManifold.cpp:208
btManifoldPoint::m_partId0
int m_partId0
Definition: btManifoldPoint.h:108
btManifoldResult::m_partId1
int m_partId1
Definition: btManifoldResult.h:48
btScalar
float btScalar
The btScalar type abstracts floating point numbers, to easily switch between double and single floati...
Definition: btScalar.h:279
btManifoldPoint::m_combinedSpinningFriction
btScalar m_combinedSpinningFriction
Definition: btManifoldPoint.h:104
btManifoldPoint::m_index0
int m_index0
Definition: btManifoldPoint.h:110
btPersistentManifold::getCacheEntry
int getCacheEntry(const btManifoldPoint &newPoint) const
Definition: btPersistentManifold.cpp:188