From 1aae22f5a59246e795801aa09ecf07bd93ee50af Mon Sep 17 00:00:00 2001 From: "Anthony J. Thibault" Date: Thu, 9 Jun 2016 09:35:19 -0700 Subject: [PATCH] Optimized MyAvatar.getCharacterControllerEnabled() Instead of doing a blocking queued invokeMethod, it just calls into CharacterController.isEnabled() which is now thread-safe. --- interface/src/avatar/MyAvatar.cpp | 11 +++-------- libraries/physics/src/CharacterController.h | 6 ++++-- 2 files changed, 7 insertions(+), 10 deletions(-) diff --git a/interface/src/avatar/MyAvatar.cpp b/interface/src/avatar/MyAvatar.cpp index 3495a05962..d5c481164c 100644 --- a/interface/src/avatar/MyAvatar.cpp +++ b/interface/src/avatar/MyAvatar.cpp @@ -1253,13 +1253,13 @@ void MyAvatar::prepareForPhysicsSimulation() { void MyAvatar::harvestResultsFromPhysicsSimulation(float deltaTime) { glm::vec3 position = getPosition(); glm::quat orientation = getOrientation(); - if (_characterController.isEnabled()) { + if (_characterController.isEnabledAndReady()) { _characterController.getPositionAndOrientation(position, orientation); } nextAttitude(position, orientation); _bodySensorMatrix = _follow.postPhysicsUpdate(*this, _bodySensorMatrix); - if (_characterController.isEnabled()) { + if (_characterController.isEnabledAndReady()) { setVelocity(_characterController.getLinearVelocity() + _characterController.getFollowVelocity()); } else { setVelocity(getVelocity() + _characterController.getFollowVelocity()); @@ -1642,7 +1642,7 @@ void MyAvatar::updatePosition(float deltaTime) { vec3 velocity = getVelocity(); const float MOVING_SPEED_THRESHOLD_SQUARED = 0.0001f; // 0.01 m/s - if (!_characterController.isEnabled()) { + if (!_characterController.isEnabledAndReady()) { // _characterController is not in physics simulation but it can still compute its target velocity updateMotors(); _characterController.computeNewVelocity(deltaTime, velocity); @@ -1838,11 +1838,6 @@ void MyAvatar::setCharacterControllerEnabled(bool enabled) { } bool MyAvatar::getCharacterControllerEnabled() { - if (QThread::currentThread() != thread()) { - bool result; - QMetaObject::invokeMethod(this, "getCharacterControllerEnabled", Qt::BlockingQueuedConnection, Q_RETURN_ARG(bool, result)); - return result; - } return _characterController.isEnabled(); } diff --git a/libraries/physics/src/CharacterController.h b/libraries/physics/src/CharacterController.h index 2191f46d55..586ea175e6 100644 --- a/libraries/physics/src/CharacterController.h +++ b/libraries/physics/src/CharacterController.h @@ -14,6 +14,7 @@ #include #include +#include #include #include @@ -105,8 +106,9 @@ public: void setLocalBoundingBox(const glm::vec3& corner, const glm::vec3& scale); + bool isEnabled() const { return _enabled; } // thread-safe void setEnabled(bool enabled); - bool isEnabled() const { return _enabled && _dynamicsWorld; } + bool isEnabledAndReady() const { return _enabled && _dynamicsWorld; } bool getRigidBodyLocation(glm::vec3& avatarRigidBodyPosition, glm::quat& avatarRigidBodyRotation); @@ -167,7 +169,7 @@ protected: btQuaternion _followAngularDisplacement; btVector3 _linearAcceleration; - bool _enabled; + std::atomic_bool _enabled; State _state; bool _isPushingUp;