Skip to content

Commit

Permalink
Framerate-independent idPlayer::Move() and idAI::AdjustFlyingAngles()
Browse files Browse the repository at this point in the history
thanks to Github user tyuah8:
dhewm#584 (comment)
  • Loading branch information
DanielGibson committed Jul 25, 2024
1 parent cbfb298 commit 01d778c
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 6 deletions.
6 changes: 5 additions & 1 deletion neo/d3xp/Player.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7117,8 +7117,12 @@ void idPlayer::Move( void ) {
if ( spectating ) {
SetEyeHeight( newEyeOffset );
} else {
// DG: make this framerate-independent, code suggested by tyuah8 on Github
// https://en.wikipedia.org/wiki/Exponential_smoothing#Time_constant
const float tau = -16.0f / idMath::Log( pm_crouchrate.GetFloat() );
const float a = 1.0f - idMath::Exp( -gameLocal.gameMsec / tau );
// smooth out duck height changes
SetEyeHeight( EyeHeight() * pm_crouchrate.GetFloat() + newEyeOffset * ( 1.0f - pm_crouchrate.GetFloat() ) );
SetEyeHeight( EyeHeight() * (1.0f - a) + newEyeOffset * a );
}
}

Expand Down
11 changes: 9 additions & 2 deletions neo/d3xp/ai/AI.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2984,8 +2984,15 @@ void idAI::AdjustFlyingAngles( void ) {
}
}

fly_roll = fly_roll * 0.95f + roll * 0.05f;
fly_pitch = fly_pitch * 0.95f + pitch * 0.05f;
// DG: make this framerate-independent, code suggested by tyuah8 on Github
// https://en.wikipedia.org/wiki/Exponential_smoothing#Time_constant
static const float tau = -16.0f / idMath::Log( 0.95f );
// TODO: use gameLocal.gameMsec instead, so it's not affected by slow motion?
// enemies turning slower in slowmo seems logical, but the original code
// just increased every tic and thus was independent of slowmo
const float a = 1.0f - idMath::Exp( -gameLocal.msec / tau );
fly_roll = fly_roll * (1.0f - a) + roll * a;
fly_pitch = fly_pitch * (1.0f - a) + pitch * a;

if ( flyTiltJoint != INVALID_JOINT ) {
animator.SetJointAxis( flyTiltJoint, JOINTMOD_WORLD, idAngles( fly_pitch, 0.0f, fly_roll ).ToMat3() );
Expand Down
6 changes: 5 additions & 1 deletion neo/game/Player.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6014,8 +6014,12 @@ void idPlayer::Move( void ) {
if ( spectating ) {
SetEyeHeight( newEyeOffset );
} else {
// DG: make this framerate-independent, code suggested by tyuah8 on Github
// https://en.wikipedia.org/wiki/Exponential_smoothing#Time_constant
const float tau = -16.0f / idMath::Log( pm_crouchrate.GetFloat() );
const float a = 1.0f - idMath::Exp( -gameLocal.gameMsec / tau );
// smooth out duck height changes
SetEyeHeight( EyeHeight() * pm_crouchrate.GetFloat() + newEyeOffset * ( 1.0f - pm_crouchrate.GetFloat() ) );
SetEyeHeight( EyeHeight() * (1.0f - a) + newEyeOffset * a );
}
}

Expand Down
8 changes: 6 additions & 2 deletions neo/game/ai/AI.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2896,8 +2896,12 @@ void idAI::AdjustFlyingAngles( void ) {
}
}

fly_roll = fly_roll * 0.95f + roll * 0.05f;
fly_pitch = fly_pitch * 0.95f + pitch * 0.05f;
// DG: make this framerate-independent, code suggested by tyuah8 on Github
// https://en.wikipedia.org/wiki/Exponential_smoothing#Time_constant
static const float tau = -16.0f / idMath::Log( 0.95f );
const float a = 1.0f - idMath::Exp( -gameLocal.msec / tau );
fly_roll = fly_roll * (1.0f - a) + roll * a;
fly_pitch = fly_pitch * (1.0f - a) + pitch * a;

if ( flyTiltJoint != INVALID_JOINT ) {
animator.SetJointAxis( flyTiltJoint, JOINTMOD_WORLD, idAngles( fly_pitch, 0.0f, fly_roll ).ToMat3() );
Expand Down

0 comments on commit 01d778c

Please sign in to comment.