I was going through the player movement code for the source engine when I stumbled upon the following function:
void CGameMovement::Accelerate( Vector& wishdir, float wishspeed, float accel )
{
int i;
float addspeed, accelspeed, currentspeed;
// This gets overridden because some games (CSPort) want to allow dead (observer) players
// to be able to move around.
if ( !CanAccelerate() )
return;
// See if we are changing direction a bit
currentspeed = mv->m_vecVelocity.Dot(wishdir);
// Reduce wishspeed by the amount of veer.
addspeed = wishspeed - currentspeed;
// If not going to add any speed, done.
if (addspeed <= 0)
return;
// Determine amount of accleration.
accelspeed = accel * gpGlobals->frametime * wishspeed * player->m_surfaceFriction;
// Cap at addspeed
if (accelspeed > addspeed)
accelspeed = addspeed;
// Adjust velocity.
for (i=0 ; i<3 ; i++)
{
mv->m_vecVelocity[i] += accelspeed * wishdir[i];
}
}
Although I do understand the concept of utilising the wishdir & wishspeed to calculate the velocity increment, I cannot figure out why they use the wishspeed and m_surfaceFriction in the calculation of accelspeed:
accelspeed = accel * gpGlobals->frametime * wishspeed * player->m_surfaceFriction;
I think it somehow compensates for the reduction in velocity due to friction that was calculated earlier, but multiplying these two variables with accel * gpGlobals->frametime does not seem to make physical sense.
Would somebody be able to explain the thoughts behind this calculation and why this extra wishspeed * player->m_surfaceFriction factor is applied to the accelspeed?