From 7be16da1a8d5affeb3790b4a1a033b067a69120a Mon Sep 17 00:00:00 2001 From: Andrew Meadows Date: Thu, 28 May 2015 14:24:39 -0700 Subject: [PATCH 1/3] compute correct time-of-flight for extrapolation --- libraries/entities/src/EntityItem.cpp | 33 ++++++++++++++++----------- 1 file changed, 20 insertions(+), 13 deletions(-) diff --git a/libraries/entities/src/EntityItem.cpp b/libraries/entities/src/EntityItem.cpp index 64196d1c35..4a9b64d1ca 100644 --- a/libraries/entities/src/EntityItem.cpp +++ b/libraries/entities/src/EntityItem.cpp @@ -480,17 +480,21 @@ int EntityItem::readEntityDataFromBuffer(const unsigned char* data, int bytesLef bytesRead += encodedUpdateDelta.size(); // Newer bitstreams will have a last simulated and a last updated value + quint64 lastSimulatedFromBufferAdjusted = now; if (args.bitstreamVersion >= VERSION_ENTITIES_HAS_LAST_SIMULATED_TIME) { // last simulated is stored as ByteCountCoded delta from lastEdited QByteArray encodedSimulatedDelta = originalDataBuffer.mid(bytesRead); // maximum possible size ByteCountCoded simulatedDeltaCoder = encodedSimulatedDelta; quint64 simulatedDelta = simulatedDeltaCoder; if (overwriteLocalData) { - _lastSimulated = lastEditedFromBufferAdjusted + simulatedDelta; // don't adjust for clock skew since we already did that + lastSimulatedFromBufferAdjusted = lastEditedFromBufferAdjusted + simulatedDelta; // don't adjust for clock skew since we already did that + if (lastSimulatedFromBufferAdjusted > now) { + lastSimulatedFromBufferAdjusted = now; + } #ifdef WANT_DEBUG - qCDebug(entities) << " _lastSimulated:" << debugTime(_lastSimulated, now); qCDebug(entities) << " _lastEdited:" << debugTime(_lastEdited, now); qCDebug(entities) << " lastEditedFromBufferAdjusted:" << debugTime(lastEditedFromBufferAdjusted, now); + qCDebug(entities) << " lastSimulatedFromBufferAdjusted:" << debugTime(lastSimulatedFromBufferAdjusted, now); #endif } encodedSimulatedDelta = simulatedDeltaCoder; // determine true length @@ -606,8 +610,8 @@ int EntityItem::readEntityDataFromBuffer(const unsigned char* data, int bytesLef // use our simulation helper routine to get a best estimate of where the entity should be. const float MIN_TIME_SKIP = 0.0f; const float MAX_TIME_SKIP = 1.0f; // in seconds - float skipTimeForward = glm::clamp((float)(now - _lastSimulated) / (float)(USECS_PER_SECOND), - MIN_TIME_SKIP, MAX_TIME_SKIP); + float skipTimeForward = glm::clamp((float)(now - lastSimulatedFromBufferAdjusted) / (float)(USECS_PER_SECOND), + MIN_TIME_SKIP, MAX_TIME_SKIP); if (skipTimeForward > 0.0f) { #ifdef WANT_DEBUG qCDebug(entities) << "skipTimeForward:" << skipTimeForward; @@ -617,19 +621,22 @@ int EntityItem::readEntityDataFromBuffer(const unsigned char* data, int bytesLef // we don't want the side effect of flag setting. simulateKinematicMotion(skipTimeForward, false); } - _lastSimulated = now; } auto nodeList = DependencyManager::get(); const QUuid& myNodeID = nodeList->getSessionUUID(); - if (overwriteLocalData && _simulatorID == myNodeID && !_simulatorID.isNull()) { - // we own the simulation, so we keep our transform+velocities and remove any related dirty flags - // rather than accept the values in the packet - _position = savePosition; - _rotation = saveRotation; - _velocity = saveVelocity; - _angularVelocity = saveAngularVelocity; - _dirtyFlags &= ~(EntityItem::DIRTY_TRANSFORM | EntityItem::DIRTY_VELOCITIES); + if (overwriteLocalData) { + if (_simulatorID == myNodeID && !_simulatorID.isNull()) { + // we own the simulation, so we keep our transform+velocities and remove any related dirty flags + // rather than accept the values in the packet + _position = savePosition; + _rotation = saveRotation; + _velocity = saveVelocity; + _angularVelocity = saveAngularVelocity; + _dirtyFlags &= ~(EntityItem::DIRTY_TRANSFORM | EntityItem::DIRTY_VELOCITIES); + } else { + _lastSimulated = now; + } } return bytesRead; From ac0609ea920bdfe0b54fc72af127c337700838cb Mon Sep 17 00:00:00 2001 From: Andrew Meadows Date: Thu, 28 May 2015 16:29:43 -0700 Subject: [PATCH 2/3] also render bbox when debugging simulator owner --- .../src/RenderableModelEntityItem.cpp | 14 ++++++-------- 1 file changed, 6 insertions(+), 8 deletions(-) diff --git a/libraries/entities-renderer/src/RenderableModelEntityItem.cpp b/libraries/entities-renderer/src/RenderableModelEntityItem.cpp index 75b29002f3..35309b0799 100644 --- a/libraries/entities-renderer/src/RenderableModelEntityItem.cpp +++ b/libraries/entities-renderer/src/RenderableModelEntityItem.cpp @@ -112,8 +112,6 @@ void RenderableModelEntityItem::render(RenderArgs* args) { PerformanceTimer perfTimer("RMEIrender"); assert(getType() == EntityTypes::Model); - bool drawAsModel = hasModel(); - glm::vec3 position = getPosition(); glm::vec3 dimensions = getDimensions(); @@ -125,8 +123,7 @@ void RenderableModelEntityItem::render(RenderArgs* args) { highlightSimulationOwnership = (getSimulatorID() == myNodeID); } - bool didDraw = false; - if (drawAsModel && !highlightSimulationOwnership) { + if (hasModel()) { remapTextures(); glPushMatrix(); { @@ -179,19 +176,20 @@ void RenderableModelEntityItem::render(RenderArgs* args) { if (args && (args->_renderMode == RenderArgs::SHADOW_RENDER_MODE)) { if (movingOrAnimating) { _model->renderInScene(alpha, args); - didDraw = true; } } else { _model->renderInScene(alpha, args); - didDraw = true; } } } } glPopMatrix(); - } - if (!didDraw) { + if (highlightSimulationOwnership) { + glm::vec4 greenColor(0.0f, 1.0f, 0.0f, 1.0f); + RenderableDebugableEntityItem::renderBoundingBox(this, args, 0.0f, greenColor); + } + } else { glm::vec4 greenColor(0.0f, 1.0f, 0.0f, 1.0f); RenderableDebugableEntityItem::renderBoundingBox(this, args, 0.0f, greenColor); } From 8177512432c7442036f48d641eb72543ee6df0ed Mon Sep 17 00:00:00 2001 From: Andrew Meadows Date: Fri, 29 May 2015 11:35:50 -0700 Subject: [PATCH 3/3] send all TerseUpdate properties when one changes --- libraries/entities/src/EntityItem.cpp | 15 +++++++++++++++ libraries/entities/src/EntityItem.h | 2 ++ libraries/entities/src/EntityItemProperties.cpp | 5 ++++- libraries/entities/src/EntityItemProperties.h | 3 +++ .../entities/src/EntityScriptingInterface.cpp | 12 +++++++++++- 5 files changed, 35 insertions(+), 2 deletions(-) diff --git a/libraries/entities/src/EntityItem.cpp b/libraries/entities/src/EntityItem.cpp index 4a9b64d1ca..cc951ac16b 100644 --- a/libraries/entities/src/EntityItem.cpp +++ b/libraries/entities/src/EntityItem.cpp @@ -925,6 +925,21 @@ EntityItemProperties EntityItem::getProperties() const { return properties; } +void EntityItem::getAllTerseUpdateProperties(EntityItemProperties& properties) const { + // a TerseUpdate includes the transform and its derivatives + properties._position = _position; + properties._velocity = _velocity; + properties._rotation = _rotation; + properties._angularVelocity = _angularVelocity; + properties._acceleration = _acceleration; + + properties._positionChanged = true; + properties._velocityChanged = true; + properties._rotationChanged = true; + properties._angularVelocityChanged = true; + properties._accelerationChanged = true; +} + bool EntityItem::setProperties(const EntityItemProperties& properties) { bool somethingChanged = false; diff --git a/libraries/entities/src/EntityItem.h b/libraries/entities/src/EntityItem.h index 4f2132bef4..6f9dc54e7a 100644 --- a/libraries/entities/src/EntityItem.h +++ b/libraries/entities/src/EntityItem.h @@ -347,6 +347,8 @@ public: quint64 getLastEditedFromRemote() { return _lastEditedFromRemote; } + void getAllTerseUpdateProperties(EntityItemProperties& properties) const; + protected: static bool _sendPhysicsUpdates; diff --git a/libraries/entities/src/EntityItemProperties.cpp b/libraries/entities/src/EntityItemProperties.cpp index 856c1a1cb4..583cf15b9c 100644 --- a/libraries/entities/src/EntityItemProperties.cpp +++ b/libraries/entities/src/EntityItemProperties.cpp @@ -1156,4 +1156,7 @@ AABox EntityItemProperties::getAABox() const { return AABox(rotatedExtentsRelativeToRegistrationPoint); } - +bool EntityItemProperties::hasTerseUpdateChanges() const { + // a TerseUpdate includes the transform and its derivatives + return _positionChanged || _velocityChanged || _rotationChanged || _angularVelocityChanged || _accelerationChanged; +} diff --git a/libraries/entities/src/EntityItemProperties.h b/libraries/entities/src/EntityItemProperties.h index 26c26bd474..dbe2e926c9 100644 --- a/libraries/entities/src/EntityItemProperties.h +++ b/libraries/entities/src/EntityItemProperties.h @@ -195,6 +195,8 @@ public: void setVoxelDataDirty() { _voxelDataChanged = true; } + bool hasTerseUpdateChanges() const; + private: QUuid _id; bool _idSet; @@ -215,6 +217,7 @@ private: QStringList _textureNames; glm::vec3 _naturalDimensions; }; + Q_DECLARE_METATYPE(EntityItemProperties); QScriptValue EntityItemPropertiesToScriptValue(QScriptEngine* engine, const EntityItemProperties& properties); QScriptValue EntityItemNonDefaultPropertiesToScriptValue(QScriptEngine* engine, const EntityItemProperties& properties); diff --git a/libraries/entities/src/EntityScriptingInterface.cpp b/libraries/entities/src/EntityScriptingInterface.cpp index 54d2ea705e..d684fbf2fc 100644 --- a/libraries/entities/src/EntityScriptingInterface.cpp +++ b/libraries/entities/src/EntityScriptingInterface.cpp @@ -146,7 +146,17 @@ QUuid EntityScriptingInterface::editEntity(QUuid id, const EntityItemProperties& EntityItemProperties modifiedProperties = properties; entity->setLastBroadcast(usecTimestampNow()); modifiedProperties.setType(entity->getType()); - bidForSimulationOwnership(modifiedProperties); + if (modifiedProperties.hasTerseUpdateChanges()) { + // we make a bid for (or assert) our simulation ownership + auto nodeList = DependencyManager::get(); + const QUuid myNodeID = nodeList->getSessionUUID(); + modifiedProperties.setSimulatorID(myNodeID); + + if (entity->getSimulatorID() == myNodeID) { + // we think we already own simulation, so make sure we send ALL TerseUpdate properties + entity->getAllTerseUpdateProperties(modifiedProperties); + } + } queueEntityMessage(PacketTypeEntityEdit, entityID, modifiedProperties); return id; }