Skip to content

Commit 39fb000

Browse files
committed
fix: Use properly FPS-independent smoothing function
1 parent 79cea88 commit 39fb000

File tree

2 files changed

+11
-10
lines changed

2 files changed

+11
-10
lines changed

Source/ORTS.Common/SmoothedData.cs

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
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)

Source/Tests/Orts.Common/SmoothedData.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,11 +24,11 @@ public static class SmoothedData
2424
{
2525
[Theory]
2626
// FPS-like tests
27-
[InlineData(5, 3, 0.318)]
28-
[InlineData(10, 3, 0.337)]
29-
[InlineData(30, 3, 0.350)]
27+
[InlineData(5, 3, 0.353)]
28+
[InlineData(10, 3, 0.353)]
29+
[InlineData(30, 3, 0.353)]
3030
[InlineData(60, 3, 0.353)]
31-
[InlineData(120, 3, 0.355)]
31+
[InlineData(120, 3, 0.353)]
3232
// Physics-like tests
3333
[InlineData(60, 1, 0.000)] // Exhaust particles
3434
[InlineData(60, 2, 0.066)] // Smoke colour

0 commit comments

Comments
 (0)