From 52aee63e0524862d69b268f65eccb9292133154c Mon Sep 17 00:00:00 2001 From: Seth Alves Date: Fri, 14 Apr 2017 06:08:58 -0700 Subject: [PATCH] keep track of objectDynamics per rigidBody --- .../physics/src/ObjectConstraintHinge.cpp | 30 +++++++++---------- libraries/physics/src/ObjectConstraintHinge.h | 4 +-- libraries/physics/src/ObjectDynamic.cpp | 25 ++++++++++++++++ libraries/physics/src/ObjectDynamic.h | 3 ++ libraries/physics/src/PhysicsEngine.cpp | 20 +++++++------ libraries/physics/src/PhysicsEngine.h | 2 ++ 6 files changed, 57 insertions(+), 27 deletions(-) diff --git a/libraries/physics/src/ObjectConstraintHinge.cpp b/libraries/physics/src/ObjectConstraintHinge.cpp index b6c19e96ce..cdd4ba490c 100644 --- a/libraries/physics/src/ObjectConstraintHinge.cpp +++ b/libraries/physics/src/ObjectConstraintHinge.cpp @@ -35,6 +35,19 @@ ObjectConstraintHinge::~ObjectConstraintHinge() { #endif } +QList ObjectConstraintHinge::getRigidBodies() { + QList result; + result += getRigidBody(); + QUuid otherEntityID; + withReadLock([&]{ + otherEntityID = _otherEntityID; + }); + if (!otherEntityID.isNull()) { + result += getOtherRigidBody(otherEntityID); + } + return result; +} + btTypedConstraint* ObjectConstraintHinge::getConstraint() { btHingeConstraint* constraint { nullptr }; QUuid otherEntityID; @@ -65,20 +78,7 @@ btTypedConstraint* ObjectConstraintHinge::getConstraint() { if (!otherEntityID.isNull()) { // This hinge is between two entities... find the other rigid body. - EntityItemPointer otherEntity = getEntityByID(otherEntityID); - if (!otherEntity) { - return nullptr; - } - - void* otherPhysicsInfo = otherEntity->getPhysicsInfo(); - if (!otherPhysicsInfo) { - return nullptr; - } - ObjectMotionState* otherMotionState = static_cast(otherPhysicsInfo); - if (!otherMotionState) { - return nullptr; - } - btRigidBody* rigidBodyB = otherMotionState->getRigidBody(); + btRigidBody* rigidBodyB = getOtherRigidBody(otherEntityID); if (!rigidBodyB) { return nullptr; } @@ -94,7 +94,6 @@ btTypedConstraint* ObjectConstraintHinge::getConstraint() { constraint->setAxis(bulletAxisInA); withWriteLock([&]{ - _otherEntity = otherEntity; // save weak-pointer to the other entity _constraint = constraint; }); @@ -110,7 +109,6 @@ btTypedConstraint* ObjectConstraintHinge::getConstraint() { constraint->setAxis(bulletAxisInA); withWriteLock([&]{ - _otherEntity.reset(); _constraint = constraint; }); return constraint; diff --git a/libraries/physics/src/ObjectConstraintHinge.h b/libraries/physics/src/ObjectConstraintHinge.h index 7823b85758..2bdbeab0aa 100644 --- a/libraries/physics/src/ObjectConstraintHinge.h +++ b/libraries/physics/src/ObjectConstraintHinge.h @@ -27,6 +27,7 @@ public: virtual QByteArray serialize() const override; virtual void deserialize(QByteArray serializedArguments) override; + virtual QList getRigidBodies() override; virtual btTypedConstraint* getConstraint() override; protected: @@ -35,8 +36,7 @@ protected: glm::vec3 _pivotInA; glm::vec3 _axisInA; - QUuid _otherEntityID; - EntityItemWeakPointer _otherEntity; + EntityItemID _otherEntityID; glm::vec3 _pivotInB; glm::vec3 _axisInB; }; diff --git a/libraries/physics/src/ObjectDynamic.cpp b/libraries/physics/src/ObjectDynamic.cpp index 4831c44705..3e5c9f3f9b 100644 --- a/libraries/physics/src/ObjectDynamic.cpp +++ b/libraries/physics/src/ObjectDynamic.cpp @@ -271,3 +271,28 @@ quint64 ObjectDynamic::serverTimeToLocalTime(quint64 timeValue) const { return timeValue - serverClockSkew; } + +btRigidBody* ObjectDynamic::getOtherRigidBody(EntityItemID otherEntityID) { + EntityItemPointer otherEntity = getEntityByID(otherEntityID); + if (!otherEntity) { + return nullptr; + } + + void* otherPhysicsInfo = otherEntity->getPhysicsInfo(); + if (!otherPhysicsInfo) { + return nullptr; + } + + ObjectMotionState* otherMotionState = static_cast(otherPhysicsInfo); + if (!otherMotionState) { + return nullptr; + } + + return otherMotionState->getRigidBody(); +} + +QList ObjectDynamic::getRigidBodies() { + QList result; + result += getRigidBody(); + return result; +} diff --git a/libraries/physics/src/ObjectDynamic.h b/libraries/physics/src/ObjectDynamic.h index 1ffc13239c..c8e809cfe1 100644 --- a/libraries/physics/src/ObjectDynamic.h +++ b/libraries/physics/src/ObjectDynamic.h @@ -43,10 +43,13 @@ public: virtual bool lifetimeIsOver() override; virtual quint64 getExpires() override { return _expires; } + virtual QList getRigidBodies(); + protected: quint64 localTimeToServerTime(quint64 timeValue) const; quint64 serverTimeToLocalTime(quint64 timeValue) const; + btRigidBody* getOtherRigidBody(EntityItemID otherEntityID); EntityItemPointer getEntityByID(EntityItemID entityID) const; virtual btRigidBody* getRigidBody(); virtual glm::vec3 getPosition() override; diff --git a/libraries/physics/src/PhysicsEngine.cpp b/libraries/physics/src/PhysicsEngine.cpp index 3cd6abb839..8a9a5edfb2 100644 --- a/libraries/physics/src/PhysicsEngine.cpp +++ b/libraries/physics/src/PhysicsEngine.cpp @@ -570,25 +570,27 @@ bool PhysicsEngine::addDynamic(EntityDynamicPointer dynamic) { removeDynamic(dynamic->getID()); } - _objectDynamics[dynamicID] = dynamic; - - // bullet needs a pointer to the dynamic, but it doesn't use shared pointers. - // is there a way to bump the reference count? + bool success { false }; if (dynamic->isAction()) { ObjectAction* objectAction = static_cast(dynamic.get()); _dynamicsWorld->addAction(objectAction); - return true; + success = true; } else if (dynamic->isConstraint()) { ObjectConstraint* objectConstraint = static_cast(dynamic.get()); btTypedConstraint* constraint = objectConstraint->getConstraint(); if (constraint) { _dynamicsWorld->addConstraint(constraint); - return true; - } else { - // perhaps not all the rigid bodies are available, yet - return false; + success = true; + } // else perhaps not all the rigid bodies are available, yet + } + + if (success) { + _objectDynamics[dynamicID] = dynamic; + foreach(btRigidBody* rigidBody, std::static_pointer_cast(dynamic)->getRigidBodies()) { + _objectDynamicsByBody[rigidBody] = dynamic; } } + return success; } void PhysicsEngine::removeDynamic(const QUuid dynamicID) { diff --git a/libraries/physics/src/PhysicsEngine.h b/libraries/physics/src/PhysicsEngine.h index d9f70efc3e..ba30375e73 100644 --- a/libraries/physics/src/PhysicsEngine.h +++ b/libraries/physics/src/PhysicsEngine.h @@ -112,6 +112,7 @@ private: ContactMap _contactMap; CollisionEvents _collisionEvents; QHash _objectDynamics; + QHash _objectDynamicsByBody; std::vector _activeStaticBodies; glm::vec3 _originOffset; @@ -123,6 +124,7 @@ private: bool _dumpNextStats = false; bool _hasOutgoingChanges = false; + }; typedef std::shared_ptr PhysicsEnginePointer;