From 1314430f0482545636e15b559c0ab23e534b5600 Mon Sep 17 00:00:00 2001 From: Andrew Meadows Date: Tue, 27 Aug 2019 13:51:54 -0700 Subject: [PATCH] more checks to prevent thrusting through mesh --- libraries/physics/src/CharacterController.cpp | 21 ++++++++++--------- 1 file changed, 11 insertions(+), 10 deletions(-) diff --git a/libraries/physics/src/CharacterController.cpp b/libraries/physics/src/CharacterController.cpp index 507a79607d..16b893977a 100755 --- a/libraries/physics/src/CharacterController.cpp +++ b/libraries/physics/src/CharacterController.cpp @@ -784,19 +784,20 @@ void CharacterController::computeNewVelocity(btScalar dt, btVector3& velocity) { // but we want to avoid getting stuck and tunelling through geometry so we perform // further checks and modify/abandon our velocity calculations - if (_isStuck || _stuckTransitionCount == 0) { - // we are either definitely stuck, or definitely not --> nothing to do + const float SAFE_COLLISION_SPEED = glm::abs(STUCK_PENETRATION) * (float)NUM_SUBSTEPS_PER_SECOND; + const float SAFE_COLLISION_SPEED_SQUARED = SAFE_COLLISION_SPEED * SAFE_COLLISION_SPEED; + + const float STRONG_IMPACT_THRESHOLD = -1000.0f; // this tuned manually + const float VERY_STRONG_IMPACT_THRESHOLD = -2000.0f; // this tuned manually + float velocityDotImpulse = velocity.dot(_netCollisionImpulse); + + if ((velocityDotImpulse < VERY_STRONG_IMPACT_THRESHOLD && _stuckTransitionCount == 0) || _isStuck) { + // we are either definitely NOT stuck, or definitely are --> nothing to do here return; } - const float SAFE_COLLISION_SPEED = glm::abs(STUCK_PENETRATION) * (float)NUM_SUBSTEPS_PER_SECOND; - const float SAFE_COLLISION_SPEED_SQUARED = SAFE_COLLISION_SPEED * SAFE_COLLISION_SPEED; - bool fast = velocity.length2() > SAFE_COLLISION_SPEED_SQUARED; - - const float STRONG_IMPACT_IMPULSE_DOT = -1000.0f; // this tuned manually - bool strongImpact = velocity.dot(_netCollisionImpulse) < STRONG_IMPACT_IMPULSE_DOT; - - if (fast && strongImpact) { + if (velocityDotImpulse < VERY_STRONG_IMPACT_THRESHOLD || + (velocity.length2() > SAFE_COLLISION_SPEED_SQUARED && velocityDotImpulse < STRONG_IMPACT_THRESHOLD)) { const float REFLECTION_COEFFICIENT = 1.5f; if (velocity.dot(currentVelocity) > 0.0f) { // our new velocity points in the same direction as our currentVelocity