Bullet Collision Detection & Physics Library
btCollisionDispatcher.cpp
Go to the documentation of this file.
1/*
2Bullet Continuous Collision Detection and Physics Library
3Copyright (c) 2003-2006 Erwin Coumans http://continuousphysics.com/Bullet/
4
5This software is provided 'as-is', without any express or implied warranty.
6In no event will the authors be held liable for any damages arising from the use of this software.
7Permission is granted to anyone to use this software for any purpose,
8including commercial applications, and to alter it and redistribute it freely,
9subject to the following restrictions:
10
111. 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.
122. Altered source versions must be plainly marked as such, and must not be misrepresented as being the original software.
133. This notice may not be removed or altered from any source distribution.
14*/
15
16
17
20
22
29
31
32#ifdef BT_DEBUG
33#include <stdio.h>
34#endif
35
36
38m_dispatcherFlags(btCollisionDispatcher::CD_USE_RELATIVE_CONTACT_BREAKING_THRESHOLD),
39 m_collisionConfiguration(collisionConfiguration)
40{
41 int i;
42
44
45 m_collisionAlgorithmPoolAllocator = collisionConfiguration->getCollisionAlgorithmPool();
46
47 m_persistentManifoldPoolAllocator = collisionConfiguration->getPersistentManifoldPool();
48
49 for (i=0;i<MAX_BROADPHASE_COLLISION_TYPES;i++)
50 {
52 {
56
57 }
58 }
59
60
61}
62
63
65{
67}
68
70{
72}
73
75{
76}
77
79{
81
82 //btAssert(gNumManifold < 65535);
83
84
85
86 //optional relative contact breaking threshold, turned on by default (use setDispatcherFlags to switch off feature for improved performance)
87
89 btMin(body0->getCollisionShape()->getContactBreakingThreshold(gContactBreakingThreshold) , body1->getCollisionShape()->getContactBreakingThreshold(gContactBreakingThreshold))
91
92 btScalar contactProcessingThreshold = btMin(body0->getContactProcessingThreshold(),body1->getContactProcessingThreshold());
93
95 if (NULL == mem)
96 {
97 //we got a pool memory overflow, by default we fallback to dynamically allocate memory. If we require a contiguous contact pool then assert.
99 {
101 } else
102 {
103 btAssert(0);
104 //make sure to increase the m_defaultMaxPersistentManifoldPoolSize in the btDefaultCollisionConstructionInfo/btDefaultCollisionConfiguration
105 return 0;
106 }
107 }
111
112 return manifold;
113}
114
116{
117 manifold->clearManifold();
118}
119
120
122{
123
124 gNumManifold--;
125
126 //printf("releaseManifold: gNumManifold %d\n",gNumManifold);
128
129 int findIndex = manifold->m_index1a;
130 btAssert(findIndex < m_manifoldsPtr.size());
131 m_manifoldsPtr.swap(findIndex,m_manifoldsPtr.size()-1);
132 m_manifoldsPtr[findIndex]->m_index1a = findIndex;
134
135 manifold->~btPersistentManifold();
137 {
139 } else
140 {
142 }
143
144}
145
146
147
148
150{
151
153
154 ci.m_dispatcher1 = this;
155 ci.m_manifold = sharedManifold;
158 {
160 }
161 else
162 {
164 }
165
166 return algo;
167}
168
169
170
171
173{
174 //here you can do filtering
175 bool hasResponse =
176 (body0->hasContactResponse() && body1->hasContactResponse());
177 //no response between two static/kinematic bodies:
179 ((!body0->isStaticOrKinematicObject()) ||(! body1->isStaticOrKinematicObject()));
180 return hasResponse;
181}
182
184{
187
188 bool needsCollision = true;
189
190#ifdef BT_DEBUG
192 {
193 //broadphase filtering already deals with this
194 if (body0->isStaticOrKinematicObject() && body1->isStaticOrKinematicObject())
195 {
197 printf("warning btCollisionDispatcher::needsCollision: static-static collision!\n");
198 }
199 }
200#endif //BT_DEBUG
201
202 if ((!body0->isActive()) && (!body1->isActive()))
203 needsCollision = false;
204 else if ((!body0->checkCollideWith(body1)) || (!body1->checkCollideWith(body0)))
205 needsCollision = false;
206
207 return needsCollision ;
208
209}
210
211
212
216{
219
220public:
221
225 {
226 }
227
228 /*btCollisionPairCallback& operator=(btCollisionPairCallback& other)
229 {
230 m_dispatchInfo = other.m_dispatchInfo;
231 m_dispatcher = other.m_dispatcher;
232 return *this;
233 }
234 */
235
236
238
239
241 {
243 return false;
244 }
245};
246
247
248
250{
251 //m_blockedForChanges = true;
252
254
255 pairCache->processAllOverlappingPairs(&collisionCallback,dispatcher);
256
257 //m_blockedForChanges = false;
258
259}
260
261
262
263//by default, Bullet will use this near callback
265{
266 btCollisionObject* colObj0 = (btCollisionObject*)collisionPair.m_pProxy0->m_clientObject;
267 btCollisionObject* colObj1 = (btCollisionObject*)collisionPair.m_pProxy1->m_clientObject;
268
269 if (dispatcher.needsCollision(colObj0,colObj1))
270 {
271 btCollisionObjectWrapper obj0Wrap(0,colObj0->getCollisionShape(),colObj0,colObj0->getWorldTransform(),-1,-1);
272 btCollisionObjectWrapper obj1Wrap(0,colObj1->getCollisionShape(),colObj1,colObj1->getWorldTransform(),-1,-1);
273
274
275 //dispatcher will keep algorithms persistent in the collision pair
276 if (!collisionPair.m_algorithm)
277 {
278 collisionPair.m_algorithm = dispatcher.findAlgorithm(&obj0Wrap,&obj1Wrap,0, BT_CONTACT_POINT_ALGORITHMS);
279 }
280
281 if (collisionPair.m_algorithm)
282 {
284
286 {
287 //discrete collision detection query
288
289 collisionPair.m_algorithm->processCollision(&obj0Wrap,&obj1Wrap,dispatchInfo,&contactPointResult);
290 } else
291 {
292 //continuous collision detection query, time of impact (toi)
293 btScalar toi = collisionPair.m_algorithm->calculateTimeOfImpact(colObj0,colObj1,dispatchInfo,&contactPointResult);
294 if (dispatchInfo.m_timeOfImpact > toi)
295 dispatchInfo.m_timeOfImpact = toi;
296
297 }
298 }
299 }
300
301}
302
303
305{
307 if (NULL == mem)
308 {
309 //warn user for overflow?
310 return btAlignedAlloc(static_cast<size_t>(size), 16);
311 }
312 return mem;
313}
314
316{
318 {
320 } else
321 {
323 }
324}
#define btAlignedFree(ptr)
#define btAlignedAlloc(size, alignment)
@ MAX_BROADPHASE_COLLISION_TYPES
int gNumManifold
btScalar gContactBreakingThreshold
static DBVT_INLINE btScalar size(const btDbvtVolume &a)
Definition btDbvt.cpp:52
ebtDispatcherQueryType
@ BT_CONTACT_POINT_ALGORITHMS
const T & btMax(const T &a, const T &b)
Definition btMinMax.h:29
const T & btMin(const T &a, const T &b)
Definition btMinMax.h:23
float btScalar
The btScalar type abstracts floating point numbers, to easily switch between double and single floati...
Definition btScalar.h:292
#define btAssert(x)
Definition btScalar.h:131
int size() const
return the number of elements in the array
void swap(int index0, int index1)
void push_back(const T &_Val)
btCollisionAlgorithm is an collision interface that is compatible with the Broadphase and btDispatche...
btCollisionConfiguration allows to configure Bullet collision detection stack allocator size,...
virtual btCollisionAlgorithmCreateFunc * getCollisionAlgorithmCreateFunc(int proxyType0, int proxyType1)=0
virtual btCollisionAlgorithmCreateFunc * getClosestPointsAlgorithmCreateFunc(int proxyType0, int proxyType1)=0
btCollisionDispatcher supports algorithms that handle ConvexConvex and ConvexConcave collision pairs.
btCollisionAlgorithmCreateFunc * m_doubleDispatchClosestPoints[MAX_BROADPHASE_COLLISION_TYPES][MAX_BROADPHASE_COLLISION_TYPES]
virtual void releaseManifold(btPersistentManifold *manifold)
void registerClosestPointsCreateFunc(int proxyType0, int proxyType1, btCollisionAlgorithmCreateFunc *createFunc)
btCollisionConfiguration * m_collisionConfiguration
virtual bool needsCollision(const btCollisionObject *body0, const btCollisionObject *body1)
btCollisionDispatcher(btCollisionConfiguration *collisionConfiguration)
btNearCallback getNearCallback() const
void setNearCallback(btNearCallback nearCallback)
virtual btPersistentManifold * getNewManifold(const btCollisionObject *b0, const btCollisionObject *b1)
virtual void freeCollisionAlgorithm(void *ptr)
btCollisionAlgorithmCreateFunc * m_doubleDispatchContactPoints[MAX_BROADPHASE_COLLISION_TYPES][MAX_BROADPHASE_COLLISION_TYPES]
static void defaultNearCallback(btBroadphasePair &collisionPair, btCollisionDispatcher &dispatcher, const btDispatcherInfo &dispatchInfo)
virtual void dispatchAllCollisionPairs(btOverlappingPairCache *pairCache, const btDispatcherInfo &dispatchInfo, btDispatcher *dispatcher)
btPoolAllocator * m_collisionAlgorithmPoolAllocator
virtual void clearManifold(btPersistentManifold *manifold)
virtual bool needsResponse(const btCollisionObject *body0, const btCollisionObject *body1)
btCollisionAlgorithm * findAlgorithm(const btCollisionObjectWrapper *body0Wrap, const btCollisionObjectWrapper *body1Wrap, btPersistentManifold *sharedManifold, ebtDispatcherQueryType queryType)
virtual void * allocateCollisionAlgorithm(int size)
btAlignedObjectArray< btPersistentManifold * > m_manifoldsPtr
btPoolAllocator * m_persistentManifoldPoolAllocator
void registerCollisionCreateFunc(int proxyType0, int proxyType1, btCollisionAlgorithmCreateFunc *createFunc)
registerCollisionCreateFunc allows registration of custom/alternative collision create functions
btCollisionObject can be used to manage collision detection objects.
interface for iterating all overlapping collision pairs, no matter how those pairs are stored (array,...
virtual bool processOverlap(btBroadphasePair &pair)
btCollisionDispatcher * m_dispatcher
const btDispatcherInfo & m_dispatchInfo
btCollisionPairCallback(const btDispatcherInfo &dispatchInfo, btCollisionDispatcher *dispatcher)
int getShapeType() const
The btDispatcher interface class can be used in combination with broadphase to dispatch calculations ...
btManifoldResult is a helper class to manage contact results.
The btOverlappingPairCache provides an interface for overlapping pair management (add,...
btPersistentManifold is a contact point cache, it stays persistent as long as objects are overlapping...
void freeMemory(void *ptr)
bool validPtr(void *ptr)
void * allocate(int size)
The btBroadphasePair class contains a pair of aabb-overlapping objects.
Used by the btCollisionDispatcher to register and create instances for btCollisionAlgorithm.
virtual btCollisionAlgorithm * CreateCollisionAlgorithm(btCollisionAlgorithmConstructionInfo &, const btCollisionObjectWrapper *body0Wrap, const btCollisionObjectWrapper *body1Wrap)
const btCollisionShape * getCollisionShape() const