1- // COPYRIGHT 2010, 2011 by the Open Rails project.
1+ // COPYRIGHT 2010, 2011 by the Open Rails project.
22//
33// This file is part of Open Rails.
44//
@@ -28,6 +28,7 @@ public class SmoothedData
2828
2929 public readonly float SmoothPeriodS ;
3030
31+ protected float rate = 0 ;
3132 protected float value = float . NaN ;
3233 protected float smoothedValue = float . NaN ;
3334
@@ -39,6 +40,8 @@ public SmoothedData()
3940 public SmoothedData ( float smoothPeriodS )
4041 {
4142 SmoothPeriodS = smoothPeriodS ;
43+ // Convert the input assuming 60 FPS (arbitary)
44+ rate = ( float ) ( - 60 * Math . Log ( 1 - 1 / ( 60 * SmoothPeriodS ) ) ) ;
4245 }
4346
4447 public void Update ( float periodS , float newValue )
@@ -56,13 +59,11 @@ public void Update(float periodS, float newValue)
5659
5760 protected void SmoothValue ( ref float smoothedValue , float periodS , float newValue )
5861 {
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 )
6364 smoothedValue = newValue ;
6465 else
65- smoothedValue = ( smoothedValue * ( rate - 1 ) + newValue ) / rate ;
66+ smoothedValue = smoothedValue * ratio + newValue * ( 1 - ratio ) ;
6667 }
6768
6869 public void ForceSmoothValue ( float forcedValue )
0 commit comments