diff options
| author | FluorescentCIAAfricanAmerican <[email protected]> | 2020-04-22 12:56:21 -0400 |
|---|---|---|
| committer | FluorescentCIAAfricanAmerican <[email protected]> | 2020-04-22 12:56:21 -0400 |
| commit | 3bf9df6b2785fa6d951086978a3e66f49427166a (patch) | |
| tree | 2c0f1f0c63c4832882bc93814ebd2c2b1c6224e5 /game/shared/apparent_velocity_helper.h | |
| download | archived-source-engine-2018-hl2-src-master.tar.xz archived-source-engine-2018-hl2-src-master.zip | |
Diffstat (limited to 'game/shared/apparent_velocity_helper.h')
| -rw-r--r-- | game/shared/apparent_velocity_helper.h | 75 |
1 files changed, 75 insertions, 0 deletions
diff --git a/game/shared/apparent_velocity_helper.h b/game/shared/apparent_velocity_helper.h new file mode 100644 index 0000000..65e28fb --- /dev/null +++ b/game/shared/apparent_velocity_helper.h @@ -0,0 +1,75 @@ +//========= Copyright Valve Corporation, All rights reserved. ============// +// +// Purpose: +// +//=============================================================================// + +#ifndef APPARENT_VELOCITY_HELPER_H +#define APPARENT_VELOCITY_HELPER_H +#ifdef _WIN32 +#pragma once +#endif + + +inline float CalcDistance( float x, float y ) +{ + return x - y; +} + +inline float CalcDistance( const Vector &a, const Vector &b ) +{ + return a.DistTo( b ); +} + + +template<class T> +class CDefaultCalcDistance +{ +public: + static inline float CalcDistance( const T &a, const T &b ) + { + return ::CalcDistance( a, b ); + } +}; + +class CCalcDistance2D +{ +public: + static inline float CalcDistance( const Vector &a, const Vector &b ) + { + return (a-b).Length2D(); + } +}; + + +template< class T, class Functor=CDefaultCalcDistance<T> > +class CApparentVelocity +{ +public: + CApparentVelocity(const T& t0) + { + m_LastTime = -1; + m_LastValue = t0; + } + + float AddSample( float time, T value ) + { + float flRet = 0; + if ( m_LastTime != -1 ) + { + flRet = Functor::CalcDistance(value, m_LastValue) / (time - m_LastTime); + } + + m_LastTime = time; + m_LastValue = value; + + return flRet; + } + +private: + T m_LastValue; + float m_LastTime; +}; + + +#endif // APPARENT_VELOCITY_HELPER_H |