From 43f735dd211e75a24673422b906638d5eab53670 Mon Sep 17 00:00:00 2001 From: Seth Alves Date: Sat, 9 Apr 2016 22:18:43 -0700 Subject: [PATCH] avoid loop where bullet moves an entity and then an entity tells bullet that it has moved. --- .../entities-renderer/src/RenderableEntityItem.h | 2 +- .../src/RenderableModelEntityItem.cpp | 4 ++-- .../src/RenderableModelEntityItem.h | 2 +- .../src/RenderableParticleEffectEntityItem.h | 2 +- .../entities-renderer/src/RenderableZoneEntityItem.h | 2 +- libraries/entities/src/EntityItem.cpp | 8 +++++--- libraries/entities/src/EntityItem.h | 2 +- libraries/physics/src/EntityMotionState.cpp | 12 ++++++++++-- libraries/shared/src/SpatiallyNestable.cpp | 12 ++++++------ libraries/shared/src/SpatiallyNestable.h | 6 +++--- 10 files changed, 31 insertions(+), 21 deletions(-) diff --git a/libraries/entities-renderer/src/RenderableEntityItem.h b/libraries/entities-renderer/src/RenderableEntityItem.h index 30d3f9d83c..92bb98ad32 100644 --- a/libraries/entities-renderer/src/RenderableEntityItem.h +++ b/libraries/entities-renderer/src/RenderableEntityItem.h @@ -92,7 +92,7 @@ private: public: \ virtual bool addToScene(EntityItemPointer self, std::shared_ptr scene, render::PendingChanges& pendingChanges) override { return _renderHelper.addToScene(self, scene, pendingChanges); } \ virtual void removeFromScene(EntityItemPointer self, std::shared_ptr scene, render::PendingChanges& pendingChanges) override { _renderHelper.removeFromScene(self, scene, pendingChanges); } \ - virtual void locationChanged() override { EntityItem::locationChanged(); _renderHelper.notifyChanged(); } \ + virtual void locationChanged(bool tellPhysics = true) override { EntityItem::locationChanged(tellPhysics); _renderHelper.notifyChanged(); } \ virtual void dimensionsChanged() override { EntityItem::dimensionsChanged(); _renderHelper.notifyChanged(); } \ private: \ SimpleRenderableEntityItem _renderHelper; diff --git a/libraries/entities-renderer/src/RenderableModelEntityItem.cpp b/libraries/entities-renderer/src/RenderableModelEntityItem.cpp index 19e5ab4683..879ff01056 100644 --- a/libraries/entities-renderer/src/RenderableModelEntityItem.cpp +++ b/libraries/entities-renderer/src/RenderableModelEntityItem.cpp @@ -798,8 +798,8 @@ void RenderableModelEntityItem::setJointTranslationsSet(const QVector& tra } -void RenderableModelEntityItem::locationChanged() { - EntityItem::locationChanged(); +void RenderableModelEntityItem::locationChanged(bool tellPhysics) { + EntityItem::locationChanged(tellPhysics); if (_model && _model->isActive()) { _model->setRotation(getRotation()); _model->setTranslation(getPosition()); diff --git a/libraries/entities-renderer/src/RenderableModelEntityItem.h b/libraries/entities-renderer/src/RenderableModelEntityItem.h index 90aca41f34..bf55c829e9 100644 --- a/libraries/entities-renderer/src/RenderableModelEntityItem.h +++ b/libraries/entities-renderer/src/RenderableModelEntityItem.h @@ -75,7 +75,7 @@ public: virtual void setJointTranslationsSet(const QVector& translationsSet) override; virtual void loader() override; - virtual void locationChanged() override; + virtual void locationChanged(bool tellPhysics = true) override; virtual void resizeJointArrays(int newSize = -1) override; diff --git a/libraries/entities-renderer/src/RenderableParticleEffectEntityItem.h b/libraries/entities-renderer/src/RenderableParticleEffectEntityItem.h index 1f066a81fd..a36c3640d6 100644 --- a/libraries/entities-renderer/src/RenderableParticleEffectEntityItem.h +++ b/libraries/entities-renderer/src/RenderableParticleEffectEntityItem.h @@ -29,7 +29,7 @@ public: virtual void removeFromScene(EntityItemPointer self, render::ScenePointer scene, render::PendingChanges& pendingChanges) override; protected: - virtual void locationChanged() override { EntityItem::locationChanged(); notifyBoundChanged(); } + virtual void locationChanged(bool tellPhysics = true) override { EntityItem::locationChanged(tellPhysics); notifyBoundChanged(); } virtual void dimensionsChanged() override { EntityItem::dimensionsChanged(); notifyBoundChanged(); } void notifyBoundChanged(); diff --git a/libraries/entities-renderer/src/RenderableZoneEntityItem.h b/libraries/entities-renderer/src/RenderableZoneEntityItem.h index 4ba862fff8..241a066341 100644 --- a/libraries/entities-renderer/src/RenderableZoneEntityItem.h +++ b/libraries/entities-renderer/src/RenderableZoneEntityItem.h @@ -42,7 +42,7 @@ public: virtual void removeFromScene(EntityItemPointer self, std::shared_ptr scene, render::PendingChanges& pendingChanges); private: - virtual void locationChanged() override { EntityItem::locationChanged(); notifyBoundChanged(); } + virtual void locationChanged(bool tellPhysics = true) override { EntityItem::locationChanged(tellPhysics); notifyBoundChanged(); } virtual void dimensionsChanged() override { EntityItem::dimensionsChanged(); notifyBoundChanged(); } void notifyBoundChanged(); diff --git a/libraries/entities/src/EntityItem.cpp b/libraries/entities/src/EntityItem.cpp index 4cd8a34248..417e6008e8 100644 --- a/libraries/entities/src/EntityItem.cpp +++ b/libraries/entities/src/EntityItem.cpp @@ -1976,14 +1976,16 @@ QList EntityItem::getActionsOfType(EntityActionType typeToG return result; } -void EntityItem::locationChanged() { +void EntityItem::locationChanged(bool tellPhysics) { requiresRecalcBoxes(); - _dirtyFlags |= Simulation::DIRTY_TRANSFORM; + if (tellPhysics) { + _dirtyFlags |= Simulation::DIRTY_TRANSFORM; + } EntityTreePointer tree = getTree(); if (tree) { tree->entityChanged(getThisPointer()); } - SpatiallyNestable::locationChanged(); // tell all the children, also + SpatiallyNestable::locationChanged(tellPhysics); // tell all the children, also } void EntityItem::dimensionsChanged() { diff --git a/libraries/entities/src/EntityItem.h b/libraries/entities/src/EntityItem.h index 622f78b2d3..08550c9ce5 100644 --- a/libraries/entities/src/EntityItem.h +++ b/libraries/entities/src/EntityItem.h @@ -432,7 +432,7 @@ protected: const QByteArray getActionDataInternal() const; void setActionDataInternal(QByteArray actionData); - virtual void locationChanged() override; + virtual void locationChanged(bool tellPhysics = true) override; virtual void dimensionsChanged() override; EntityTypes::EntityType _type; diff --git a/libraries/physics/src/EntityMotionState.cpp b/libraries/physics/src/EntityMotionState.cpp index 700160514c..a9dcb4a16c 100644 --- a/libraries/physics/src/EntityMotionState.cpp +++ b/libraries/physics/src/EntityMotionState.cpp @@ -207,8 +207,16 @@ void EntityMotionState::setWorldTransform(const btTransform& worldTrans) { assert(entityTreeIsLocked()); measureBodyAcceleration(); - _entity->setPosition(bulletToGLM(worldTrans.getOrigin()) + ObjectMotionState::getWorldOffset()); - _entity->setRotation(bulletToGLM(worldTrans.getRotation())); + bool positionSuccess; + _entity->setPosition(bulletToGLM(worldTrans.getOrigin()) + ObjectMotionState::getWorldOffset(), positionSuccess, false); + if (!positionSuccess) { + qDebug() << "EntityMotionState::setWorldTransform setPosition failed" << _entity->getID(); + } + bool orientationSuccess; + _entity->setOrientation(bulletToGLM(worldTrans.getRotation()), orientationSuccess, false); + if (!orientationSuccess) { + qDebug() << "EntityMotionState::setWorldTransform setOrientation failed" << _entity->getID(); + } _entity->setVelocity(getBodyLinearVelocity()); _entity->setAngularVelocity(getBodyAngularVelocity()); _entity->setLastSimulated(usecTimestampNow()); diff --git a/libraries/shared/src/SpatiallyNestable.cpp b/libraries/shared/src/SpatiallyNestable.cpp index 96a5a1d8ae..6b3e3806a7 100644 --- a/libraries/shared/src/SpatiallyNestable.cpp +++ b/libraries/shared/src/SpatiallyNestable.cpp @@ -314,7 +314,7 @@ glm::vec3 SpatiallyNestable::getPosition(int jointIndex, bool& success) const { return getTransform(jointIndex, success).getTranslation(); } -void SpatiallyNestable::setPosition(const glm::vec3& position, bool& success) { +void SpatiallyNestable::setPosition(const glm::vec3& position, bool& success, bool tellPhysics) { // guard against introducing NaN into the transform if (isNaN(position)) { success = false; @@ -328,7 +328,7 @@ void SpatiallyNestable::setPosition(const glm::vec3& position, bool& success) { Transform::inverseMult(_transform, parentTransform, myWorldTransform); }); if (success) { - locationChanged(); + locationChanged(tellPhysics); } else { qDebug() << "setPosition failed for" << getID(); } @@ -363,7 +363,7 @@ glm::quat SpatiallyNestable::getOrientation(int jointIndex, bool& success) const return getTransform(jointIndex, success).getRotation(); } -void SpatiallyNestable::setOrientation(const glm::quat& orientation, bool& success) { +void SpatiallyNestable::setOrientation(const glm::quat& orientation, bool& success, bool tellPhysics) { // guard against introducing NaN into the transform if (isNaN(orientation)) { success = false; @@ -378,7 +378,7 @@ void SpatiallyNestable::setOrientation(const glm::quat& orientation, bool& succe Transform::inverseMult(_transform, parentTransform, myWorldTransform); }); if (success) { - locationChanged(); + locationChanged(tellPhysics); } } @@ -751,9 +751,9 @@ void SpatiallyNestable::forEachDescendant(std::functionlocationChanged(); + object->locationChanged(tellPhysics); }); } diff --git a/libraries/shared/src/SpatiallyNestable.h b/libraries/shared/src/SpatiallyNestable.h index 58a141b1fa..c120c1010c 100644 --- a/libraries/shared/src/SpatiallyNestable.h +++ b/libraries/shared/src/SpatiallyNestable.h @@ -59,13 +59,13 @@ public: virtual glm::vec3 getPosition(bool& success) const; virtual glm::vec3 getPosition() const; - virtual void setPosition(const glm::vec3& position, bool& success); + virtual void setPosition(const glm::vec3& position, bool& success, bool tellPhysics = true); virtual void setPosition(const glm::vec3& position); virtual glm::quat getOrientation(bool& success) const; virtual glm::quat getOrientation() const; virtual glm::quat getOrientation(int jointIndex, bool& success) const; - virtual void setOrientation(const glm::quat& orientation, bool& success); + virtual void setOrientation(const glm::quat& orientation, bool& success, bool tellPhysics = true); virtual void setOrientation(const glm::quat& orientation); virtual glm::vec3 getVelocity(bool& success) const; @@ -159,7 +159,7 @@ protected: mutable ReadWriteLockable _childrenLock; mutable QHash _children; - virtual void locationChanged(); // called when a this object's location has changed + virtual void locationChanged(bool tellPhysics = true); // called when a this object's location has changed virtual void dimensionsChanged() { } // called when a this object's dimensions have changed // _queryAACube is used to decide where something lives in the octree