22 #include <BulletCollision/CollisionDispatch/btCollisionObject.h>
23 #include <BulletCollision/CollisionShapes/btCompoundShape.h>
24 #include <BulletCollision/CollisionShapes/btCollisionShape.h>
25 #include <BulletCollision/BroadphaseCollision/btDbvt.h>
26 #include <LinearMath/btIDebugDraw.h>
27 #include <LinearMath/btAabbUtil2.h>
28 #include <BulletCollision/CollisionDispatch/btManifoldResult.h>
29 #include <BulletCollision/CollisionDispatch/btCollisionObjectWrapper.h>
41 const btCollisionObjectWrapper* body0Wrap,
42 const btCollisionObjectWrapper* body1Wrap,
44 : btActivatingCollisionAlgorithm(ci, body0Wrap, body1Wrap), m_isSwapped(isSwapped), m_sharedManifold(ci.m_manifold)
46 const btCollisionObjectWrapper* colObjWrap =
m_isSwapped ? body1Wrap : body0Wrap;
47 btAssert(colObjWrap->getCollisionShape()->isCompound());
49 const auto* compoundShape =
static_cast<const btCompoundShape*
>(colObjWrap->getCollisionShape());
56 const btCollisionObjectWrapper* body1Wrap)
58 const btCollisionObjectWrapper* colObjWrap =
m_isSwapped ? body1Wrap : body0Wrap;
59 const btCollisionObjectWrapper* otherObjWrap =
m_isSwapped ? body0Wrap : body1Wrap;
60 btAssert(colObjWrap->getCollisionShape()->isCompound());
62 const auto* compoundShape =
static_cast<const btCompoundShape*
>(colObjWrap->getCollisionShape());
64 int numChildren = compoundShape->getNumChildShapes();
67 for (
int i = 0; i < numChildren; i++)
69 if (compoundShape->getDynamicAabbTree() !=
nullptr)
75 const btCollisionShape* childShape = compoundShape->getChildShape(i);
77 btCollisionObjectWrapper childWrap(colObjWrap,
79 colObjWrap->getCollisionObject(),
80 colObjWrap->getWorldTransform(),
84 m_dispatcher->findAlgorithm(&childWrap, otherObjWrap,
m_sharedManifold, BT_CONTACT_POINT_ALGORITHMS);
86 btAlignedObjectArray<btCollisionAlgorithm*> m_childCollisionAlgorithmsContact;
87 btAlignedObjectArray<btCollisionAlgorithm*> m_childCollisionAlgorithmsClosestPoints;
95 for (
int i = 0; i < numChildren; i++)
120 const btCollisionObjectWrapper* otherObjWrap,
121 btDispatcher* dispatcher,
122 const btDispatcherInfo& dispatchInfo,
123 btManifoldResult* resultOut,
124 btCollisionAlgorithm** childCollisionAlgorithms,
125 btPersistentManifold* sharedManifold)
139 btAssert(index >= 0);
140 const auto* compoundShape =
static_cast<const btCompoundShape*
>(
m_compoundColObjWrap->getCollisionShape());
141 btAssert(index < compoundShape->getNumChildShapes());
149 const btTransform& childTrans = compoundShape->getChildTransform(index);
150 btTransform newChildWorldTrans = orgTrans * childTrans;
153 btVector3 aabbMin0, aabbMax0;
154 childShape->getAabb(newChildWorldTrans, aabbMin0, aabbMax0);
156 btVector3 extendAabb(
m_resultOut->m_closestPointDistanceThreshold,
159 aabbMin0 -= extendAabb;
160 aabbMax0 += extendAabb;
162 btVector3 aabbMin1, aabbMax1;
165 if (TestAabbAgainstAabb2(aabbMin0, aabbMax0, aabbMin1, aabbMax1))
167 #if BT_BULLET_VERSION >= 300
168 btTransform preTransform = childTrans;
169 if (this->m_compoundColObjWrap->m_preTransform !=
nullptr)
171 preTransform = preTransform * (*(this->m_compoundColObjWrap->m_preTransform));
173 btCollisionObjectWrapper compoundWrap(this->m_compoundColObjWrap,
181 btCollisionObjectWrapper compoundWrap(this->m_compoundColObjWrap,
189 btCollisionAlgorithm* algo =
nullptr;
190 bool allocatedAlgorithm =
false;
192 if (
m_resultOut->m_closestPointDistanceThreshold > 0)
195 allocatedAlgorithm =
true;
208 const btCollisionObjectWrapper* tmpWrap =
nullptr;
229 btVector3 worldAabbMin,worldAabbMax;
230 m_dispatchInfo.m_debugDraw->drawAabb(aabbMin0,aabbMax0,btVector3(1,1,1));
231 m_dispatchInfo.m_debugDraw->drawAabb(aabbMin1,aabbMax1,btVector3(1,1,1));
243 if (allocatedAlgorithm)
245 algo->~btCollisionAlgorithm();
252 int index = leaf->dataAsInt;
254 const auto* compoundShape =
static_cast<const btCompoundShape*
>(
m_compoundColObjWrap->getCollisionShape());
255 const btCollisionShape* childShape = compoundShape->getChildShape(index);
260 btVector3 worldAabbMin,worldAabbMax;
262 btTransformAabb(leaf->volume.Mins(),leaf->volume.Maxs(),0.,orgTrans,worldAabbMin,worldAabbMax);
263 m_dispatchInfo.m_debugDraw->drawAabb(worldAabbMin,worldAabbMax,btVector3(1,0,0));
272 const btCollisionObjectWrapper* body1Wrap,
273 const btDispatcherInfo& dispatchInfo,
274 btManifoldResult* resultOut)
276 const btCollisionObjectWrapper* colObjWrap =
m_isSwapped ? body1Wrap : body0Wrap;
277 const btCollisionObjectWrapper* otherObjWrap =
m_isSwapped ? body0Wrap : body1Wrap;
279 btAssert(colObjWrap->getCollisionShape()->isCompound());
280 const auto* compoundShape =
static_cast<const btCompoundShape*
>(colObjWrap->getCollisionShape());
296 const btDbvt* tree = compoundShape->getDynamicAabbTree();
322 resultOut->refreshContactPoints();
323 resultOut->setPersistentManifold(
nullptr);
333 btVector3 localAabbMin, localAabbMax;
334 btTransform otherInCompoundSpace;
335 otherInCompoundSpace = colObjWrap->getWorldTransform().inverse() * otherObjWrap->getWorldTransform();
336 otherObjWrap->getCollisionShape()->getAabb(otherInCompoundSpace, localAabbMin, localAabbMax);
337 btVector3 extraExtends(resultOut->m_closestPointDistanceThreshold,
338 resultOut->m_closestPointDistanceThreshold,
339 resultOut->m_closestPointDistanceThreshold);
340 localAabbMin -= extraExtends;
341 localAabbMax += extraExtends;
343 const ATTRIBUTE_ALIGNED16(btDbvtVolume) bounds = btDbvtVolume::FromMM(localAabbMin, localAabbMax);
345 tree->collideTVNoStackAlloc(tree->m_root, bounds,
stack2, callback);
351 for (
int i = 0; i < numChildren; i++)
361 const btCollisionShape* childShape =
nullptr;
362 btTransform orgTrans;
364 btTransform newChildWorldTrans;
365 btVector3 aabbMin0, aabbMax0, aabbMin1, aabbMax1;
367 for (
int i = 0; i < numChildren; i++)
371 childShape = compoundShape->getChildShape(i);
373 orgTrans = colObjWrap->getWorldTransform();
375 const btTransform& childTrans = compoundShape->getChildTransform(i);
376 newChildWorldTrans = orgTrans * childTrans;
379 childShape->getAabb(newChildWorldTrans, aabbMin0, aabbMax0);
380 otherObjWrap->getCollisionShape()->getAabb(otherObjWrap->getWorldTransform(), aabbMin1, aabbMax1);
382 if (!TestAabbAgainstAabb2(aabbMin0, aabbMax0, aabbMin1, aabbMax1))
394 btCollisionObject* body1,
395 const btDispatcherInfo& dispatchInfo,
396 btManifoldResult* resultOut)
400 btCollisionObject* colObj =
m_isSwapped ? body1 : body0;
401 btCollisionObject* otherObj =
m_isSwapped ? body0 : body1;
403 btAssert(colObj->getCollisionShape()->isCompound());
405 auto* compoundShape =
static_cast<btCompoundShape*
>(colObj->getCollisionShape());
414 auto hitFraction = btScalar(1.);
417 btTransform orgTrans;
418 for (
int i = 0; i < numChildren; i++)
423 orgTrans = colObj->getWorldTransform();
425 const btTransform& childTrans = compoundShape->getChildTransform(i);
427 colObj->setWorldTransform(orgTrans * childTrans);
432 hitFraction = std::min(frac, hitFraction);
436 colObj->setWorldTransform(orgTrans);