19
19
#include "BulletCollision/BroadphaseCollision/btDbvt.h"
20
20
#include "LinearMath/btIDebugDraw.h"
21
21
#include "LinearMath/btAabbUtil2.h"
22
#include "btManifoldResult.h"
23
24
btCompoundCollisionAlgorithm::btCompoundCollisionAlgorithm( const btCollisionAlgorithmConstructionInfo& ci,btCollisionObject* body0,btCollisionObject* body1,bool isSwapped)
24
:btCollisionAlgorithm(ci),
25
:btActivatingCollisionAlgorithm(ci,body0,body1),
25
26
m_isSwapped(isSwapped),
26
27
m_sharedManifold(ci.m_manifold)
28
29
m_ownsManifold = false;
30
31
btCollisionObject* colObj = m_isSwapped? body1 : body0;
32
btAssert (colObj->getCollisionShape()->isCompound());
34
btCompoundShape* compoundShape = static_cast<btCompoundShape*>(colObj->getCollisionShape());
35
m_compoundShapeRevision = compoundShape->getUpdateRevision();
37
preallocateChildAlgorithms(body0,body1);
40
void btCompoundCollisionAlgorithm::preallocateChildAlgorithms(btCollisionObject* body0,btCollisionObject* body1)
42
btCollisionObject* colObj = m_isSwapped? body1 : body0;
31
43
btCollisionObject* otherObj = m_isSwapped? body0 : body1;
32
assert (colObj->getCollisionShape()->isCompound());
44
btAssert (colObj->getCollisionShape()->isCompound());
34
46
btCompoundShape* compoundShape = static_cast<btCompoundShape*>(colObj->getCollisionShape());
35
48
int numChildren = compoundShape->getNumChildShapes();
46
59
btCollisionShape* tmpShape = colObj->getCollisionShape();
47
60
btCollisionShape* childShape = compoundShape->getChildShape(i);
48
61
colObj->internalSetTemporaryCollisionShape( childShape );
49
m_childCollisionAlgorithms[i] = ci.m_dispatcher1->findAlgorithm(colObj,otherObj,m_sharedManifold);
62
m_childCollisionAlgorithms[i] = m_dispatcher->findAlgorithm(colObj,otherObj,m_sharedManifold);
50
63
colObj->internalSetTemporaryCollisionShape( tmpShape );
56
btCompoundCollisionAlgorithm::~btCompoundCollisionAlgorithm()
68
void btCompoundCollisionAlgorithm::removeChildAlgorithms()
58
70
int numChildren = m_childCollisionAlgorithms.size();
167
184
btCollisionObject* colObj = m_isSwapped? body1 : body0;
168
185
btCollisionObject* otherObj = m_isSwapped? body0 : body1;
170
assert (colObj->getCollisionShape()->isCompound());
189
btAssert (colObj->getCollisionShape()->isCompound());
171
190
btCompoundShape* compoundShape = static_cast<btCompoundShape*>(colObj->getCollisionShape());
192
///btCompoundShape might have changed:
193
////make sure the internal child collision algorithm caches are still valid
194
if (compoundShape->getUpdateRevision() != m_compoundShapeRevision)
196
///clear and update all
197
removeChildAlgorithms();
199
preallocateChildAlgorithms(body0,body1);
173
203
btDbvt* tree = compoundShape->getDynamicAabbTree();
174
204
//use a dynamic aabb tree to cull potential child-overlaps
175
205
btCompoundLeafCallback callback(colObj,otherObj,m_dispatcher,dispatchInfo,resultOut,&m_childCollisionAlgorithms[0],m_sharedManifold);
207
///we need to refresh all contact manifolds
208
///note that we should actually recursively traverse all children, btCompoundShape can nested more then 1 level deep
209
///so we should add a 'refreshManifolds' in the btCollisionAlgorithm
212
btManifoldArray manifoldArray;
213
for (i=0;i<m_childCollisionAlgorithms.size();i++)
215
if (m_childCollisionAlgorithms[i])
217
m_childCollisionAlgorithms[i]->getAllContactManifolds(manifoldArray);
218
for (int m=0;m<manifoldArray.size();m++)
220
if (manifoldArray[m]->getNumContacts())
222
resultOut->setPersistentManifold(manifoldArray[m]);
223
resultOut->refreshContactPoints();
224
resultOut->setPersistentManifold(0);//??necessary?
227
manifoldArray.clear();
242
296
btCollisionObject* colObj = m_isSwapped? body1 : body0;
243
297
btCollisionObject* otherObj = m_isSwapped? body0 : body1;
245
assert (colObj->getCollisionShape()->isCompound());
299
btAssert (colObj->getCollisionShape()->isCompound());
247
301
btCompoundShape* compoundShape = static_cast<btCompoundShape*>(colObj->getCollisionShape());