reduce movement speed when walking

This commit is contained in:
Andrew Meadows 2014-05-05 13:28:42 -07:00
parent 815262b802
commit 0c78449cf7
2 changed files with 19 additions and 12 deletions

View file

@ -157,7 +157,7 @@ void MyAvatar::simulate(float deltaTime) {
fabsf(_driveKeys[RIGHT] - _driveKeys[LEFT]) + fabsf(_driveKeys[RIGHT] - _driveKeys[LEFT]) +
fabsf(_driveKeys[UP] - _driveKeys[DOWN]); fabsf(_driveKeys[UP] - _driveKeys[DOWN]);
bool standingOnFloor = false; bool walkingOnFloor = false;
float gravityLength = glm::length(_gravity); float gravityLength = glm::length(_gravity);
if (gravityLength > EPSILON) { if (gravityLength > EPSILON) {
const CapsuleShape& boundingShape = _skeletonModel.getBoundingShape(); const CapsuleShape& boundingShape = _skeletonModel.getBoundingShape();
@ -166,18 +166,20 @@ void MyAvatar::simulate(float deltaTime) {
glm::vec3 bottomOfBoundingCapsule = startCap + (boundingShape.getRadius() / gravityLength) * _gravity; glm::vec3 bottomOfBoundingCapsule = startCap + (boundingShape.getRadius() / gravityLength) * _gravity;
float fallThreshold = 2.f * deltaTime * gravityLength; float fallThreshold = 2.f * deltaTime * gravityLength;
standingOnFloor = (glm::distance(bottomOfBoundingCapsule, _lastFloorContactPoint) < fallThreshold); walkingOnFloor = (glm::distance(bottomOfBoundingCapsule, _lastFloorContactPoint) < fallThreshold);
} }
if (keyboardInput > 0.0f || glm::length2(_velocity) > 0.0f || glm::length2(_thrust) > 0.0f || if (keyboardInput > 0.0f || glm::length2(_velocity) > 0.0f || glm::length2(_thrust) > 0.0f ||
! standingOnFloor) { ! walkingOnFloor) {
// apply gravity
_velocity += _scale * _gravity * (GRAVITY_EARTH * deltaTime); _velocity += _scale * _gravity * (GRAVITY_EARTH * deltaTime);
updateMotorFromKeyboard(deltaTime); // update motor and thrust
updateMotorFromKeyboard(deltaTime, walkingOnFloor);
applyMotor(deltaTime); applyMotor(deltaTime);
applyThrust(deltaTime); applyThrust(deltaTime);
// update position
if (glm::length2(_velocity) < EPSILON) { if (glm::length2(_velocity) < EPSILON) {
_velocity = glm::vec3(0.0f); _velocity = glm::vec3(0.0f);
} else { } else {
@ -647,7 +649,7 @@ void MyAvatar::updateOrientation(float deltaTime) {
setOrientation(orientation); setOrientation(orientation);
} }
void MyAvatar::updateMotorFromKeyboard(float deltaTime) { void MyAvatar::updateMotorFromKeyboard(float deltaTime, bool walking) {
// Increase motor velocity until its length is equal to _maxMotorSpeed. // Increase motor velocity until its length is equal to _maxMotorSpeed.
if (!(_motionBehaviors & AVATAR_MOTION_MOTOR_KEYBOARD_ENABLED)) { if (!(_motionBehaviors & AVATAR_MOTION_MOTOR_KEYBOARD_ENABLED)) {
// nothing to do // nothing to do
@ -671,18 +673,23 @@ void MyAvatar::updateMotorFromKeyboard(float deltaTime) {
// Compute motor magnitude // Compute motor magnitude
if (directionLength > EPSILON) { if (directionLength > EPSILON) {
direction /= directionLength; direction /= directionLength;
const float MIN_WALKING_SPEED = 2.0f; // the finalMotorSpeed depends on whether we are walking or not
const float MIN_KEYBOARD_CONTROL_SPEED = 2.0f;
const float MAX_WALKING_SPEED = 4.0f * MIN_KEYBOARD_CONTROL_SPEED;
float finalMaxMotorSpeed = walking ? MAX_WALKING_SPEED : _maxMotorSpeed;
float motorLength = glm::length(_motorVelocity); float motorLength = glm::length(_motorVelocity);
if (motorLength < MIN_WALKING_SPEED) { if (motorLength < MIN_KEYBOARD_CONTROL_SPEED) {
_motorVelocity = MIN_WALKING_SPEED * direction; // an active keyboard motor should never be slower than this
_motorVelocity = MIN_KEYBOARD_CONTROL_SPEED * direction;
} else { } else {
float MOTOR_LENGTH_TIMESCALE = 1.5f; float MOTOR_LENGTH_TIMESCALE = 1.5f;
float tau = glm::clamp(deltaTime / MOTOR_LENGTH_TIMESCALE, 0.0f, 1.0f); float tau = glm::clamp(deltaTime / MOTOR_LENGTH_TIMESCALE, 0.0f, 1.0f);
float INCREASE_FACTOR = 2.0f; float INCREASE_FACTOR = 2.0f;
//_motorVelocity *= 1.0f + tau * INCREASE_FACTOR; //_motorVelocity *= 1.0f + tau * INCREASE_FACTOR;
motorLength *= 1.0f + tau * INCREASE_FACTOR; motorLength *= 1.0f + tau * INCREASE_FACTOR;
if (motorLength > _maxMotorSpeed) { if (motorLength > finalMaxMotorSpeed) {
motorLength = _maxMotorSpeed; motorLength = finalMaxMotorSpeed;
} }
_motorVelocity = motorLength * direction; _motorVelocity = motorLength * direction;
} }

View file

@ -146,7 +146,7 @@ private:
// private methods // private methods
void updateOrientation(float deltaTime); void updateOrientation(float deltaTime);
void updateMotorFromKeyboard(float deltaTime); void updateMotorFromKeyboard(float deltaTime, bool walking);
float computeMotorTimescale(); float computeMotorTimescale();
void applyMotor(float deltaTime); void applyMotor(float deltaTime);
void applyThrust(float deltaTime); void applyThrust(float deltaTime);