Optimized MyAvatar.getCharacterControllerEnabled()

Instead of doing a blocking queued invokeMethod, it just calls into CharacterController.isEnabled() which is now thread-safe.
This commit is contained in:
Anthony J. Thibault 2016-06-09 09:35:19 -07:00
parent 30d8ae36e8
commit 1aae22f5a5
2 changed files with 7 additions and 10 deletions

View file

@ -1253,13 +1253,13 @@ void MyAvatar::prepareForPhysicsSimulation() {
void MyAvatar::harvestResultsFromPhysicsSimulation(float deltaTime) { void MyAvatar::harvestResultsFromPhysicsSimulation(float deltaTime) {
glm::vec3 position = getPosition(); glm::vec3 position = getPosition();
glm::quat orientation = getOrientation(); glm::quat orientation = getOrientation();
if (_characterController.isEnabled()) { if (_characterController.isEnabledAndReady()) {
_characterController.getPositionAndOrientation(position, orientation); _characterController.getPositionAndOrientation(position, orientation);
} }
nextAttitude(position, orientation); nextAttitude(position, orientation);
_bodySensorMatrix = _follow.postPhysicsUpdate(*this, _bodySensorMatrix); _bodySensorMatrix = _follow.postPhysicsUpdate(*this, _bodySensorMatrix);
if (_characterController.isEnabled()) { if (_characterController.isEnabledAndReady()) {
setVelocity(_characterController.getLinearVelocity() + _characterController.getFollowVelocity()); setVelocity(_characterController.getLinearVelocity() + _characterController.getFollowVelocity());
} else { } else {
setVelocity(getVelocity() + _characterController.getFollowVelocity()); setVelocity(getVelocity() + _characterController.getFollowVelocity());
@ -1642,7 +1642,7 @@ void MyAvatar::updatePosition(float deltaTime) {
vec3 velocity = getVelocity(); vec3 velocity = getVelocity();
const float MOVING_SPEED_THRESHOLD_SQUARED = 0.0001f; // 0.01 m/s 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 // _characterController is not in physics simulation but it can still compute its target velocity
updateMotors(); updateMotors();
_characterController.computeNewVelocity(deltaTime, velocity); _characterController.computeNewVelocity(deltaTime, velocity);
@ -1838,11 +1838,6 @@ void MyAvatar::setCharacterControllerEnabled(bool enabled) {
} }
bool MyAvatar::getCharacterControllerEnabled() { 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(); return _characterController.isEnabled();
} }

View file

@ -14,6 +14,7 @@
#include <assert.h> #include <assert.h>
#include <stdint.h> #include <stdint.h>
#include <atomic>
#include <btBulletDynamicsCommon.h> #include <btBulletDynamicsCommon.h>
#include <BulletDynamics/Character/btCharacterControllerInterface.h> #include <BulletDynamics/Character/btCharacterControllerInterface.h>
@ -105,8 +106,9 @@ public:
void setLocalBoundingBox(const glm::vec3& corner, const glm::vec3& scale); void setLocalBoundingBox(const glm::vec3& corner, const glm::vec3& scale);
bool isEnabled() const { return _enabled; } // thread-safe
void setEnabled(bool enabled); void setEnabled(bool enabled);
bool isEnabled() const { return _enabled && _dynamicsWorld; } bool isEnabledAndReady() const { return _enabled && _dynamicsWorld; }
bool getRigidBodyLocation(glm::vec3& avatarRigidBodyPosition, glm::quat& avatarRigidBodyRotation); bool getRigidBodyLocation(glm::vec3& avatarRigidBodyPosition, glm::quat& avatarRigidBodyRotation);
@ -167,7 +169,7 @@ protected:
btQuaternion _followAngularDisplacement; btQuaternion _followAngularDisplacement;
btVector3 _linearAcceleration; btVector3 _linearAcceleration;
bool _enabled; std::atomic_bool _enabled;
State _state; State _state;
bool _isPushingUp; bool _isPushingUp;