Bullet Collision Detection & Physics Library
btPersistentManifold.h
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 #ifndef BT_PERSISTENT_MANIFOLD_H
17 #define BT_PERSISTENT_MANIFOLD_H
18 
19 
20 #include "LinearMath/btVector3.h"
21 #include "LinearMath/btTransform.h"
22 #include "btManifoldPoint.h"
23 class btCollisionObject;
25 
26 struct btCollisionResult;
27 
30 
31 typedef bool (*ContactDestroyedCallback)(void* userPersistentData);
32 typedef bool (*ContactProcessedCallback)(btManifoldPoint& cp,void* body0,void* body1);
35 
36 //the enum starts at 1024 to avoid type conflicts with btTypedConstraint
38 {
41 };
42 
43 #define MANIFOLD_CACHE_SIZE 4
44 
52 
53 
55 //ATTRIBUTE_ALIGNED16( class) btPersistentManifold : public btTypedObject
56 {
57 
59 
63 
65 
68 
69 
71  int sortCachedPoints(const btManifoldPoint& pt);
72 
73  int findContactPoint(const btManifoldPoint* unUsed, int numUnused,const btManifoldPoint& pt);
74 
75 public:
76 
78 
81 
82  int m_index1a;
83 
85 
86  btPersistentManifold(const btCollisionObject* body0,const btCollisionObject* body1,int , btScalar contactBreakingThreshold,btScalar contactProcessingThreshold)
87  : btTypedObject(BT_PERSISTENT_MANIFOLD_TYPE),
88  m_body0(body0),m_body1(body1),m_cachedPoints(0),
89  m_contactBreakingThreshold(contactBreakingThreshold),
90  m_contactProcessingThreshold(contactProcessingThreshold)
91  {
92  }
93 
94  SIMD_FORCE_INLINE const btCollisionObject* getBody0() const { return m_body0;}
95  SIMD_FORCE_INLINE const btCollisionObject* getBody1() const { return m_body1;}
96 
97  void setBodies(const btCollisionObject* body0,const btCollisionObject* body1)
98  {
99  m_body0 = body0;
100  m_body1 = body1;
101  }
102 
103  void clearUserCache(btManifoldPoint& pt);
104 
105 #ifdef DEBUG_PERSISTENCY
106  void DebugPersistency();
107 #endif //
108 
109  SIMD_FORCE_INLINE int getNumContacts() const { return m_cachedPoints;}
111  void setNumContacts(int cachedPoints)
112  {
113  m_cachedPoints = cachedPoints;
114  }
115 
116 
118  {
119  btAssert(index < m_cachedPoints);
120  return m_pointCache[index];
121  }
122 
124  {
125  btAssert(index < m_cachedPoints);
126  return m_pointCache[index];
127  }
128 
130  btScalar getContactBreakingThreshold() const;
131 
133  {
134  return m_contactProcessingThreshold;
135  }
136 
137  void setContactBreakingThreshold(btScalar contactBreakingThreshold)
138  {
139  m_contactBreakingThreshold = contactBreakingThreshold;
140  }
141 
142  void setContactProcessingThreshold(btScalar contactProcessingThreshold)
143  {
144  m_contactProcessingThreshold = contactProcessingThreshold;
145  }
146 
147 
148 
149 
150  int getCacheEntry(const btManifoldPoint& newPoint) const;
151 
152  int addManifoldPoint( const btManifoldPoint& newPoint, bool isPredictive=false);
153 
154  void removeContactPoint (int index)
155  {
156  clearUserCache(m_pointCache[index]);
157 
158  int lastUsedIndex = getNumContacts() - 1;
159 // m_pointCache[index] = m_pointCache[lastUsedIndex];
160  if(index != lastUsedIndex)
161  {
162  m_pointCache[index] = m_pointCache[lastUsedIndex];
163  //get rid of duplicated userPersistentData pointer
164  m_pointCache[lastUsedIndex].m_userPersistentData = 0;
165  m_pointCache[lastUsedIndex].m_appliedImpulse = 0.f;
166  m_pointCache[lastUsedIndex].m_lateralFrictionInitialized = false;
167  m_pointCache[lastUsedIndex].m_appliedImpulseLateral1 = 0.f;
168  m_pointCache[lastUsedIndex].m_appliedImpulseLateral2 = 0.f;
169  m_pointCache[lastUsedIndex].m_lifeTime = 0;
170  }
171 
172  btAssert(m_pointCache[lastUsedIndex].m_userPersistentData==0);
173  m_cachedPoints--;
174  }
175  void replaceContactPoint(const btManifoldPoint& newPoint,int insertIndex)
176  {
177  btAssert(validContactDistance(newPoint));
178 
179 #define MAINTAIN_PERSISTENCY 1
180 #ifdef MAINTAIN_PERSISTENCY
181  int lifeTime = m_pointCache[insertIndex].getLifeTime();
182  btScalar appliedImpulse = m_pointCache[insertIndex].m_appliedImpulse;
183  btScalar appliedLateralImpulse1 = m_pointCache[insertIndex].m_appliedImpulseLateral1;
184  btScalar appliedLateralImpulse2 = m_pointCache[insertIndex].m_appliedImpulseLateral2;
185 // bool isLateralFrictionInitialized = m_pointCache[insertIndex].m_lateralFrictionInitialized;
186 
187 
188 
189  btAssert(lifeTime>=0);
190  void* cache = m_pointCache[insertIndex].m_userPersistentData;
191 
192  m_pointCache[insertIndex] = newPoint;
193 
194  m_pointCache[insertIndex].m_userPersistentData = cache;
195  m_pointCache[insertIndex].m_appliedImpulse = appliedImpulse;
196  m_pointCache[insertIndex].m_appliedImpulseLateral1 = appliedLateralImpulse1;
197  m_pointCache[insertIndex].m_appliedImpulseLateral2 = appliedLateralImpulse2;
198 
199  m_pointCache[insertIndex].m_appliedImpulse = appliedImpulse;
200  m_pointCache[insertIndex].m_appliedImpulseLateral1 = appliedLateralImpulse1;
201  m_pointCache[insertIndex].m_appliedImpulseLateral2 = appliedLateralImpulse2;
202 
203 
204  m_pointCache[insertIndex].m_lifeTime = lifeTime;
205 #else
206  clearUserCache(m_pointCache[insertIndex]);
207  m_pointCache[insertIndex] = newPoint;
208 
209 #endif
210  }
211 
212 
213  bool validContactDistance(const btManifoldPoint& pt) const
214  {
215  return pt.m_distance1 <= getContactBreakingThreshold();
216  }
218  void refreshContactPoints( const btTransform& trA,const btTransform& trB);
219 
220 
222  {
223  int i;
224  for (i=0;i<m_cachedPoints;i++)
225  {
226  clearUserCache(m_pointCache[i]);
227  }
228  m_cachedPoints = 0;
229  }
230 
231 
232 
233 }
234 ;
235 
236 
237 
238 
239 
240 #endif //BT_PERSISTENT_MANIFOLD_H
btPersistentManifold is a contact point cache, it stays persistent as long as objects are overlapping...
const btCollisionObject * m_body1
btScalar m_appliedImpulseLateral1
#define ATTRIBUTE_ALIGNED128(a)
Definition: btScalar.h:66
int getLifeTime() const
btScalar m_appliedImpulse
#define btAssert(x)
Definition: btScalar.h:113
#define SIMD_FORCE_INLINE
Definition: btScalar.h:63
ContactDestroyedCallback gContactDestroyedCallback
ManifoldContactPoint collects and maintains persistent contactpoints.
const btCollisionObject * getBody0() const
#define MANIFOLD_CACHE_SIZE
void setNumContacts(int cachedPoints)
the setNumContacts API is usually not used, except when you gather/fill all contacts manually ...
void * m_userPersistentData
btManifoldPoint & getContactPoint(int index)
btScalar m_appliedImpulseLateral2
btScalar gContactBreakingThreshold
maximum contact breaking and merging threshold
btContactManifoldTypes
btCollisionObject can be used to manage collision detection objects.
btScalar getContactProcessingThreshold() const
const btManifoldPoint & getContactPoint(int index) const
bool(* ContactDestroyedCallback)(void *userPersistentData)
void removeContactPoint(int index)
The btTransform class supports rigid transforms with only translation and rotation and no scaling/she...
Definition: btTransform.h:34
ContactProcessedCallback gContactProcessedCallback
btScalar m_distance1
bool(* ContactProcessedCallback)(btManifoldPoint &cp, void *body0, void *body1)
rudimentary class to provide type info
Definition: btScalar.h:744
#define BT_DECLARE_ALIGNED_ALLOCATOR()
Definition: btScalar.h:388
bool m_lateralFrictionInitialized
void setContactProcessingThreshold(btScalar contactProcessingThreshold)
btPersistentManifold(const btCollisionObject *body0, const btCollisionObject *body1, int, btScalar contactBreakingThreshold, btScalar contactProcessingThreshold)
void replaceContactPoint(const btManifoldPoint &newPoint, int insertIndex)
void setContactBreakingThreshold(btScalar contactBreakingThreshold)
const btCollisionObject * m_body0
this two body pointers can point to the physics rigidbody class.
const btCollisionObject * getBody1() const
void setBodies(const btCollisionObject *body0, const btCollisionObject *body1)
float btScalar
The btScalar type abstracts floating point numbers, to easily switch between double and single floati...
Definition: btScalar.h:278
bool validContactDistance(const btManifoldPoint &pt) const