Remove involved overlapping pairs when addNoCollisionPair() is called

This commit is contained in:
jingqi 2021-12-03 11:57:23 +08:00
parent e28c6e5d7f
commit baf4d3f492
2 changed files with 28 additions and 5 deletions

View File

@ -413,11 +413,6 @@ RP3D_FORCE_INLINE void CollisionDetectionSystem::addCollider(Collider* collider,
mMapBroadPhaseIdToColliderEntity.add(Pair<int, Entity>(broadPhaseId, collider->getEntity()));
}
// Add a pair of bodies that cannot collide with each other
RP3D_FORCE_INLINE void CollisionDetectionSystem::addNoCollisionPair(Entity body1Entity, Entity body2Entity) {
mNoCollisionPairs.add(OverlappingPairs::computeBodiesIndexPair(body1Entity, body2Entity));
}
// Remove a pair of bodies that cannot collide with each other
RP3D_FORCE_INLINE void CollisionDetectionSystem::removeNoCollisionPair(Entity body1Entity, Entity body2Entity) {
mNoCollisionPairs.remove(OverlappingPairs::computeBodiesIndexPair(body1Entity, body2Entity));

View File

@ -196,6 +196,34 @@ void CollisionDetectionSystem::addLostContactPair(OverlappingPairs::OverlappingP
mLostContactPairs.add(lostContactPair);
}
// Add a pair of bodies that cannot collide with each other
void CollisionDetectionSystem::addNoCollisionPair(Entity body1Entity, Entity body2Entity) {
mNoCollisionPairs.add(OverlappingPairs::computeBodiesIndexPair(body1Entity, body2Entity));
// If there already are OverlappingPairs involved, they should be removed; Or they will remain in collision state
Array<uint64> toBeRemove(mMemoryManager.getPoolAllocator());
const Array<Entity>& colliderEntities = mWorld->mCollisionBodyComponents.getColliders(body1Entity);
for (uint32 i = 0; i < colliderEntities.size(); ++i) {
// Get the currently overlapping pairs for colliders of body1
const Array<uint64>& overlappingPairs = mCollidersComponents.getOverlappingPairs(colliderEntities[i]);
for (uint32 j = 0; j < overlappingPairs.size(); ++j) {
OverlappingPairs::OverlappingPair *pair = mOverlappingPairs.getOverlappingPair(overlappingPairs[j]);
assert(pair != nullptr);
const Entity overlappingBody1 = mOverlappingPairs.mColliderComponents.getBody(pair->collider1);
const Entity overlappingBody2 = mOverlappingPairs.mColliderComponents.getBody(pair->collider2);
if (overlappingBody1 == body2Entity || overlappingBody2 == body2Entity)
toBeRemove.add(overlappingPairs[j]);
}
}
for (uint32 i = 0; i < toBeRemove.size(); ++i)
mOverlappingPairs.removePair(toBeRemove[i]);
}
// Take an array of overlapping nodes in the broad-phase and create new overlapping pairs if necessary
void CollisionDetectionSystem::updateOverlappingPairs(const Array<Pair<int32, int32>>& overlappingNodes) {