jump to hover works again

This commit is contained in:
Andrew Meadows 2015-04-07 16:23:06 -07:00
parent b5f0c57dd1
commit d9146b1033
2 changed files with 33 additions and 29 deletions

View file

@ -59,6 +59,7 @@ DynamicCharacterController::DynamicCharacterController(AvatarData* avatarData) {
_isJumping = false; _isJumping = false;
_isFalling = false; _isFalling = false;
_isHovering = true; _isHovering = true;
_isPushingUp = false;
_jumpToHoverStart = 0; _jumpToHoverStart = 0;
_pendingFlags = PENDING_FLAG_UPDATE_SHAPE; _pendingFlags = PENDING_FLAG_UPDATE_SHAPE;
@ -102,8 +103,12 @@ void DynamicCharacterController::playerStep(btCollisionWorld* dynaWorld,btScalar
btVector3 desiredVelocity = _walkVelocity; btVector3 desiredVelocity = _walkVelocity;
btScalar desiredSpeed = desiredVelocity.length(); btScalar desiredSpeed = desiredVelocity.length();
const btScalar MIN_SPEED = 0.001f; const btScalar MIN_UP_PUSH = 0.1f;
if (desiredVelocity.dot(_currentUp) < MIN_UP_PUSH) {
_isPushingUp = false;
}
const btScalar MIN_SPEED = 0.001f;
if (_isHovering) { if (_isHovering) {
if (desiredSpeed < MIN_SPEED) { if (desiredSpeed < MIN_SPEED) {
if (actualSpeed < MIN_SPEED) { if (actualSpeed < MIN_SPEED) {
@ -139,35 +144,36 @@ void DynamicCharacterController::playerStep(btCollisionWorld* dynaWorld,btScalar
_rigidBody->setLinearVelocity(actualVelocity + velocityCorrection); _rigidBody->setLinearVelocity(actualVelocity + velocityCorrection);
} }
} else { } else {
// falling or jumping // transitioning to flying
const btScalar FALL_ACCELERATION_TIMESCALE = 2.0f; btVector3 velocityCorrection = desiredVelocity - actualVelocity;
btScalar tau = dt / FALL_ACCELERATION_TIMESCALE; const btScalar FLY_ACCELERATION_TIMESCALE = 0.2f;
btVector3 velocityCorrection = tau * (desiredVelocity - actualVelocity); btScalar tau = dt / FLY_ACCELERATION_TIMESCALE;
// subtract vertical component if (!_isPushingUp) {
velocityCorrection -= velocityCorrection.dot(_currentUp) * _currentUp; // actually falling --> compute a different velocity attenuation factor
_rigidBody->setLinearVelocity(actualVelocity + velocityCorrection); const btScalar FALL_ACCELERATION_TIMESCALE = 2.0f;
tau = dt / FALL_ACCELERATION_TIMESCALE;
// zero vertical component
velocityCorrection -= velocityCorrection.dot(_currentUp) * _currentUp;
}
_rigidBody->setLinearVelocity(actualVelocity + tau * velocityCorrection);
} }
} }
} }
bool DynamicCharacterController::canJump() const {
return onGround() && !_isJumping;
}
void DynamicCharacterController::jump() { void DynamicCharacterController::jump() {
_pendingFlags |= PENDING_FLAG_JUMP;
// check for case where user is holding down "jump" key... // check for case where user is holding down "jump" key...
// we'll eventually tansition to "hover" // we'll eventually tansition to "hover"
if (!_isHovering) { if (!_isJumping) {
if (!_isJumping) { if (!_isHovering) {
_jumpToHoverStart = usecTimestampNow(); _jumpToHoverStart = usecTimestampNow();
} else { _pendingFlags |= PENDING_FLAG_JUMP;
quint64 now = usecTimestampNow(); }
const quint64 JUMP_TO_HOVER_PERIOD = USECS_PER_SECOND; } else {
if (now - _jumpToHoverStart > JUMP_TO_HOVER_PERIOD) { quint64 now = usecTimestampNow();
_isHovering = true; const quint64 JUMP_TO_HOVER_PERIOD = 75 * (USECS_PER_SECOND / 100);
} if (now - _jumpToHoverStart > JUMP_TO_HOVER_PERIOD) {
_isPushingUp = true;
setHovering(true);
} }
} }
} }
@ -188,7 +194,6 @@ void DynamicCharacterController::setHovering(bool hover) {
} else { } else {
_rigidBody->setGravity(DEFAULT_GRAVITY * _currentUp); _rigidBody->setGravity(DEFAULT_GRAVITY * _currentUp);
} }
btVector3 g = _rigidBody->getGravity();
} }
} }
} }
@ -368,7 +373,7 @@ void DynamicCharacterController::preSimulation(btScalar timeStep) {
if (rayCallback.hasHit()) { if (rayCallback.hasHit()) {
_floorDistance = rayLength * rayCallback.m_closestHitFraction - _radius; _floorDistance = rayLength * rayCallback.m_closestHitFraction - _radius;
const btScalar MIN_HOVER_HEIGHT = 3.0f; const btScalar MIN_HOVER_HEIGHT = 3.0f;
if (_isHovering && _floorDistance < MIN_HOVER_HEIGHT) { if (_isHovering && _floorDistance < MIN_HOVER_HEIGHT && !_isPushingUp) {
setHovering(false); setHovering(false);
} }
// TODO: use collision events rather than ray-trace test to disable jumping // TODO: use collision events rather than ray-trace test to disable jumping
@ -385,9 +390,7 @@ void DynamicCharacterController::preSimulation(btScalar timeStep) {
if (_pendingFlags & PENDING_FLAG_JUMP) { if (_pendingFlags & PENDING_FLAG_JUMP) {
_pendingFlags &= ~ PENDING_FLAG_JUMP; _pendingFlags &= ~ PENDING_FLAG_JUMP;
if (canJump()) { if (onGround()) {
// TODO: make jump work
//_verticalVelocity = _jumpSpeed;
_isJumping = true; _isJumping = true;
btVector3 velocity = _rigidBody->getLinearVelocity(); btVector3 velocity = _rigidBody->getLinearVelocity();
velocity += _jumpSpeed * _currentUp; velocity += _jumpSpeed * _currentUp;

View file

@ -37,6 +37,7 @@ protected:
bool _isJumping; bool _isJumping;
bool _isFalling; bool _isFalling;
bool _isHovering; bool _isHovering;
bool _isPushingUp;
quint64 _jumpToHoverStart; quint64 _jumpToHoverStart;
uint32_t _pendingFlags; uint32_t _pendingFlags;
@ -68,8 +69,8 @@ public:
virtual void preStep(btCollisionWorld* collisionWorld); virtual void preStep(btCollisionWorld* collisionWorld);
virtual void playerStep(btCollisionWorld* collisionWorld, btScalar dt); virtual void playerStep(btCollisionWorld* collisionWorld, btScalar dt);
virtual bool canJump() const; virtual bool canJump() const { assert(false); return false; } // never call this
virtual void jump(); virtual void jump(); // call this every frame the jump button is pressed
virtual bool onGround() const; virtual bool onGround() const;
bool isHovering() const { return _isHovering; } bool isHovering() const { return _isHovering; }
void setHovering(bool enabled); void setHovering(bool enabled);