Working on contacts

This commit is contained in:
Daniel Chappuis 2019-05-24 07:15:31 +02:00
parent 669e74d528
commit 251333a6ef
3 changed files with 48 additions and 25 deletions

View File

@ -109,6 +109,36 @@ void CollisionDetection::computeBroadPhase() {
// Create new overlapping pairs if necessary
updateOverlappingPairs(overlappingNodes);
// Remove non overlapping pairs
removeNonOverlappingPairs();
}
// Remove pairs that are not overlapping anymore
void CollisionDetection::removeNonOverlappingPairs() {
// For each possible collision pair of bodies
for (auto it = mOverlappingPairs.begin(); it != mOverlappingPairs.end(); ) {
OverlappingPair* pair = it->second;
ProxyShape* shape1 = pair->getShape1();
ProxyShape* shape2 = pair->getShape2();
// Check if the two shapes are still overlapping. Otherwise, we destroy the overlapping pair
if (!mBroadPhaseSystem.testOverlappingShapes(shape1, shape2)) {
// Destroy the overlapping pair
pair->~OverlappingPair();
mWorld->mMemoryManager.release(MemoryManager::AllocationType::Pool, pair, sizeof(OverlappingPair));
it = mOverlappingPairs.remove(it);
continue;
}
else {
++it;
}
}
}
// Take a list of overlapping nodes in the broad-phase and create new overlapping pairs if necessary
@ -183,7 +213,7 @@ void CollisionDetection::computeMiddlePhase() {
mNarrowPhaseInput.reserveMemory();
// For each possible collision pair of bodies
for (auto it = mOverlappingPairs.begin(); it != mOverlappingPairs.end(); ) {
for (auto it = mOverlappingPairs.begin(); it != mOverlappingPairs.end(); ++it) {
OverlappingPair* pair = it->second;
@ -200,21 +230,6 @@ void CollisionDetection::computeMiddlePhase() {
assert(mProxyShapesComponents.getBroadPhaseId(proxyShape2Entity) != -1);
assert(mProxyShapesComponents.getBroadPhaseId(proxyShape1Entity) != mProxyShapesComponents.getBroadPhaseId(proxyShape2Entity));
// Check if the two shapes are still overlapping. Otherwise, we destroy the
// overlapping pair
if (!mBroadPhaseSystem.testOverlappingShapes(shape1, shape2)) {
// Destroy the overlapping pair
pair->~OverlappingPair();
mWorld->mMemoryManager.release(MemoryManager::AllocationType::Pool, pair, sizeof(OverlappingPair));
it = mOverlappingPairs.remove(it);
continue;
}
else {
++it;
}
// Check if the collision filtering allows collision between the two shapes
if ((mProxyShapesComponents.getCollideWithMaskBits(proxyShape1Entity) & mProxyShapesComponents.getCollisionCategoryBits(proxyShape2Entity)) != 0 &&
(mProxyShapesComponents.getCollisionCategoryBits(proxyShape1Entity) & mProxyShapesComponents.getCollideWithMaskBits(proxyShape2Entity)) != 0) {
@ -398,9 +413,6 @@ void CollisionDetection::processAllPotentialContacts(NarrowPhaseInput& narrowPha
// Compute the narrow-phase collision detection
void CollisionDetection::computeNarrowPhase() {
// TODO : DELETE THIS
//std::cout << "---------------------- NARROW PHASE ----------------------------------------" << std::endl;
RP3D_PROFILE("CollisionDetection::computeNarrowPhase()", mProfiler);
MemoryAllocator& allocator = mMemoryManager.getSingleFrameAllocator();
@ -415,7 +427,7 @@ void CollisionDetection::computeNarrowPhase() {
processAllPotentialContacts(mNarrowPhaseInput, true);
// Reduce the number of contact points in the manifolds
reducePotentialContactManifolds(mOverlappingPairs);
reducePotentialContactManifolds();
// Report contacts to the user
reportAllContacts();
@ -840,7 +852,7 @@ void CollisionDetection::processPotentialContacts(NarrowPhaseInfoBatch& narrowPh
}
// Clear the obsolete manifolds and contact points and reduce the number of contacts points of the remaining manifolds
void CollisionDetection::reducePotentialContactManifolds(const OverlappingPairMap& overlappingPairs) {
void CollisionDetection::reducePotentialContactManifolds() {
RP3D_PROFILE("CollisionDetection::reducePotentialContactManifolds()", mProfiler);
@ -1369,7 +1381,7 @@ void CollisionDetection::testCollision(CollisionBody* body1, CollisionBody* body
processAllPotentialContacts(narrowPhaseInput, false);
// Reduce the number of contact points in the manifolds
reducePotentialContactManifolds(overlappingPairs);
//reducePotentialContactManifolds(overlappingPairs);
// TODO : Rework how we report contacts
/*
@ -1467,7 +1479,7 @@ void CollisionDetection::testCollision(CollisionBody* body, CollisionCallback* c
processAllPotentialContacts(narrowPhaseInput, false);
// Reduce the number of contact points in the manifolds
reducePotentialContactManifolds(overlappingPairs);
//reducePotentialContactManifolds(overlappingPairs);
// TODO : Rework how we report contacts
/*
@ -1549,7 +1561,7 @@ void CollisionDetection::testCollision(CollisionCallback* callback) {
processAllPotentialContacts(narrowPhaseInput, false);
// Reduce the number of contact points in the manifolds
reducePotentialContactManifolds(overlappingPairs);
//reducePotentialContactManifolds(overlappingPairs);
// TODO : Rework how we report contacts
/*

View File

@ -189,6 +189,9 @@ class CollisionDetection {
/// Take a list of overlapping nodes in the broad-phase and create new overlapping pairs if necessary
void updateOverlappingPairs(List<Pair<int, int> >& overlappingNodes);
/// Remove pairs that are not overlapping anymore
void removeNonOverlappingPairs();
/// Execute the narrow-phase collision detection algorithm on batches
bool testNarrowPhaseCollision(NarrowPhaseInput& narrowPhaseInput, bool stopFirstContactFound,
bool reportContacts, MemoryAllocator& allocator);
@ -211,7 +214,7 @@ class CollisionDetection {
void processAllPotentialContacts(NarrowPhaseInput& narrowPhaseInput, bool updateLastFrameInfo);
/// Reduce the potential contact manifolds and contact points of the overlapping pair contacts
void reducePotentialContactManifolds(const OverlappingPairMap& overlappingPairs);
void reducePotentialContactManifolds();
/// Create the actual contact manifolds and contacts points (from potential contacts) for a given contact pair
void createContacts();

View File

@ -78,6 +78,10 @@ bool ProxyShape::testPointInside(const Vector3& worldPoint) {
*/
void ProxyShape::setCollisionCategoryBits(unsigned short collisionCategoryBits) {
// TODO : Here we should probably remove all overlapping pairs with this shape in the
// broad-phase and add the shape in the "has moved" shape list so it is reevaluated
// with the new mask bits
mBody->mWorld.mProxyShapesComponents.setCollisionCategoryBits(mEntity, collisionCategoryBits);
int broadPhaseId = mBody->mWorld.mProxyShapesComponents.getBroadPhaseId(mEntity);
@ -93,6 +97,10 @@ void ProxyShape::setCollisionCategoryBits(unsigned short collisionCategoryBits)
*/
void ProxyShape::setCollideWithMaskBits(unsigned short collideWithMaskBits) {
// TODO : Here we should probably remove all overlapping pairs with this shape in the
// broad-phase and add the shape in the "has moved" shape list so it is reevaluated
// with the new mask bits
mBody->mWorld.mProxyShapesComponents.setCollideWithMaskBits(mEntity, collideWithMaskBits);
int broadPhaseId = mBody->mWorld.mProxyShapesComponents.getBroadPhaseId(mEntity);