aboutsummaryrefslogtreecommitdiff
path: root/sp/src/game/shared/apparent_velocity_helper.h
blob: 765a419d97e88118c336c91a3f4060c59445b4ea (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
//========= 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()
	{
		m_LastTime = -1;
	}

	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