1
- // COPYRIGHT 2010, 2011 by the Open Rails project.
1
+ // COPYRIGHT 2010, 2011 by the Open Rails project.
2
2
//
3
3
// This file is part of Open Rails.
4
4
//
@@ -28,6 +28,7 @@ public class SmoothedData
28
28
29
29
public readonly float SmoothPeriodS ;
30
30
31
+ protected float rate = 0 ;
31
32
protected float value = float . NaN ;
32
33
protected float smoothedValue = float . NaN ;
33
34
@@ -39,6 +40,8 @@ public SmoothedData()
39
40
public SmoothedData ( float smoothPeriodS )
40
41
{
41
42
SmoothPeriodS = smoothPeriodS ;
43
+ // Convert the input assuming 60 FPS (arbitary)
44
+ rate = ( float ) ( - 60 * Math . Log ( 1 - 1 / ( 60 * SmoothPeriodS ) ) ) ;
42
45
}
43
46
44
47
public void Update ( float periodS , float newValue )
@@ -56,13 +59,11 @@ public void Update(float periodS, float newValue)
56
59
57
60
protected void SmoothValue ( ref float smoothedValue , float periodS , float newValue )
58
61
{
59
- var rate = SmoothPeriodS / periodS ;
60
- if ( float . IsNaN ( smoothedValue ) || float . IsInfinity ( smoothedValue ) )
61
- smoothedValue = newValue ;
62
- else if ( rate < 1 )
62
+ var ratio = ( float ) Math . Exp ( - rate * periodS ) ;
63
+ if ( float . IsNaN ( smoothedValue ) || float . IsInfinity ( smoothedValue ) || ratio < 0.5 )
63
64
smoothedValue = newValue ;
64
65
else
65
- smoothedValue = ( smoothedValue * ( rate - 1 ) + newValue ) / rate ;
66
+ smoothedValue = smoothedValue * ratio + newValue * ( 1 - ratio ) ;
66
67
}
67
68
68
69
public void ForceSmoothValue ( float forcedValue )
0 commit comments