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;
24 #include "LinearMath/btAlignedAllocator.h"
25 
26 struct btCollisionResult;
27 
28 ///maximum contact breaking and merging threshold
29 extern btScalar gContactBreakingThreshold;
30 
31 typedef bool (*ContactDestroyedCallback)(void* userPersistentData);
32 typedef bool (*ContactProcessedCallback)(btManifoldPoint& cp,void* body0,void* body1);
33 extern ContactDestroyedCallback	gContactDestroyedCallback;
34 extern ContactProcessedCallback gContactProcessedCallback;
35 
36 //the enum starts at 1024 to avoid type conflicts with btTypedConstraint
37 enum btContactManifoldTypes
38 {
39 	MIN_CONTACT_MANIFOLD_TYPE = 1024,
40 	BT_PERSISTENT_MANIFOLD_TYPE
41 };
42 
43 #define MANIFOLD_CACHE_SIZE 4
44 
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.
52 
53 
54 //ATTRIBUTE_ALIGNED128( class) btPersistentManifold : public btTypedObject
ATTRIBUTE_ALIGNED16(class)55 ATTRIBUTE_ALIGNED16( class) btPersistentManifold : public btTypedObject
56 {
57 
58 	btManifoldPoint m_pointCache[MANIFOLD_CACHE_SIZE];
59 
60 	/// this two body pointers can point to the physics rigidbody class.
61 	const btCollisionObject* m_body0;
62 	const btCollisionObject* m_body1;
63 
64 	int	m_cachedPoints;
65 
66 	btScalar	m_contactBreakingThreshold;
67 	btScalar	m_contactProcessingThreshold;
68 
69 
70 	/// sort cached points so most isolated points come first
71 	int	sortCachedPoints(const btManifoldPoint& pt);
72 
73 	int		findContactPoint(const btManifoldPoint* unUsed, int numUnused,const btManifoldPoint& pt);
74 
75 public:
76 
77 	BT_DECLARE_ALIGNED_ALLOCATOR();
78 
79 	int	m_companionIdA;
80 	int	m_companionIdB;
81 
82 	int m_index1a;
83 
84 	btPersistentManifold();
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;}
110 	/// the setNumContacts API is usually not used, except when you gather/fill all contacts manually
111 	void setNumContacts(int cachedPoints)
112 	{
113 		m_cachedPoints = cachedPoints;
114 	}
115 
116 
117 	SIMD_FORCE_INLINE const btManifoldPoint& getContactPoint(int index) const
118 	{
119 		btAssert(index < m_cachedPoints);
120 		return m_pointCache[index];
121 	}
122 
123 	SIMD_FORCE_INLINE btManifoldPoint& getContactPoint(int index)
124 	{
125 		btAssert(index < m_cachedPoints);
126 		return m_pointCache[index];
127 	}
128 
129 	///@todo: get this margin from the current physics / collision environment
130 	btScalar	getContactBreakingThreshold() const;
131 
132 	btScalar	getContactProcessingThreshold() const
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_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;
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 		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;
197 
198 
199 		m_pointCache[insertIndex].m_lifeTime = lifeTime;
200 #else
201 		clearUserCache(m_pointCache[insertIndex]);
202 		m_pointCache[insertIndex] = newPoint;
203 
204 #endif
205 	}
206 
207 
208 	bool validContactDistance(const btManifoldPoint& pt) const
209 	{
210 		return pt.m_distance1 <= getContactBreakingThreshold();
211 	}
212 	/// calculated new worldspace coordinates and depth, and reject points that exceed the collision margin
213 	void	refreshContactPoints(  const btTransform& trA,const btTransform& trB);
214 
215 
216 	SIMD_FORCE_INLINE	void	clearManifold()
217 	{
218 		int i;
219 		for (i=0;i<m_cachedPoints;i++)
220 		{
221 			clearUserCache(m_pointCache[i]);
222 		}
223 		m_cachedPoints = 0;
224 	}
225 
226 
227 
228 }
229 ;
230 
231 
232 
233 
234 
235 #endif //BT_PERSISTENT_MANIFOLD_H
236