From ac0c4e8512cc6f4ae752310c0c6816188b00e285 Mon Sep 17 00:00:00 2001 From: Seth Alves Date: Wed, 4 Mar 2015 19:57:06 -0800 Subject: [PATCH] cap max speed at MAX_WALKING_SPEED --- interface/src/avatar/MyAvatar.cpp | 8 +++----- libraries/physics/src/PhysicsEngine.cpp | 13 +++++++------ 2 files changed, 10 insertions(+), 11 deletions(-) diff --git a/interface/src/avatar/MyAvatar.cpp b/interface/src/avatar/MyAvatar.cpp index e6f84330bc..f30a22d78c 100644 --- a/interface/src/avatar/MyAvatar.cpp +++ b/interface/src/avatar/MyAvatar.cpp @@ -1397,8 +1397,6 @@ void MyAvatar::updatePosition(float deltaTime) { void MyAvatar::updatePositionWithPhysics(float deltaTime) { - // bool pushingUp = (_driveKeys[UP] - _driveKeys[DOWN] > 0.0f) || _scriptedMotorVelocity.y > 0.0f; - // rotate velocity into camera frame glm::quat rotation = getHead()->getCameraOrientation(); glm::vec3 localVelocity = glm::inverse(rotation) * _velocity; @@ -1408,9 +1406,9 @@ void MyAvatar::updatePositionWithPhysics(float deltaTime) { newLocalVelocity = applyScriptedMotor(deltaTime, newLocalVelocity); // cap avatar speed - float speed = glm::length(_velocity); - if (speed > MAX_AVATAR_SPEED) { - _velocity *= MAX_AVATAR_SPEED / speed; + float speed = glm::length(newLocalVelocity); + if (speed > MAX_WALKING_SPEED) { + newLocalVelocity *= MAX_WALKING_SPEED / speed; } // rotate back into world-frame diff --git a/libraries/physics/src/PhysicsEngine.cpp b/libraries/physics/src/PhysicsEngine.cpp index d7df649713..625afca683 100644 --- a/libraries/physics/src/PhysicsEngine.cpp +++ b/libraries/physics/src/PhysicsEngine.cpp @@ -281,18 +281,19 @@ void PhysicsEngine::stepSimulation() { // This is step (1). relayIncomingChangesToSimulation(); - if (_avatarData->isPhysicsEnabled()) { - _avatarGhostObject->setWorldTransform(btTransform(glmToBullet(_avatarData->getOrientation()), - glmToBullet(_avatarData->getPosition()))); - _characterController->setWalkDirection(glmToBullet(_avatarData->getVelocity())); - } - const int MAX_NUM_SUBSTEPS = 4; const float MAX_TIMESTEP = (float)MAX_NUM_SUBSTEPS * PHYSICS_ENGINE_FIXED_SUBSTEP; float dt = 1.0e-6f * (float)(_clock.getTimeMicroseconds()); _clock.reset(); float timeStep = btMin(dt, MAX_TIMESTEP); + if (_avatarData->isPhysicsEnabled()) { + _avatarGhostObject->setWorldTransform(btTransform(glmToBullet(_avatarData->getOrientation()), + glmToBullet(_avatarData->getPosition()))); + // _characterController->setWalkDirection(glmToBullet(_avatarData->getVelocity())); + _characterController->setVelocityForTimeInterval(glmToBullet(_avatarData->getVelocity()), timeStep); + } + // This is step (2). int numSubsteps = _dynamicsWorld->stepSimulation(timeStep, MAX_NUM_SUBSTEPS, PHYSICS_ENGINE_FIXED_SUBSTEP); _numSubsteps += (uint32_t)numSubsteps;