From 434af8bdfb0ec717f829779ea1a1a402849430c8 Mon Sep 17 00:00:00 2001 From: Andrew Meadows Date: Fri, 16 Aug 2019 15:30:20 -0700 Subject: [PATCH] remove support for sub-contactAddecCallbacks from PhysicsEngine --- libraries/physics/src/CharacterController.cpp | 6 ++- libraries/physics/src/PhysicsEngine.cpp | 53 ++----------------- libraries/physics/src/PhysicsEngine.h | 3 +- 3 files changed, 10 insertions(+), 52 deletions(-) diff --git a/libraries/physics/src/CharacterController.cpp b/libraries/physics/src/CharacterController.cpp index 8e96d256b7..e488a4da98 100755 --- a/libraries/physics/src/CharacterController.cpp +++ b/libraries/physics/src/CharacterController.cpp @@ -284,10 +284,12 @@ bool CharacterController::checkForSupport(btCollisionWorld* collisionWorld) { _numStuckSubsteps = NUM_SUBSTEPS_FOR_SAFE_LANDING_RETRY; _stuckTransitionCount = 0; if (_isStuck) { - _physicsEngine->addContactAddedCallback(applyPairwiseFilter); + // enable pairwise filter + _physicsEngine->setContactAddedCallback(applyPairwiseFilter); _pairwiseFilter.incrementStepCount(); } else { - _physicsEngine->removeContactAddedCallback(applyPairwiseFilter); + // disable pairwise filter + _physicsEngine->setContactAddedCallback(nullptr); _appliedStuckRecoveryStrategy = false; _pairwiseFilter.clearAllEntries(); } diff --git a/libraries/physics/src/PhysicsEngine.cpp b/libraries/physics/src/PhysicsEngine.cpp index 2dd1140e45..c6f9aa84bc 100644 --- a/libraries/physics/src/PhysicsEngine.cpp +++ b/libraries/physics/src/PhysicsEngine.cpp @@ -27,29 +27,6 @@ #include "ThreadSafeDynamicsWorld.h" #include "PhysicsLogging.h" -// a list of sub-callbacks -std::vector _contactAddedCallbacks; - -// a callback that calls each sub-callback in the list -// if one returns 'true' --> break and return -bool globalContactAddedCallback(btManifoldPoint& cp, - const btCollisionObjectWrapper* colObj0Wrap, int partId0, int index0, - const btCollisionObjectWrapper* colObj1Wrap, int partId1, int index1) { - // call each callback - for (auto cb : _contactAddedCallbacks) { - if (cb(cp, colObj0Wrap, partId0, index0, colObj1Wrap, partId1, index1)) { - // Not a Bullet convention, but one we are using for sub-callbacks: - // a return value of 'true' indicates the contact has been "disabled" - // in which case there is no need to process other callbacks - break; - } - } - // by Bullet convention for its gContactAddedCallback feature: - // the return value is currently ignored but to be future-proof: - // return true when friction has been modified - return false; -} - PhysicsEngine::PhysicsEngine(const glm::vec3& offset) : _originOffset(offset), _myAvatarController(nullptr) { @@ -843,31 +820,11 @@ void PhysicsEngine::setShowBulletConstraintLimits(bool value) { } } -void PhysicsEngine::addContactAddedCallback(PhysicsEngine::ContactAddedCallback newCb) { - for (auto cb : _contactAddedCallbacks) { - if (cb == newCb) { - // newCb is already in the list - return; - } - } - _contactAddedCallbacks.push_back(newCb); - gContactAddedCallback = globalContactAddedCallback; -} - -void PhysicsEngine::removeContactAddedCallback(PhysicsEngine::ContactAddedCallback cb) { - int32_t numCallbacks = (int32_t)(_contactAddedCallbacks.size()); - for (int32_t i = 0; i < numCallbacks; ++i) { - if (_contactAddedCallbacks[i] == cb) { - // found it --> remove it - _contactAddedCallbacks[i] = _contactAddedCallbacks[numCallbacks - 1]; - _contactAddedCallbacks.pop_back(); - numCallbacks--; - break; - } - } - if (numCallbacks == 0) { - gContactAddedCallback = nullptr; - } +void PhysicsEngine::setContactAddedCallback(PhysicsEngine::ContactAddedCallback newCb) { + // gContactAddedCallback is a special feature hook in Bullet + // if non-null AND one of the colliding objects has btCollisionObject::CF_CUSTOM_MATERIAL_CALLBACK flag set + // then it is called whenever a new candidate contact point is created + gContactAddedCallback = newCb; } struct AllContactsCallback : public btCollisionWorld::ContactResultCallback { diff --git a/libraries/physics/src/PhysicsEngine.h b/libraries/physics/src/PhysicsEngine.h index bbca20c301..e0164a33f7 100644 --- a/libraries/physics/src/PhysicsEngine.h +++ b/libraries/physics/src/PhysicsEngine.h @@ -154,8 +154,7 @@ public: // See PhysicsCollisionGroups.h for mask flags. std::vector contactTest(uint16_t mask, const ShapeInfo& regionShapeInfo, const Transform& regionTransform, uint16_t group = USER_COLLISION_GROUP_DYNAMIC, float threshold = 0.0f) const; - void addContactAddedCallback(ContactAddedCallback cb); - void removeContactAddedCallback(ContactAddedCallback cb); + void setContactAddedCallback(ContactAddedCallback cb); btDiscreteDynamicsWorld* getDynamicsWorld() const { return _dynamicsWorld; } void removeContacts(ObjectMotionState* motionState);