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 PERSISTENT_MANIFOLD_H
17 #define PERSISTENT_MANIFOLD_H
18 
19 
20 #include "LinearMath/btVector3.h"
21 #include "LinearMath/btTransform.h"
22 #include "btManifoldPoint.h"
23 #include "LinearMath/btAlignedAllocator.h"
24 
25 struct btCollisionResult;
26 
27 ///maximum contact breaking and merging threshold
28 extern btScalar gContactBreakingThreshold;
29 
30 typedef bool (*ContactDestroyedCallback)(void* userPersistentData);
31 typedef bool (*ContactProcessedCallback)(btManifoldPoint& cp,void* body0,void* body1);
32 extern ContactDestroyedCallback	gContactDestroyedCallback;
33 extern ContactProcessedCallback gContactProcessedCallback;
34 
35 
36 enum btContactManifoldTypes
37 {
38 	BT_PERSISTENT_MANIFOLD_TYPE = 1,
39 	MAX_CONTACT_MANIFOLD_TYPE
40 };
41 
42 #define MANIFOLD_CACHE_SIZE 4
43 
44 ///btPersistentManifold is a contact point cache, it stays persistent as long as objects are overlapping in the broadphase.
45 ///Those contact points are created by the collision narrow phase.
46 ///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.
47 ///updates/refreshes old contact points, and throw them away if necessary (distance becomes too large)
48 ///reduces the cache to 4 points, when more then 4 points are added, using following rules:
49 ///the contact point with deepest penetration is always kept, and it tries to maximuze the area covered by the points
50 ///note that some pairs of objects might have more then one contact manifold.
ATTRIBUTE_ALIGNED16(class)51 ATTRIBUTE_ALIGNED16( class) btPersistentManifold : public btTypedObject
52 {
53 
54 	btManifoldPoint m_pointCache[MANIFOLD_CACHE_SIZE];
55 
56 	/// this two body pointers can point to the physics rigidbody class.
57 	/// void* will allow any rigidbody class
58 	void* m_body0;
59 	void* m_body1;
60 	int	m_cachedPoints;
61 
62 	btScalar	m_contactBreakingThreshold;
63 	btScalar	m_contactProcessingThreshold;
64 
65 
66 	/// sort cached points so most isolated points come first
67 	int	sortCachedPoints(const btManifoldPoint& pt);
68 
69 	int		findContactPoint(const btManifoldPoint* unUsed, int numUnused,const btManifoldPoint& pt);
70 
71 public:
72 
73 	BT_DECLARE_ALIGNED_ALLOCATOR();
74 
75 	int m_index1a;
76 
77 	btPersistentManifold();
78 
79 	btPersistentManifold(void* body0,void* body1,int , btScalar contactBreakingThreshold,btScalar contactProcessingThreshold)
80 		: btTypedObject(BT_PERSISTENT_MANIFOLD_TYPE),
81 	m_body0(body0),m_body1(body1),m_cachedPoints(0),
82 		m_contactBreakingThreshold(contactBreakingThreshold),
83 		m_contactProcessingThreshold(contactProcessingThreshold)
84 	{
85 	}
86 
87 	SIMD_FORCE_INLINE void* getBody0() { return m_body0;}
88 	SIMD_FORCE_INLINE void* getBody1() { return m_body1;}
89 
90 	SIMD_FORCE_INLINE const void* getBody0() const { return m_body0;}
91 	SIMD_FORCE_INLINE const void* getBody1() const { return m_body1;}
92 
93 	void	setBodies(void* body0,void* body1)
94 	{
95 		m_body0 = body0;
96 		m_body1 = body1;
97 	}
98 
99 	void clearUserCache(btManifoldPoint& pt);
100 
101 #ifdef DEBUG_PERSISTENCY
102 	void	DebugPersistency();
103 #endif //
104 
105 	SIMD_FORCE_INLINE int	getNumContacts() const { return m_cachedPoints;}
106 
107 	SIMD_FORCE_INLINE const btManifoldPoint& getContactPoint(int index) const
108 	{
109 		btAssert(index < m_cachedPoints);
110 		return m_pointCache[index];
111 	}
112 
113 	SIMD_FORCE_INLINE btManifoldPoint& getContactPoint(int index)
114 	{
115 		btAssert(index < m_cachedPoints);
116 		return m_pointCache[index];
117 	}
118 
119 	///@todo: get this margin from the current physics / collision environment
120 	btScalar	getContactBreakingThreshold() const;
121 
122 	btScalar	getContactProcessingThreshold() const
123 	{
124 		return m_contactProcessingThreshold;
125 	}
126 
127 	int getCacheEntry(const btManifoldPoint& newPoint) const;
128 
129 	int addManifoldPoint( const btManifoldPoint& newPoint);
130 
131 	void removeContactPoint (int index)
132 	{
133 		clearUserCache(m_pointCache[index]);
134 
135 		int lastUsedIndex = getNumContacts() - 1;
136 //		m_pointCache[index] = m_pointCache[lastUsedIndex];
137 		if(index != lastUsedIndex)
138 		{
139 			m_pointCache[index] = m_pointCache[lastUsedIndex];
140 			//get rid of duplicated userPersistentData pointer
141 			m_pointCache[lastUsedIndex].m_userPersistentData = 0;
142 			m_pointCache[lastUsedIndex].m_appliedImpulse = 0.f;
143 			m_pointCache[lastUsedIndex].m_lateralFrictionInitialized = false;
144 			m_pointCache[lastUsedIndex].m_appliedImpulseLateral1 = 0.f;
145 			m_pointCache[lastUsedIndex].m_appliedImpulseLateral2 = 0.f;
146 			m_pointCache[lastUsedIndex].m_lifeTime = 0;
147 		}
148 
149 		btAssert(m_pointCache[lastUsedIndex].m_userPersistentData==0);
150 		m_cachedPoints--;
151 	}
152 	void replaceContactPoint(const btManifoldPoint& newPoint,int insertIndex)
153 	{
154 		btAssert(validContactDistance(newPoint));
155 
156 #define MAINTAIN_PERSISTENCY 1
157 #ifdef MAINTAIN_PERSISTENCY
158 		int	lifeTime = m_pointCache[insertIndex].getLifeTime();
159 		btScalar	appliedImpulse = m_pointCache[insertIndex].m_appliedImpulse;
160 		btScalar	appliedLateralImpulse1 = m_pointCache[insertIndex].m_appliedImpulseLateral1;
161 		btScalar	appliedLateralImpulse2 = m_pointCache[insertIndex].m_appliedImpulseLateral2;
162 
163 		btAssert(lifeTime>=0);
164 		void* cache = m_pointCache[insertIndex].m_userPersistentData;
165 
166 		m_pointCache[insertIndex] = newPoint;
167 
168 		m_pointCache[insertIndex].m_userPersistentData = cache;
169 		m_pointCache[insertIndex].m_appliedImpulse = appliedImpulse;
170 		m_pointCache[insertIndex].m_appliedImpulseLateral1 = appliedLateralImpulse1;
171 		m_pointCache[insertIndex].m_appliedImpulseLateral2 = appliedLateralImpulse2;
172 
173 		m_pointCache[insertIndex].m_lifeTime = lifeTime;
174 #else
175 		clearUserCache(m_pointCache[insertIndex]);
176 		m_pointCache[insertIndex] = newPoint;
177 
178 #endif
179 	}
180 
181 	bool validContactDistance(const btManifoldPoint& pt) const
182 	{
183 		return pt.m_distance1 <= getContactBreakingThreshold();
184 	}
185 	/// calculated new worldspace coordinates and depth, and reject points that exceed the collision margin
186 	void	refreshContactPoints(  const btTransform& trA,const btTransform& trB);
187 
188 
189 	SIMD_FORCE_INLINE	void	clearManifold()
190 	{
191 		int i;
192 		for (i=0;i<m_cachedPoints;i++)
193 		{
194 			clearUserCache(m_pointCache[i]);
195 		}
196 		m_cachedPoints = 0;
197 	}
198 
199 
200 
201 }
202 ;
203 
204 
205 
206 
207 
208 #endif //PERSISTENT_MANIFOLD_H
209