Bullet 2.85 update
[Torque-3d.git] / Engine / lib / bullet / src / BulletCollision / NarrowPhaseCollision / btPersistentManifold.h
blobd220f2993abcb16d8ea900d1775a980460efe254
1 /*
2 Bullet Continuous Collision Detection and Physics Library
3 Copyright (c) 2003-2006 Erwin Coumans http://continuousphysics.com/Bullet/
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:
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.
16 #ifndef BT_PERSISTENT_MANIFOLD_H
17 #define BT_PERSISTENT_MANIFOLD_H
20 #include "LinearMath/btVector3.h"
21 #include "LinearMath/btTransform.h"
22 #include "btManifoldPoint.h"
23 class btCollisionObject;
24 #include "LinearMath/btAlignedAllocator.h"
26 struct btCollisionResult;
28 ///maximum contact breaking and merging threshold
29 extern btScalar gContactBreakingThreshold;
31 typedef bool (*ContactDestroyedCallback)(void* userPersistentData);
32 typedef bool (*ContactProcessedCallback)(btManifoldPoint& cp,void* body0,void* body1);
33 extern ContactDestroyedCallback gContactDestroyedCallback;
34 extern ContactProcessedCallback gContactProcessedCallback;
36 //the enum starts at 1024 to avoid type conflicts with btTypedConstraint
37 enum btContactManifoldTypes
39 MIN_CONTACT_MANIFOLD_TYPE = 1024,
40 BT_PERSISTENT_MANIFOLD_TYPE
43 #define MANIFOLD_CACHE_SIZE 4
45 ///btPersistentManifold is a contact point cache, it stays persistent as long as objects are overlapping in the broadphase.
46 ///Those contact points are created by the collision narrow phase.
47 ///The cache can be empty, or hold 1,2,3 or 4 points. Some collision algorithms (GJK) might only add one point at a time.
48 ///updates/refreshes old contact points, and throw them away if necessary (distance becomes too large)
49 ///reduces the cache to 4 points, when more then 4 points are added, using following rules:
50 ///the contact point with deepest penetration is always kept, and it tries to maximuze the area covered by the points
51 ///note that some pairs of objects might have more then one contact manifold.
54 ATTRIBUTE_ALIGNED128( class) btPersistentManifold : public btTypedObject
55 //ATTRIBUTE_ALIGNED16( class) btPersistentManifold : public btTypedObject
58 btManifoldPoint m_pointCache[MANIFOLD_CACHE_SIZE];
60 /// this two body pointers can point to the physics rigidbody class.
61 const btCollisionObject* m_body0;
62 const btCollisionObject* m_body1;
64 int m_cachedPoints;
66 btScalar m_contactBreakingThreshold;
67 btScalar m_contactProcessingThreshold;
70 /// sort cached points so most isolated points come first
71 int sortCachedPoints(const btManifoldPoint& pt);
73 int findContactPoint(const btManifoldPoint* unUsed, int numUnused,const btManifoldPoint& pt);
75 public:
77 BT_DECLARE_ALIGNED_ALLOCATOR();
79 int m_companionIdA;
80 int m_companionIdB;
82 int m_index1a;
84 btPersistentManifold();
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)
94 SIMD_FORCE_INLINE const btCollisionObject* getBody0() const { return m_body0;}
95 SIMD_FORCE_INLINE const btCollisionObject* getBody1() const { return m_body1;}
97 void setBodies(const btCollisionObject* body0,const btCollisionObject* body1)
99 m_body0 = body0;
100 m_body1 = body1;
103 void clearUserCache(btManifoldPoint& pt);
105 #ifdef DEBUG_PERSISTENCY
106 void DebugPersistency();
107 #endif //
109 SIMD_FORCE_INLINE int getNumContacts() const { return m_cachedPoints;}
110 /// the setNumContacts API is usually not used, except when you gather/fill all contacts manually
111 void setNumContacts(int cachedPoints)
113 m_cachedPoints = cachedPoints;
117 SIMD_FORCE_INLINE const btManifoldPoint& getContactPoint(int index) const
119 btAssert(index < m_cachedPoints);
120 return m_pointCache[index];
123 SIMD_FORCE_INLINE btManifoldPoint& getContactPoint(int index)
125 btAssert(index < m_cachedPoints);
126 return m_pointCache[index];
129 ///@todo: get this margin from the current physics / collision environment
130 btScalar getContactBreakingThreshold() const;
132 btScalar getContactProcessingThreshold() const
134 return m_contactProcessingThreshold;
137 void setContactBreakingThreshold(btScalar contactBreakingThreshold)
139 m_contactBreakingThreshold = contactBreakingThreshold;
142 void setContactProcessingThreshold(btScalar contactProcessingThreshold)
144 m_contactProcessingThreshold = contactProcessingThreshold;
150 int getCacheEntry(const btManifoldPoint& newPoint) const;
152 int addManifoldPoint( const btManifoldPoint& newPoint, bool isPredictive=false);
154 void removeContactPoint (int index)
156 clearUserCache(m_pointCache[index]);
158 int lastUsedIndex = getNumContacts() - 1;
159 // m_pointCache[index] = m_pointCache[lastUsedIndex];
160 if(index != lastUsedIndex)
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_contactPointFlags = 0;
167 m_pointCache[lastUsedIndex].m_appliedImpulseLateral1 = 0.f;
168 m_pointCache[lastUsedIndex].m_appliedImpulseLateral2 = 0.f;
169 m_pointCache[lastUsedIndex].m_lifeTime = 0;
172 btAssert(m_pointCache[lastUsedIndex].m_userPersistentData==0);
173 m_cachedPoints--;
175 void replaceContactPoint(const btManifoldPoint& newPoint,int insertIndex)
177 btAssert(validContactDistance(newPoint));
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;
189 btAssert(lifeTime>=0);
190 void* cache = m_pointCache[insertIndex].m_userPersistentData;
192 m_pointCache[insertIndex] = newPoint;
193 m_pointCache[insertIndex].m_userPersistentData = cache;
194 m_pointCache[insertIndex].m_appliedImpulse = appliedImpulse;
195 m_pointCache[insertIndex].m_appliedImpulseLateral1 = appliedLateralImpulse1;
196 m_pointCache[insertIndex].m_appliedImpulseLateral2 = appliedLateralImpulse2;
199 m_pointCache[insertIndex].m_lifeTime = lifeTime;
200 #else
201 clearUserCache(m_pointCache[insertIndex]);
202 m_pointCache[insertIndex] = newPoint;
204 #endif
208 bool validContactDistance(const btManifoldPoint& pt) const
210 return pt.m_distance1 <= getContactBreakingThreshold();
212 /// calculated new worldspace coordinates and depth, and reject points that exceed the collision margin
213 void refreshContactPoints( const btTransform& trA,const btTransform& trB);
216 SIMD_FORCE_INLINE void clearManifold()
218 int i;
219 for (i=0;i<m_cachedPoints;i++)
221 clearUserCache(m_pointCache[i]);
223 m_cachedPoints = 0;
235 #endif //BT_PERSISTENT_MANIFOLD_H