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 | |
---|
17 | |
---|
18 | #include "btCollisionDispatcher.h" |
---|
19 | |
---|
20 | |
---|
21 | #include "BulletCollision/BroadphaseCollision/btCollisionAlgorithm.h" |
---|
22 | |
---|
23 | #include "BulletCollision/CollisionShapes/btCollisionShape.h" |
---|
24 | #include "BulletCollision/CollisionDispatch/btCollisionObject.h" |
---|
25 | #include "BulletCollision/BroadphaseCollision/btOverlappingPairCache.h" |
---|
26 | #include "LinearMath/btPoolAllocator.h" |
---|
27 | #include "BulletCollision/CollisionDispatch/btCollisionConfiguration.h" |
---|
28 | |
---|
29 | int gNumManifold = 0; |
---|
30 | |
---|
31 | #ifdef BT_DEBUG |
---|
32 | #include <stdio.h> |
---|
33 | #endif |
---|
34 | |
---|
35 | |
---|
36 | btCollisionDispatcher::btCollisionDispatcher (btCollisionConfiguration* collisionConfiguration): |
---|
37 | m_dispatcherFlags(btCollisionDispatcher::CD_USE_RELATIVE_CONTACT_BREAKING_THRESHOLD), |
---|
38 | m_collisionConfiguration(collisionConfiguration) |
---|
39 | { |
---|
40 | int i; |
---|
41 | |
---|
42 | setNearCallback(defaultNearCallback); |
---|
43 | |
---|
44 | m_collisionAlgorithmPoolAllocator = collisionConfiguration->getCollisionAlgorithmPool(); |
---|
45 | |
---|
46 | m_persistentManifoldPoolAllocator = collisionConfiguration->getPersistentManifoldPool(); |
---|
47 | |
---|
48 | for (i=0;i<MAX_BROADPHASE_COLLISION_TYPES;i++) |
---|
49 | { |
---|
50 | for (int j=0;j<MAX_BROADPHASE_COLLISION_TYPES;j++) |
---|
51 | { |
---|
52 | m_doubleDispatch[i][j] = m_collisionConfiguration->getCollisionAlgorithmCreateFunc(i,j); |
---|
53 | btAssert(m_doubleDispatch[i][j]); |
---|
54 | } |
---|
55 | } |
---|
56 | |
---|
57 | |
---|
58 | } |
---|
59 | |
---|
60 | |
---|
61 | void btCollisionDispatcher::registerCollisionCreateFunc(int proxyType0, int proxyType1, btCollisionAlgorithmCreateFunc *createFunc) |
---|
62 | { |
---|
63 | m_doubleDispatch[proxyType0][proxyType1] = createFunc; |
---|
64 | } |
---|
65 | |
---|
66 | btCollisionDispatcher::~btCollisionDispatcher() |
---|
67 | { |
---|
68 | } |
---|
69 | |
---|
70 | btPersistentManifold* btCollisionDispatcher::getNewManifold(void* b0,void* b1) |
---|
71 | { |
---|
72 | gNumManifold++; |
---|
73 | |
---|
74 | //btAssert(gNumManifold < 65535); |
---|
75 | |
---|
76 | |
---|
77 | btCollisionObject* body0 = (btCollisionObject*)b0; |
---|
78 | btCollisionObject* body1 = (btCollisionObject*)b1; |
---|
79 | |
---|
80 | //optional relative contact breaking threshold, turned on by default (use setDispatcherFlags to switch off feature for improved performance) |
---|
81 | |
---|
82 | btScalar contactBreakingThreshold = (m_dispatcherFlags & btCollisionDispatcher::CD_USE_RELATIVE_CONTACT_BREAKING_THRESHOLD) ? |
---|
83 | btMin(body0->getCollisionShape()->getContactBreakingThreshold(gContactBreakingThreshold) , body1->getCollisionShape()->getContactBreakingThreshold(gContactBreakingThreshold)) |
---|
84 | : gContactBreakingThreshold ; |
---|
85 | |
---|
86 | btScalar contactProcessingThreshold = btMin(body0->getContactProcessingThreshold(),body1->getContactProcessingThreshold()); |
---|
87 | |
---|
88 | void* mem = 0; |
---|
89 | |
---|
90 | if (m_persistentManifoldPoolAllocator->getFreeCount()) |
---|
91 | { |
---|
92 | mem = m_persistentManifoldPoolAllocator->allocate(sizeof(btPersistentManifold)); |
---|
93 | } else |
---|
94 | { |
---|
95 | mem = btAlignedAlloc(sizeof(btPersistentManifold),16); |
---|
96 | |
---|
97 | } |
---|
98 | btPersistentManifold* manifold = new(mem) btPersistentManifold (body0,body1,0,contactBreakingThreshold,contactProcessingThreshold); |
---|
99 | manifold->m_index1a = m_manifoldsPtr.size(); |
---|
100 | m_manifoldsPtr.push_back(manifold); |
---|
101 | |
---|
102 | return manifold; |
---|
103 | } |
---|
104 | |
---|
105 | void btCollisionDispatcher::clearManifold(btPersistentManifold* manifold) |
---|
106 | { |
---|
107 | manifold->clearManifold(); |
---|
108 | } |
---|
109 | |
---|
110 | |
---|
111 | void btCollisionDispatcher::releaseManifold(btPersistentManifold* manifold) |
---|
112 | { |
---|
113 | |
---|
114 | gNumManifold--; |
---|
115 | |
---|
116 | //printf("releaseManifold: gNumManifold %d\n",gNumManifold); |
---|
117 | clearManifold(manifold); |
---|
118 | |
---|
119 | int findIndex = manifold->m_index1a; |
---|
120 | btAssert(findIndex < m_manifoldsPtr.size()); |
---|
121 | m_manifoldsPtr.swap(findIndex,m_manifoldsPtr.size()-1); |
---|
122 | m_manifoldsPtr[findIndex]->m_index1a = findIndex; |
---|
123 | m_manifoldsPtr.pop_back(); |
---|
124 | |
---|
125 | manifold->~btPersistentManifold(); |
---|
126 | if (m_persistentManifoldPoolAllocator->validPtr(manifold)) |
---|
127 | { |
---|
128 | m_persistentManifoldPoolAllocator->freeMemory(manifold); |
---|
129 | } else |
---|
130 | { |
---|
131 | btAlignedFree(manifold); |
---|
132 | } |
---|
133 | |
---|
134 | } |
---|
135 | |
---|
136 | |
---|
137 | |
---|
138 | btCollisionAlgorithm* btCollisionDispatcher::findAlgorithm(btCollisionObject* body0,btCollisionObject* body1,btPersistentManifold* sharedManifold) |
---|
139 | { |
---|
140 | |
---|
141 | btCollisionAlgorithmConstructionInfo ci; |
---|
142 | |
---|
143 | ci.m_dispatcher1 = this; |
---|
144 | ci.m_manifold = sharedManifold; |
---|
145 | btCollisionAlgorithm* algo = m_doubleDispatch[body0->getCollisionShape()->getShapeType()][body1->getCollisionShape()->getShapeType()]->CreateCollisionAlgorithm(ci,body0,body1); |
---|
146 | |
---|
147 | return algo; |
---|
148 | } |
---|
149 | |
---|
150 | |
---|
151 | |
---|
152 | |
---|
153 | bool btCollisionDispatcher::needsResponse(btCollisionObject* body0,btCollisionObject* body1) |
---|
154 | { |
---|
155 | //here you can do filtering |
---|
156 | bool hasResponse = |
---|
157 | (body0->hasContactResponse() && body1->hasContactResponse()); |
---|
158 | //no response between two static/kinematic bodies: |
---|
159 | hasResponse = hasResponse && |
---|
160 | ((!body0->isStaticOrKinematicObject()) ||(! body1->isStaticOrKinematicObject())); |
---|
161 | return hasResponse; |
---|
162 | } |
---|
163 | |
---|
164 | bool btCollisionDispatcher::needsCollision(btCollisionObject* body0,btCollisionObject* body1) |
---|
165 | { |
---|
166 | btAssert(body0); |
---|
167 | btAssert(body1); |
---|
168 | |
---|
169 | bool needsCollision = true; |
---|
170 | |
---|
171 | #ifdef BT_DEBUG |
---|
172 | if (!(m_dispatcherFlags & btCollisionDispatcher::CD_STATIC_STATIC_REPORTED)) |
---|
173 | { |
---|
174 | //broadphase filtering already deals with this |
---|
175 | if ((body0->isStaticObject() || body0->isKinematicObject()) && |
---|
176 | (body1->isStaticObject() || body1->isKinematicObject())) |
---|
177 | { |
---|
178 | m_dispatcherFlags |= btCollisionDispatcher::CD_STATIC_STATIC_REPORTED; |
---|
179 | printf("warning btCollisionDispatcher::needsCollision: static-static collision!\n"); |
---|
180 | } |
---|
181 | } |
---|
182 | #endif //BT_DEBUG |
---|
183 | |
---|
184 | if ((!body0->isActive()) && (!body1->isActive())) |
---|
185 | needsCollision = false; |
---|
186 | else if (!body0->checkCollideWith(body1)) |
---|
187 | needsCollision = false; |
---|
188 | |
---|
189 | return needsCollision ; |
---|
190 | |
---|
191 | } |
---|
192 | |
---|
193 | |
---|
194 | |
---|
195 | ///interface for iterating all overlapping collision pairs, no matter how those pairs are stored (array, set, map etc) |
---|
196 | ///this is useful for the collision dispatcher. |
---|
197 | class btCollisionPairCallback : public btOverlapCallback |
---|
198 | { |
---|
199 | const btDispatcherInfo& m_dispatchInfo; |
---|
200 | btCollisionDispatcher* m_dispatcher; |
---|
201 | |
---|
202 | public: |
---|
203 | |
---|
204 | btCollisionPairCallback(const btDispatcherInfo& dispatchInfo,btCollisionDispatcher* dispatcher) |
---|
205 | :m_dispatchInfo(dispatchInfo), |
---|
206 | m_dispatcher(dispatcher) |
---|
207 | { |
---|
208 | } |
---|
209 | |
---|
210 | /*btCollisionPairCallback& operator=(btCollisionPairCallback& other) |
---|
211 | { |
---|
212 | m_dispatchInfo = other.m_dispatchInfo; |
---|
213 | m_dispatcher = other.m_dispatcher; |
---|
214 | return *this; |
---|
215 | } |
---|
216 | */ |
---|
217 | |
---|
218 | |
---|
219 | virtual ~btCollisionPairCallback() {} |
---|
220 | |
---|
221 | |
---|
222 | virtual bool processOverlap(btBroadphasePair& pair) |
---|
223 | { |
---|
224 | (*m_dispatcher->getNearCallback())(pair,*m_dispatcher,m_dispatchInfo); |
---|
225 | |
---|
226 | return false; |
---|
227 | } |
---|
228 | }; |
---|
229 | |
---|
230 | |
---|
231 | |
---|
232 | void btCollisionDispatcher::dispatchAllCollisionPairs(btOverlappingPairCache* pairCache,const btDispatcherInfo& dispatchInfo,btDispatcher* dispatcher) |
---|
233 | { |
---|
234 | //m_blockedForChanges = true; |
---|
235 | |
---|
236 | btCollisionPairCallback collisionCallback(dispatchInfo,this); |
---|
237 | |
---|
238 | pairCache->processAllOverlappingPairs(&collisionCallback,dispatcher); |
---|
239 | |
---|
240 | //m_blockedForChanges = false; |
---|
241 | |
---|
242 | } |
---|
243 | |
---|
244 | |
---|
245 | |
---|
246 | |
---|
247 | //by default, Bullet will use this near callback |
---|
248 | void btCollisionDispatcher::defaultNearCallback(btBroadphasePair& collisionPair, btCollisionDispatcher& dispatcher, const btDispatcherInfo& dispatchInfo) |
---|
249 | { |
---|
250 | btCollisionObject* colObj0 = (btCollisionObject*)collisionPair.m_pProxy0->m_clientObject; |
---|
251 | btCollisionObject* colObj1 = (btCollisionObject*)collisionPair.m_pProxy1->m_clientObject; |
---|
252 | |
---|
253 | if (dispatcher.needsCollision(colObj0,colObj1)) |
---|
254 | { |
---|
255 | //dispatcher will keep algorithms persistent in the collision pair |
---|
256 | if (!collisionPair.m_algorithm) |
---|
257 | { |
---|
258 | collisionPair.m_algorithm = dispatcher.findAlgorithm(colObj0,colObj1); |
---|
259 | } |
---|
260 | |
---|
261 | if (collisionPair.m_algorithm) |
---|
262 | { |
---|
263 | btManifoldResult contactPointResult(colObj0,colObj1); |
---|
264 | |
---|
265 | if (dispatchInfo.m_dispatchFunc == btDispatcherInfo::DISPATCH_DISCRETE) |
---|
266 | { |
---|
267 | //discrete collision detection query |
---|
268 | collisionPair.m_algorithm->processCollision(colObj0,colObj1,dispatchInfo,&contactPointResult); |
---|
269 | } else |
---|
270 | { |
---|
271 | //continuous collision detection query, time of impact (toi) |
---|
272 | btScalar toi = collisionPair.m_algorithm->calculateTimeOfImpact(colObj0,colObj1,dispatchInfo,&contactPointResult); |
---|
273 | if (dispatchInfo.m_timeOfImpact > toi) |
---|
274 | dispatchInfo.m_timeOfImpact = toi; |
---|
275 | |
---|
276 | } |
---|
277 | } |
---|
278 | } |
---|
279 | |
---|
280 | } |
---|
281 | |
---|
282 | |
---|
283 | void* btCollisionDispatcher::allocateCollisionAlgorithm(int size) |
---|
284 | { |
---|
285 | if (m_collisionAlgorithmPoolAllocator->getFreeCount()) |
---|
286 | { |
---|
287 | return m_collisionAlgorithmPoolAllocator->allocate(size); |
---|
288 | } |
---|
289 | |
---|
290 | //warn user for overflow? |
---|
291 | return btAlignedAlloc(static_cast<size_t>(size), 16); |
---|
292 | } |
---|
293 | |
---|
294 | void btCollisionDispatcher::freeCollisionAlgorithm(void* ptr) |
---|
295 | { |
---|
296 | if (m_collisionAlgorithmPoolAllocator->validPtr(ptr)) |
---|
297 | { |
---|
298 | m_collisionAlgorithmPoolAllocator->freeMemory(ptr); |
---|
299 | } else |
---|
300 | { |
---|
301 | btAlignedFree(ptr); |
---|
302 | } |
---|
303 | } |
---|