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