diff --git a/libraries/entities/src/EntityItem.cpp b/libraries/entities/src/EntityItem.cpp index b771279efa..512687d224 100644 --- a/libraries/entities/src/EntityItem.cpp +++ b/libraries/entities/src/EntityItem.cpp @@ -76,6 +76,7 @@ EntityItem::EntityItem(const EntityItemID& entityItemID) { _changedOnServer = 0; _element = NULL; _simulatorIDChangedTime = 0; + _shouldClaimSimulationOwnership = false; initFromEntityItemID(entityItemID); } diff --git a/libraries/entities/src/EntityItem.h b/libraries/entities/src/EntityItem.h index 96186b4c91..3a6a00d2ba 100644 --- a/libraries/entities/src/EntityItem.h +++ b/libraries/entities/src/EntityItem.h @@ -259,6 +259,8 @@ public: QUuid getSimulatorID() const { return _simulatorID; } void setSimulatorID(const QUuid& value); quint64 getSimulatorIDChangedTime() const { return _simulatorIDChangedTime; } + void setShouldClaimSimulationOwnership(bool value) { _shouldClaimSimulationOwnership = value; } + bool getShouldClaimSimulationOwnership() { return _shouldClaimSimulationOwnership; } const QString& getMarketplaceID() const { return _marketplaceID; } void setMarketplaceID(const QString& value) { _marketplaceID = value; } @@ -355,6 +357,7 @@ protected: QString _userData; QUuid _simulatorID; // id of Node which is currently responsible for simulating this Entity quint64 _simulatorIDChangedTime; // when was _simulatorID last updated? + bool _shouldClaimSimulationOwnership; QString _marketplaceID; // NOTE: Damping is applied like this: v *= pow(1 - damping, dt) diff --git a/libraries/entities/src/EntityTree.cpp b/libraries/entities/src/EntityTree.cpp index 7c2f41ed2c..85caccbf34 100644 --- a/libraries/entities/src/EntityTree.cpp +++ b/libraries/entities/src/EntityTree.cpp @@ -147,6 +147,8 @@ bool EntityTree::updateEntityWithElement(EntityItem* entity, const EntityItemPro properties.setPositionChanged(false); properties.setVelocityChanged(false); properties.setAccelerationChanged(false); + } else { + qCDebug(entities) << "allowing simulatorID change"; } } diff --git a/libraries/physics/src/EntityMotionState.cpp b/libraries/physics/src/EntityMotionState.cpp index 37472695c6..74a3d3220c 100644 --- a/libraries/physics/src/EntityMotionState.cpp +++ b/libraries/physics/src/EntityMotionState.cpp @@ -191,6 +191,10 @@ bool EntityMotionState::shouldSendUpdate(uint32_t simulationFrame) { return false; } + if (_entity->getShouldClaimSimulationOwnership()) { + return true; + } + auto nodeList = DependencyManager::get(); const QUuid& myNodeID = nodeList->getSessionUUID(); const QUuid& simulatorID = _entity->getSimulatorID(); @@ -262,7 +266,12 @@ void EntityMotionState::sendUpdate(OctreeEditPacketSender* packetSender, uint32_ QUuid myNodeID = nodeList->getSessionUUID(); QUuid simulatorID = _entity->getSimulatorID(); - if (simulatorID.isNull() && !(zeroSpeed && zeroSpin)) { + if (_entity->getShouldClaimSimulationOwnership()) { + _entity->setSimulatorID(myNodeID); + properties.setSimulatorID(myNodeID); + _entity->setShouldClaimSimulationOwnership(false); + } + else if (simulatorID.isNull() && !(zeroSpeed && zeroSpin)) { // The object is moving and nobody thinks they own the motion. set this Node as the simulator _entity->setSimulatorID(myNodeID); properties.setSimulatorID(myNodeID); diff --git a/libraries/physics/src/PhysicsEngine.cpp b/libraries/physics/src/PhysicsEngine.cpp index 88b3a1362c..c3ed870375 100644 --- a/libraries/physics/src/PhysicsEngine.cpp +++ b/libraries/physics/src/PhysicsEngine.cpp @@ -397,20 +397,19 @@ void PhysicsEngine::computeCollisionEvents() { _contactMap[ContactKey(a, b)].update(_numContactFrames, contactManifold->getContactPoint(0), _originOffset); // if our character capsule is colliding with something dynamic, claim simulation ownership. - // do this by setting the entity's simulator-id to NULL, thereby causing EntityMotionState::sendUpdate - // to set ownership to us. + // see EntityMotionState::sendUpdate if (objectA == characterCollisionObject && !objectB->isStaticOrKinematicObject() && b) { EntityItem* entityB = static_cast(b)->getEntity(); - if (!entityB->getSimulatorID().isNull() && entityB->getSimulatorID() != myNodeID) { + if (entityB->getSimulatorID() != myNodeID && !entityB->getShouldClaimSimulationOwnership()) { qDebug() << "CLAIMING B"; - entityB->setSimulatorID(NULL); + entityB->setShouldClaimSimulationOwnership(true); } } if (objectB == characterCollisionObject && !objectA->isStaticOrKinematicObject() && a) { EntityItem* entityA = static_cast(a)->getEntity(); - if (!entityA->getSimulatorID().isNull() && entityA->getSimulatorID() != myNodeID) { + if (entityA->getSimulatorID() != myNodeID && !entityA->getShouldClaimSimulationOwnership()) { qDebug() << "CLAIMING A"; - entityA->setSimulatorID(NULL); + entityA->setShouldClaimSimulationOwnership(true); } } }