Skip to content

Commit 4a02caa

Browse files
committed
Refactor AntiChatter
1 parent 882d6c9 commit 4a02caa

1 file changed

Lines changed: 33 additions & 28 deletions

File tree

DevocubFilters/AntiChatter.cs

Lines changed: 33 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -14,63 +14,68 @@ public class AntiChatter : Interpolator
1414
{
1515
public AntiChatter(ITimer scheduler) : base(scheduler) { }
1616

17-
private Vector2 lastPos;
18-
private Vector2 targetPos;
17+
private bool isReady;
18+
private Vector2 position;
19+
private Vector2 prevTargetPos, targetPos, calcTarget;
1920
private SyntheticTabletReport report;
2021
private const float threshold = 0.9f;
2122
private float latency = 2.0f;
2223

2324
public override void UpdateState(SyntheticTabletReport report)
2425
{
2526
this.targetPos = report.Position;
26-
this.report = report;
27-
}
28-
29-
public override SyntheticTabletReport Interpolate()
30-
{
31-
this.report.Position = Filter(this.targetPos);
32-
return this.report;
33-
}
34-
35-
public Vector2 Filter(Vector2 point)
36-
{
37-
Vector2 calcTarget = new Vector2();
38-
Vector2 delta;
39-
float distance, weightModifier, predictionModifier;
4027

4128
if (PredictionEnabled)
4229
{
4330
// Calculate predicted position onNewPacket
44-
if (this.lastPos.X != point.X || this.lastPos.Y != point.Y)
31+
if (this.prevTargetPos.X != this.targetPos.X || this.prevTargetPos.Y != this.targetPos.Y)
4532
{
4633
// Calculate distance between last 2 packets and prediction
47-
delta = point - lastPos;
48-
distance = Vector2.Distance(lastPos, point);
49-
predictionModifier = 1 / Cosh((distance - PredictionOffsetX) * PredictionSharpness) * PredictionStrength + PredictionOffsetY;
34+
var delta = this.targetPos - this.prevTargetPos;
35+
var distance = Vector2.Distance(this.prevTargetPos, this.targetPos);
36+
var predictionModifier = 1 / Cosh((distance - PredictionOffsetX) * PredictionSharpness) * PredictionStrength + PredictionOffsetY;
5037

5138
// Apply prediction
5239
delta *= predictionModifier;
5340

5441
// Update predicted position
55-
calcTarget = point + delta;
42+
this.calcTarget = this.targetPos + delta;
5643

5744
// Update old position for further prediction
58-
this.lastPos = point;
45+
this.prevTargetPos = this.targetPos;
5946
}
6047
}
6148
else
62-
calcTarget = point;
49+
calcTarget = targetPos;
50+
51+
this.report = report;
52+
}
53+
54+
public override SyntheticTabletReport Interpolate()
55+
{
56+
this.report.Position = Filter(this.calcTarget);
57+
return this.report;
58+
}
59+
60+
public Vector2 Filter(Vector2 calcTarget)
61+
{
62+
if (!this.isReady)
63+
{
64+
this.position = calcTarget;
65+
this.isReady = true;
66+
return calcTarget;
67+
}
6368

64-
delta = calcTarget - lastPos;
65-
distance = Vector2.Distance(lastPos, calcTarget);
69+
var delta = calcTarget - this.position;
70+
var distance = Vector2.Distance(this.position, calcTarget);
6671

6772
float stepCount = Latency / TimerInterval;
6873
float target = 1 - threshold;
6974
float weight = (float)(1.0 - (1.0 / Pow((float)(1.0 / target), (float)(1.0 / stepCount))));
7075

7176
// Devocub smoothing
7277
// Increase weight of filter in {formula} times
73-
weightModifier = (float)(Pow(distance + AntichatterOffsetX, AntichatterStrength * -1) * AntichatterMultiplier);
78+
var weightModifier = (float)(Pow(distance + AntichatterOffsetX, AntichatterStrength * -1) * AntichatterMultiplier);
7479

7580
// Limit minimum
7681
if (weightModifier + AntichatterOffsetY < 0)
@@ -80,9 +85,9 @@ public Vector2 Filter(Vector2 point)
8085

8186
weightModifier = weight / weightModifier;
8287
weightModifier = Math.Clamp(weightModifier, 0, 1);
83-
this.lastPos += delta * weightModifier;
88+
this.position += delta * weightModifier;
8489

85-
return lastPos;
90+
return this.position;
8691
}
8792

8893
public static FilterStage FilterStage => FilterStage.PostTranspose;

0 commit comments

Comments
 (0)