From f454dac709d5b709545a7e484f7c3eed30333594 Mon Sep 17 00:00:00 2001 From: Andrew Meadows Date: Wed, 9 May 2018 12:04:44 -0700 Subject: [PATCH 1/2] avoid div by zero when measuring acceleration --- libraries/physics/src/EntityMotionState.cpp | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/libraries/physics/src/EntityMotionState.cpp b/libraries/physics/src/EntityMotionState.cpp index a801392b66..fbb4b69ce0 100644 --- a/libraries/physics/src/EntityMotionState.cpp +++ b/libraries/physics/src/EntityMotionState.cpp @@ -731,7 +731,9 @@ void EntityMotionState::measureBodyAcceleration() { // hence the equation for acceleration is: a = (v1 / (1 - D)^dt - v0) / dt glm::vec3 velocity = getBodyLinearVelocityGTSigma(); - _measuredAcceleration = (velocity / powf(1.0f - _body->getLinearDamping(), dt) - _lastVelocity) * invDt; + const float MIN_DAMPING_FACTOR = 0.01f; + float dampingAttenuationFactor = 1.0f / glm::max(powf(1.0f - _body->getLinearDamping(), dt), MIN_DAMPING_FACTOR); + _measuredAcceleration = (velocity * dampingAttenuationFactor - _lastVelocity) * invDt; _lastVelocity = velocity; if (numSubsteps > PHYSICS_ENGINE_MAX_NUM_SUBSTEPS) { // we fall in here when _lastMeasureStep is old: the body has just become active From b8d34f4b8fee9f754f7312f6351bc347db31ba85 Mon Sep 17 00:00:00 2001 From: Andrew Meadows Date: Wed, 9 May 2018 12:13:27 -0700 Subject: [PATCH 2/2] more correct variable name --- libraries/physics/src/EntityMotionState.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/libraries/physics/src/EntityMotionState.cpp b/libraries/physics/src/EntityMotionState.cpp index fbb4b69ce0..e04055ec32 100644 --- a/libraries/physics/src/EntityMotionState.cpp +++ b/libraries/physics/src/EntityMotionState.cpp @@ -732,8 +732,8 @@ void EntityMotionState::measureBodyAcceleration() { glm::vec3 velocity = getBodyLinearVelocityGTSigma(); const float MIN_DAMPING_FACTOR = 0.01f; - float dampingAttenuationFactor = 1.0f / glm::max(powf(1.0f - _body->getLinearDamping(), dt), MIN_DAMPING_FACTOR); - _measuredAcceleration = (velocity * dampingAttenuationFactor - _lastVelocity) * invDt; + float invDampingAttenuationFactor = 1.0f / glm::max(powf(1.0f - _body->getLinearDamping(), dt), MIN_DAMPING_FACTOR); + _measuredAcceleration = (velocity * invDampingAttenuationFactor - _lastVelocity) * invDt; _lastVelocity = velocity; if (numSubsteps > PHYSICS_ENGINE_MAX_NUM_SUBSTEPS) { // we fall in here when _lastMeasureStep is old: the body has just become active