//----------------------------------------------------------------------------- // Purpose: //----------------------------------------------------------------------------- void CASW_PropJeep::DampenForwardMotion( Vector &vecVehicleEyePos, QAngle &vecVehicleEyeAngles, float flFrameTime ) { // Get forward vector. Vector vecForward; AngleVectors( vecVehicleEyeAngles, &vecForward); // Simulate the eye position forward based on the data from last frame // (assumes no acceleration - it will get that from the "spring"). Vector vecCurrentEyePos = m_vecLastEyePos + m_vecEyeSpeed * flFrameTime; // Calculate target speed based on the current vehicle eye position and the last vehicle eye position and frametime. Vector vecVehicleEyeSpeed = ( vecVehicleEyePos - m_vecLastEyeTarget ) / flFrameTime; m_vecLastEyeTarget = vecVehicleEyePos; // Calculate the speed and position deltas. Vector vecDeltaSpeed = vecVehicleEyeSpeed - m_vecEyeSpeed; Vector vecDeltaPos = vecVehicleEyePos - vecCurrentEyePos; // Clamp. if ( vecDeltaPos.Length() > JEEP_DELTA_LENGTH_MAX ) { float flSign = vecForward.Dot( vecVehicleEyeSpeed ) >= 0.0f ? -1.0f : 1.0f; vecVehicleEyePos += flSign * ( vecForward * JEEP_DELTA_LENGTH_MAX ); m_vecLastEyePos = vecVehicleEyePos; m_vecEyeSpeed = vecVehicleEyeSpeed; return; } // Generate an updated (dampening) speed for use in next frames position extrapolation. float flCoefficients[2]; ComputePDControllerCoefficients( flCoefficients, r_JeepViewDampenFreq.GetFloat(), r_JeepViewDampenDamp.GetFloat(), flFrameTime ); m_vecEyeSpeed += ( ( flCoefficients[0] * vecDeltaPos + flCoefficients[1] * vecDeltaSpeed ) * flFrameTime ); // Save off data for next frame. m_vecLastEyePos = vecCurrentEyePos; // Move eye forward/backward. Vector vecForwardOffset = vecForward * ( vecForward.Dot( vecDeltaPos ) ); vecVehicleEyePos -= vecForwardOffset; }
//----------------------------------------------------------------------------- // Purpose: //----------------------------------------------------------------------------- void C_PropJeep::DampenForwardMotion( Vector &vecVehicleEyePos, QAngle &vecVehicleEyeAngles, float flFrameTime ) { // vecVehicleEyePos = real eye position this frame // m_vecLastEyePos = eye position last frame // m_vecEyeSpeed = eye speed last frame // vecPredEyePos = predicted eye position this frame (assuming no acceleration - it will get that from the pd controller). // vecPredEyeSpeed = predicted eye speed Vector vecPredEyePos = m_vecLastEyePos + m_vecEyeSpeed * flFrameTime; Vector vecPredEyeSpeed = m_vecEyeSpeed; // m_vecLastEyeTarget = real eye position last frame (used for speed calculation). // Calculate the approximate speed based on the current vehicle eye position and the eye position last frame. Vector vecVehicleEyeSpeed = ( vecVehicleEyePos - m_vecLastEyeTarget ) / flFrameTime; m_vecLastEyeTarget = vecVehicleEyePos; if (vecVehicleEyeSpeed.Length() == 0.0) return; // Calculate the delta between the predicted eye position and speed and the current eye position and speed. Vector vecDeltaSpeed = vecVehicleEyeSpeed - vecPredEyeSpeed; Vector vecDeltaPos = vecVehicleEyePos - vecPredEyePos; // Forward vector. Vector vecForward; AngleVectors( vecVehicleEyeAngles, &vecForward ); float flDeltaLength = vecDeltaPos.Length(); if ( flDeltaLength > JEEP_DELTA_LENGTH_MAX ) { // Clamp. float flDelta = flDeltaLength - JEEP_DELTA_LENGTH_MAX; if ( flDelta > 40.0f ) { // This part is a bit of a hack to get rid of large deltas (at level load, etc.). m_vecLastEyePos = vecVehicleEyePos; m_vecEyeSpeed = vecVehicleEyeSpeed; } else { // Position clamp. float flRatio = JEEP_DELTA_LENGTH_MAX / flDeltaLength; vecDeltaPos *= flRatio; Vector vecForwardOffset = vecForward * ( vecForward.Dot( vecDeltaPos ) ); vecVehicleEyePos -= vecForwardOffset; m_vecLastEyePos = vecVehicleEyePos; // Speed clamp. vecDeltaSpeed *= flRatio; float flCoefficients[2]; ComputePDControllerCoefficients( flCoefficients, r_JeepViewDampenFreq.GetFloat(), r_JeepViewDampenDamp.GetFloat(), flFrameTime ); m_vecEyeSpeed += ( ( flCoefficients[0] * vecDeltaPos + flCoefficients[1] * vecDeltaSpeed ) * flFrameTime ); } } else { // Generate an updated (dampening) speed for use in next frames position prediction. float flCoefficients[2]; ComputePDControllerCoefficients( flCoefficients, r_JeepViewDampenFreq.GetFloat(), r_JeepViewDampenDamp.GetFloat(), flFrameTime ); m_vecEyeSpeed += ( ( flCoefficients[0] * vecDeltaPos + flCoefficients[1] * vecDeltaSpeed ) * flFrameTime ); // Save off data for next frame. m_vecLastEyePos = vecPredEyePos; // Move eye forward/backward. Vector vecForwardOffset = vecForward * ( vecForward.Dot( vecDeltaPos ) ); vecVehicleEyePos -= vecForwardOffset; } }