move code into CharacterController::updateState()

This commit is contained in:
Andrew Meadows 2016-09-19 15:03:03 -07:00
parent 8dd5c9b92b
commit a8af8d6027
2 changed files with 100 additions and 98 deletions

View file

@ -581,14 +581,12 @@ void CharacterController::computeNewVelocity(btScalar dt, glm::vec3& velocity) {
velocity = bulletToGLM(btVelocity);
}
void CharacterController::preSimulation() {
if (_dynamicsWorld) {
quint64 now = usecTimestampNow();
// slam body transform
_rigidBody->setWorldTransform(btTransform(btTransform(_rotation, _position)));
btVector3 velocity = _rigidBody->getLinearVelocity();
_preSimulationVelocity = velocity;
void CharacterController::updateState() {
const btScalar FLY_TO_GROUND_THRESHOLD = 0.1f * _radius;
const btScalar GROUND_TO_FLY_THRESHOLD = 0.8f * _radius + _halfHeight;
const quint64 TAKE_OFF_TO_IN_AIR_PERIOD = 250 * MSECS_PER_SECOND;
const btScalar MIN_HOVER_HEIGHT = 2.5f;
const quint64 JUMP_TO_HOVER_PERIOD = 1100 * MSECS_PER_SECOND;
// scan for distant floor
// rayStart is at center of bottom sphere
@ -598,28 +596,25 @@ void CharacterController::preSimulation() {
btScalar rayLength = _radius + MAX_FALL_HEIGHT;
btVector3 rayEnd = rayStart - rayLength * _currentUp;
const btScalar FLY_TO_GROUND_THRESHOLD = 0.1f * _radius;
const btScalar GROUND_TO_FLY_THRESHOLD = 0.8f * _radius + _halfHeight;
const quint64 TAKE_OFF_TO_IN_AIR_PERIOD = 250 * MSECS_PER_SECOND;
const btScalar MIN_HOVER_HEIGHT = 2.5f;
const quint64 JUMP_TO_HOVER_PERIOD = 1100 * MSECS_PER_SECOND;
const btScalar MAX_WALKING_SPEED = 2.5f;
const quint64 RAY_HIT_START_PERIOD = 500 * MSECS_PER_SECOND;
ClosestNotMe rayCallback(_rigidBody);
rayCallback.m_closestHitFraction = 1.0f;
_dynamicsWorld->rayTest(rayStart, rayEnd, rayCallback);
bool rayHasHit = rayCallback.hasHit();
quint64 now = usecTimestampNow();
if (rayHasHit) {
_rayHitStartTime = now;
_floorDistance = rayLength * rayCallback.m_closestHitFraction - (_radius + _halfHeight);
} else if ((now - _rayHitStartTime) < RAY_HIT_START_PERIOD) {
} else {
const quint64 RAY_HIT_START_PERIOD = 500 * MSECS_PER_SECOND;
if ((now - _rayHitStartTime) < RAY_HIT_START_PERIOD) {
rayHasHit = true;
} else {
_floorDistance = FLT_MAX;
}
}
// record a time stamp when the jump button was first pressed.
bool jumpButtonHeld = _pendingFlags & PENDING_FLAG_JUMP;
if ((_previousFlags & PENDING_FLAG_JUMP) != (_pendingFlags & PENDING_FLAG_JUMP)) {
if (_pendingFlags & PENDING_FLAG_JUMP) {
_jumpButtonDownStartTime = now;
@ -627,10 +622,7 @@ void CharacterController::preSimulation() {
}
}
bool jumpButtonHeld = _pendingFlags & PENDING_FLAG_JUMP;
btVector3 actualHorizVelocity = velocity - velocity.dot(_currentUp) * _currentUp;
bool flyingFast = _state == State::Hover && actualHorizVelocity.length() > (MAX_WALKING_SPEED * 0.75f);
btVector3 velocity = _preSimulationVelocity;
// OUTOFBODY_HACK -- disable normal state transitions while collisionless
if (_collisionGroup == BULLET_COLLISION_GROUP_MY_AVATAR) {
@ -673,6 +665,10 @@ void CharacterController::preSimulation() {
break;
}
case State::Hover:
btVector3 actualHorizVelocity = velocity - velocity.dot(_currentUp) * _currentUp;
const btScalar MAX_WALKING_SPEED = 2.5f;
bool flyingFast = _state == State::Hover && actualHorizVelocity.length() > (MAX_WALKING_SPEED * 0.75f);
if ((_floorDistance < MIN_HOVER_HEIGHT) && !jumpButtonHeld && !flyingFast) {
SET_STATE(State::InAir, "near ground");
} else if (((_floorDistance < FLY_TO_GROUND_THRESHOLD) || _hasSupport) && !flyingFast) {
@ -684,7 +680,7 @@ void CharacterController::preSimulation() {
SET_STATE(State::Hover, "kinematic motion"); // HACK
}
} else {
// OUTOFBODY_HACK -- in collisionless state switch between Ground and Hover states
// OUTOFBODY_HACK -- in collisionless state switch only between Ground and Hover states
if (rayHasHit) {
SET_STATE(State::Ground, "collisionless above ground");
} else {
@ -693,6 +689,15 @@ void CharacterController::preSimulation() {
}
}
void CharacterController::preSimulation() {
if (_dynamicsWorld) {
// slam body transform and remember velocity
_rigidBody->setWorldTransform(btTransform(btTransform(_rotation, _position)));
_preSimulationVelocity = _rigidBody->getLinearVelocity();
updateState();
}
_previousFlags = _pendingFlags;
_pendingFlags &= ~PENDING_FLAG_JUMP;
@ -702,13 +707,9 @@ void CharacterController::preSimulation() {
}
void CharacterController::postSimulation() {
// postSimulation() exists for symmetry and just in case we need to do something here later
btVector3 velocity = _rigidBody->getLinearVelocity();
_velocityChange = velocity - _preSimulationVelocity;
_velocityChange = _rigidBody->getLinearVelocity() - _preSimulationVelocity;
}
bool CharacterController::getRigidBodyLocation(glm::vec3& avatarRigidBodyPosition, glm::quat& avatarRigidBodyRotation) {
if (!_rigidBody) {
return false;

View file

@ -109,6 +109,7 @@ public:
};
State getState() const { return _state; }
void updateState();
void setLocalBoundingBox(const glm::vec3& minCorner, const glm::vec3& scale);