Bullet Collision Detection & Physics Library
btManifoldResult.cpp
Go to the documentation of this file.
1/*
2Bullet Continuous Collision Detection and Physics Library
3Copyright (c) 2003-2006 Erwin Coumans http://continuousphysics.com/Bullet/
4
5This software is provided 'as-is', without any express or implied warranty.
6In no event will the authors be held liable for any damages arising from the use of this software.
7Permission is granted to anyone to use this software for any purpose,
8including commercial applications, and to alter it and redistribute it freely,
9subject to the following restrictions:
10
111. 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.
122. Altered source versions must be plainly marked as such, and must not be misrepresented as being the original software.
133. This notice may not be removed or altered from any source distribution.
14*/
15
16
17#include "btManifoldResult.h"
21
24
25
26
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
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
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
67{
68 return body0->getRestitution() * body1->getRestitution();
69}
70
72{
73 return body0->getContactDamping() + body1->getContactDamping();
74}
75
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
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 , m_closestPointDistanceThreshold(0)
100{
101}
102
103
104void btManifoldResult::addContactPoint(const btVector3& normalOnBInWorld,const btVector3& pointInWorld,btScalar depth)
105{
107 //order in manifold needs to match
108
110// if (depth > m_manifoldPtr->getContactProcessingThreshold())
111 return;
112
113 bool isSwapped = m_manifoldPtr->getBody0() != m_body0Wrap->getCollisionObject();
114 bool isNewCollision = m_manifoldPtr->getNumContacts() == 0;
115
116 btVector3 pointA = pointInWorld + normalOnBInWorld * depth;
117
118 btVector3 localA;
119 btVector3 localB;
120
121 if (isSwapped)
122 {
124 localB = m_body0Wrap->getCollisionObject()->getWorldTransform().invXform(pointInWorld);
125 } else
126 {
128 localB = m_body1Wrap->getCollisionObject()->getWorldTransform().invXform(pointInWorld);
129 }
130
131 btManifoldPoint newPt(localA,localB,normalOnBInWorld,depth);
132 newPt.m_positionWorldOnA = pointA;
133 newPt.m_positionWorldOnB = pointInWorld;
134
135 int insertIndex = m_manifoldPtr->getCacheEntry(newPt);
136
141
144 {
148 }
149
152 {
154 }
155
157
158
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 } else
168 {
169 newPt.m_partId0 = m_partId0;
170 newPt.m_partId1 = m_partId1;
171 newPt.m_index0 = m_index0;
172 newPt.m_index1 = m_index1;
173 }
174 //printf("depth=%f\n",depth);
176 if (insertIndex >= 0)
177 {
178 //const btManifoldPoint& oldPoint = m_manifoldPtr->getContactPoint(insertIndex);
179 m_manifoldPtr->replaceContactPoint(newPt,insertIndex);
180 } else
181 {
182 insertIndex = m_manifoldPtr->addManifoldPoint(newPt);
183 }
184
185 //User can override friction and/or restitution
187 //and if either of the two bodies requires custom material
190 {
191 //experimental feature info, for per-triangle material etc.
192 const btCollisionObjectWrapper* obj0Wrap = isSwapped? m_body1Wrap : m_body0Wrap;
193 const btCollisionObjectWrapper* obj1Wrap = isSwapped? m_body0Wrap : m_body1Wrap;
194 (*gContactAddedCallback)(m_manifoldPtr->getContactPoint(insertIndex),obj0Wrap,newPt.m_partId0,newPt.m_index0,obj1Wrap,newPt.m_partId1,newPt.m_index1);
195 }
196
197 if (gContactStartedCallback && isNewCollision)
198 {
200 }
201}
202
@ BT_CONTACT_FLAG_CONTACT_STIFFNESS_DAMPING
@ BT_CONTACT_FLAG_FRICTION_ANCHOR
ContactAddedCallback gContactAddedCallback
This is to allow MaterialCombiner/Custom Friction/Restitution values.
bool(* ContactAddedCallback)(btManifoldPoint &cp, const btCollisionObjectWrapper *colObj0Wrap, int partId0, int index0, const btCollisionObjectWrapper *colObj1Wrap, int partId1, int index1)
ContactStartedCallback gContactStartedCallback
float btScalar
The btScalar type abstracts floating point numbers, to easily switch between double and single floati...
Definition: btScalar.h:292
#define btAssert(x)
Definition: btScalar.h:131
void btPlaneSpace1(const T &n, T &p, T &q)
Definition: btVector3.h:1283
btCollisionObject can be used to manage collision detection objects.
btScalar getContactStiffness() const
btTransform & getWorldTransform()
btScalar getSpinningFriction() const
btScalar getFriction() const
btScalar getContactDamping() const
int getCollisionFlags() const
btScalar getRestitution() const
btScalar getRollingFriction() const
ManifoldContactPoint collects and maintains persistent contactpoints.
btScalar m_combinedSpinningFriction
btScalar m_combinedRollingFriction
btScalar m_combinedContactStiffness1
btScalar m_combinedRestitution
btVector3 m_lateralFrictionDir2
btVector3 m_positionWorldOnA
m_positionWorldOnA is redundant information, see getPositionWorldOnA(), but for clarity
btScalar m_combinedContactDamping1
btVector3 m_normalWorldOnB
btScalar m_combinedFriction
btVector3 m_positionWorldOnB
btVector3 m_lateralFrictionDir1
const btCollisionObjectWrapper * m_body0Wrap
static btScalar calculateCombinedFriction(const btCollisionObject *body0, const btCollisionObject *body1)
User can override this material combiner by implementing gContactAddedCallback and setting body0->m_c...
static btScalar calculateCombinedContactDamping(const btCollisionObject *body0, const btCollisionObject *body1)
const btCollisionObjectWrapper * m_body1Wrap
static btScalar calculateCombinedContactStiffness(const btCollisionObject *body0, const btCollisionObject *body1)
static btScalar calculateCombinedSpinningFriction(const btCollisionObject *body0, const btCollisionObject *body1)
static btScalar calculateCombinedRollingFriction(const btCollisionObject *body0, const btCollisionObject *body1)
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
btPersistentManifold * m_manifoldPtr
virtual void addContactPoint(const btVector3 &normalOnBInWorld, const btVector3 &pointInWorld, btScalar depth)
const btManifoldPoint & getContactPoint(int index) const
const btCollisionObject * getBody0() const
int getCacheEntry(const btManifoldPoint &newPoint) const
void replaceContactPoint(const btManifoldPoint &newPoint, int insertIndex)
int addManifoldPoint(const btManifoldPoint &newPoint, bool isPredictive=false)
btScalar getContactBreakingThreshold() const
btVector3 invXform(const btVector3 &inVec) const
Definition: btTransform.h:223
btVector3 can be used to represent 3D points and vectors.
Definition: btVector3.h:84
const btCollisionObject * getCollisionObject() const