Improve avatar movement, added Physics.cpp for routines to help with physics calculations

This commit is contained in:
Philip Rosedale 2013-07-11 15:53:07 -07:00
parent 4ec3bcb365
commit 79b2703e1c
3 changed files with 64 additions and 15 deletions

View file

@ -16,12 +16,14 @@
#include "Hand.h"
#include "Head.h"
#include "Log.h"
#include "Physics.h"
#include "ui/TextRenderer.h"
#include <NodeList.h>
#include <NodeTypes.h>
#include <PacketHeaders.h>
#include <OculusManager.h>
using namespace std;
const bool BALLS_ON = false;
@ -553,21 +555,13 @@ void Avatar::simulate(float deltaTime, Transmitter* transmitter) {
_bodyYawDelta *= bodySpinMomentum;
_bodyRollDelta *= bodySpinMomentum;
// Decay velocity. If velocity is really low, increase decay to simulate static friction
const float VELOCITY_DECAY_UNDER_THRUST = 0.2;
const float VELOCITY_FAST_DECAY = 0.6;
const float VELOCITY_SLOW_DECAY = 3.0;
const float VELOCITY_FAST_THRESHOLD = 2.0f;
float decayConstant, decay;
if (glm::length(_thrust) > 0.f) {
decayConstant = VELOCITY_DECAY_UNDER_THRUST;
} else if (glm::length(_velocity) > VELOCITY_FAST_THRESHOLD) {
decayConstant = VELOCITY_FAST_DECAY;
} else {
decayConstant = VELOCITY_SLOW_DECAY;
}
decay = glm::clamp(1.0f - decayConstant * deltaTime, 0.0f, 1.0f);
_velocity *= decay;
const float MAX_STATIC_FRICTION_VELOCITY = 0.25f;
const float STATIC_FRICTION_STRENGTH = 20.f;
applyStaticFriction(deltaTime, _velocity, MAX_STATIC_FRICTION_VELOCITY, STATIC_FRICTION_STRENGTH);
const float LINEAR_DAMPING_STRENGTH = 0.2f;
const float SQUARED_DAMPING_STRENGTH = 0.1f;
applyDamping(deltaTime, _velocity, LINEAR_DAMPING_STRENGTH, SQUARED_DAMPING_STRENGTH);
//pitch and roll the body as a function of forward speed and turning delta
const float BODY_PITCH_WHILE_WALKING = -20.0;

40
interface/src/Physics.cpp Normal file
View file

@ -0,0 +1,40 @@
//
// Physics.cpp
// hifi
//
// Created by Philip on July 11, 2013
//
// Routines to help with doing virtual world physics
//
#include <glm/glm.hpp>
#include <SharedUtil.h>
#include "Util.h"
#include "world.h"
#include "Physics.h"
//
// Applies static friction: maxVelocity is the largest velocity for which there
// there is friction, and strength is the amount of friction force applied to reduce
// velocity.
//
void applyStaticFriction(float deltaTime, glm::vec3& velocity, float maxVelocity, float strength) {
float v = glm::length(velocity);
if (v < maxVelocity) {
velocity *= glm::clamp((1.0f - deltaTime * strength * (1.f - v / maxVelocity)), 0.0f, 1.0f);
}
}
//
// Applies velocity damping, with a strength value for linear and squared velocity damping
//
void applyDamping(float deltaTime, glm::vec3& velocity, float linearStrength, float squaredStrength) {
if (squaredStrength == 0.f) {
velocity *= glm::clamp(1.f - deltaTime * linearStrength, 0.f, 1.f);
} else {
velocity *= glm::clamp(1.f - deltaTime * (linearStrength + glm::length(velocity) * squaredStrength), 0.f, 1.f);
}
}

15
interface/src/Physics.h Normal file
View file

@ -0,0 +1,15 @@
//
// Balls.h
// hifi
//
// Created by Philip on 4/25/13.
//
//
#ifndef hifi_Physics_h
#define hifi_Physics_h
void applyStaticFriction(float deltaTime, glm::vec3& velocity, float maxVelocity, float strength);
void applyDamping(float deltaTime, glm::vec3& velocity, float linearStrength, float squaredStrength);
#endif