Browse Source

engine: client: more accurate ScreenShake implementation, thanks @vasiavasiavasia95 for sharing it

pull/2/head
Alibek Omarov 1 year ago
parent
commit
1affc36f06
  1. 77
      engine/client/cl_game.c
  2. 15
      engine/client/cl_parse.c

77
engine/client/cl_game.c

@ -2119,58 +2119,52 @@ pfnCalcShake
*/ */
void GAME_EXPORT pfnCalcShake( void ) void GAME_EXPORT pfnCalcShake( void )
{ {
int i; screen_shake_t *const shake = &clgame.shake;
float fraction, freq; float frametime, fraction, freq;
float localAmp; int i;
if( clgame.shake.time == 0 )
return;
if(( cl.time > clgame.shake.time ) || clgame.shake.amplitude <= 0 || clgame.shake.frequency <= 0 ) if( cl.time > shake->time || shake->amplitude <= 0 || shake->frequency <= 0 || shake->duration <= 0 )
{ {
memset( &clgame.shake, 0, sizeof( clgame.shake )); // reset shake
if( shake->time != 0 )
{
shake->time = 0;
shake->applied_angle = 0;
VectorClear( shake->applied_offset );
}
return; return;
} }
if( cl.time > clgame.shake.next_shake ) frametime = cl_clientframetime();
if( cl.time > shake->next_shake )
{ {
// higher frequency means we recalc the extents more often and perturb the display again // get next shake time based on frequency over duration
clgame.shake.next_shake = cl.time + ( 1.0f / clgame.shake.frequency ); shake->next_shake = (float)cl.time + shake->frequency / shake->duration;
// compute random shake extents (the shake will settle down from this) // randomize each shake
for( i = 0; i < 3; i++ ) for( i = 0; i < 3; i++ )
clgame.shake.offset[i] = COM_RandomFloat( -clgame.shake.amplitude, clgame.shake.amplitude ); shake->offset[i] = COM_RandomFloat( -shake->amplitude, shake->amplitude );
clgame.shake.angle = COM_RandomFloat( -clgame.shake.amplitude * 0.25f, clgame.shake.amplitude * 0.25f ); shake->angle = COM_RandomFloat( -shake->amplitude * 0.25f, shake->amplitude * 0.25f );
} }
// ramp down amplitude over duration (fraction goes from 1 to 0 linearly with slope 1/duration) // get initial fraction and frequency values over the duration
fraction = ( clgame.shake.time - cl.time ) / clgame.shake.duration; fraction = ((float)cl.time - shake->time ) / shake->duration;
freq = fraction != 0.0f ? ( shake->frequency / fraction ) * shake->frequency : 0.0f;
// ramp up frequency over duration // quickly approach zero but apply time over sine wave
if( fraction ) fraction *= fraction * sin( cl.time * freq );
{
freq = ( clgame.shake.frequency / fraction );
}
else
{
freq = 0;
}
// square fraction to approach zero more quickly
fraction *= fraction;
// Sine wave that slowly settles to zero // apply shake offset
fraction = fraction * sin( cl.time * freq ); for( i = 0; i < 3; i++ )
shake->applied_offset[i] = shake->offset[i] * fraction;
// add to view origin // apply roll angle
VectorScale( clgame.shake.offset, fraction, clgame.shake.applied_offset ); shake->applied_angle = shake->angle * fraction;
// add to roll // decrease amplitude, but slower on longer shakes or higher frequency
clgame.shake.applied_angle = clgame.shake.angle * fraction; shake->amplitude -= shake->amplitude * ( frametime / ( shake->frequency * shake->duration ));
// drop amplitude a bit, less for higher frequency shakes
localAmp = clgame.shake.amplitude * ( host.frametime / ( clgame.shake.duration * clgame.shake.frequency ));
clgame.shake.amplitude -= localAmp;
} }
/* /*
@ -2181,8 +2175,11 @@ pfnApplyShake
*/ */
void GAME_EXPORT pfnApplyShake( float *origin, float *angles, float factor ) void GAME_EXPORT pfnApplyShake( float *origin, float *angles, float factor )
{ {
if( origin ) VectorMA( origin, factor, clgame.shake.applied_offset, origin ); if( origin )
if( angles ) angles[ROLL] += clgame.shake.applied_angle * factor; VectorMA( origin, factor, clgame.shake.applied_offset, origin );
if( angles )
angles[ROLL] += clgame.shake.applied_angle * factor;
} }
/* /*

15
engine/client/cl_parse.c

@ -1854,10 +1854,17 @@ Set screen shake
*/ */
void CL_ParseScreenShake( sizebuf_t *msg ) void CL_ParseScreenShake( sizebuf_t *msg )
{ {
clgame.shake.amplitude = (float)(word)MSG_ReadShort( msg ) * (1.0f / (float)(1<<12)); float amplitude = (float)(word)MSG_ReadShort( msg ) * ( 1.0f / (float)( 1 << 12 ));
clgame.shake.duration = (float)(word)MSG_ReadShort( msg ) * (1.0f / (float)(1<<12)); float duration = (float)(word)MSG_ReadShort( msg ) * ( 1.0f / (float)( 1 << 12 ));
clgame.shake.frequency = (float)(word)MSG_ReadShort( msg ) * (1.0f / (float)(1<<8)); float frequency = (float)(word)MSG_ReadShort( msg ) * ( 1.0f / (float)( 1 << 8 ));
clgame.shake.time = cl.time + Q_max( clgame.shake.duration, 0.01f );
// don't overwrite larger existing shake
if( amplitude > clgame.shake.amplitude )
clgame.shake.amplitude = amplitude;
clgame.shake.duration = duration;
clgame.shake.time = cl.time + clgame.shake.duration;
clgame.shake.frequency = frequency;
clgame.shake.next_shake = 0.0f; // apply immediately clgame.shake.next_shake = 0.0f; // apply immediately
} }

Loading…
Cancel
Save