Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/libraries2/src/bullet/BulletCollision/CollisionDispatch/btCollisionDispatcher.cpp @ 5703

Last change on this file since 5703 was 2882, checked in by rgrieder, 16 years ago

Update from Bullet 2.73 to 2.74.

  • Property svn:eol-style set to native
File size: 8.8 KB
Line 
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
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
29int gNumManifold = 0;
30
31#ifdef BT_DEBUG
32#include <stdio.h>
33#endif
34
35
36btCollisionDispatcher::btCollisionDispatcher (btCollisionConfiguration* collisionConfiguration): 
37        m_count(0),
38        m_useIslands(true),
39        m_staticWarningReported(false),
40        m_collisionConfiguration(collisionConfiguration)
41{
42        int i;
43
44        setNearCallback(defaultNearCallback);
45       
46        m_collisionAlgorithmPoolAllocator = collisionConfiguration->getCollisionAlgorithmPool();
47
48        m_persistentManifoldPoolAllocator = collisionConfiguration->getPersistentManifoldPool();
49
50        for (i=0;i<MAX_BROADPHASE_COLLISION_TYPES;i++)
51        {
52                for (int j=0;j<MAX_BROADPHASE_COLLISION_TYPES;j++)
53                {
54                        m_doubleDispatch[i][j] = m_collisionConfiguration->getCollisionAlgorithmCreateFunc(i,j);
55                        btAssert(m_doubleDispatch[i][j]);
56                }
57        }
58       
59       
60}
61
62
63void btCollisionDispatcher::registerCollisionCreateFunc(int proxyType0, int proxyType1, btCollisionAlgorithmCreateFunc *createFunc)
64{
65        m_doubleDispatch[proxyType0][proxyType1] = createFunc;
66}
67
68btCollisionDispatcher::~btCollisionDispatcher()
69{
70}
71
72btPersistentManifold*   btCollisionDispatcher::getNewManifold(void* b0,void* b1) 
73{ 
74        gNumManifold++;
75       
76        //btAssert(gNumManifold < 65535);
77       
78
79        btCollisionObject* body0 = (btCollisionObject*)b0;
80        btCollisionObject* body1 = (btCollisionObject*)b1;
81
82        //test for Bullet 2.74: use a relative contact breaking threshold without clamping against 'gContactBreakingThreshold'
83        //btScalar contactBreakingThreshold = btMin(gContactBreakingThreshold,btMin(body0->getCollisionShape()->getContactBreakingThreshold(),body1->getCollisionShape()->getContactBreakingThreshold()));
84        btScalar contactBreakingThreshold = btMin(body0->getCollisionShape()->getContactBreakingThreshold(),body1->getCollisionShape()->getContactBreakingThreshold());
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
105void btCollisionDispatcher::clearManifold(btPersistentManifold* manifold)
106{
107        manifold->clearManifold();
108}
109
110       
111void 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
138btCollisionAlgorithm* 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
153bool    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
164bool    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_staticWarningReported)
173        {
174                //broadphase filtering already deals with this
175                if ((body0->isStaticObject() || body0->isKinematicObject()) &&
176                        (body1->isStaticObject() || body1->isKinematicObject()))
177                {
178                        m_staticWarningReported = true;
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.
197class btCollisionPairCallback : public btOverlapCallback
198{
199        const btDispatcherInfo& m_dispatchInfo;
200        btCollisionDispatcher*  m_dispatcher;
201
202public:
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
232void    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
248void 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
283void* 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
294void 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}
Note: See TracBrowser for help on using the repository browser.