Skip to content

Commit

Permalink
add average jitter info. Also precalculate jitter inside jitterSample…
Browse files Browse the repository at this point in the history
…s (more stable calculation)
  • Loading branch information
RevenantX committed Nov 26, 2024
1 parent fef2531 commit 88be45f
Showing 1 changed file with 11 additions and 3 deletions.
14 changes: 11 additions & 3 deletions LiteEntitySystem/ClientEntityManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,11 @@ public sealed class ClientEntityManager : EntityManager
/// </summary>
public float NetworkJitter { get; private set; }

/// <summary>
/// Average jitter
/// </summary>
public float AverageJitter => _jitterMiddle;

/// <summary>
/// Preferred input and incoming states buffer length in seconds lowest bound
/// Buffer automatically increases to Jitter time + PreferredBufferTimeLowest
Expand Down Expand Up @@ -178,6 +183,7 @@ public SyncCallInfo(OnSyncCallDelegate onSync, InternalEntity entity, int prevDa
private readonly float[] _jitterSamples = new float[10];
private int _jitterSampleIdx;
private readonly Stopwatch _jitterTimer = new();
private float _jitterPrevTime;
private float _jitterMiddle;

//local player
Expand Down Expand Up @@ -392,7 +398,9 @@ public unsafe DeserializeResult Deserialize(ReadOnlySpan<byte> inData)
}

//sample jitter
_jitterSamples[_jitterSampleIdx] = _jitterTimer.ElapsedMilliseconds / 1000f;
float currentJitterTimer = _jitterTimer.ElapsedMilliseconds / 1000f;
_jitterSamples[_jitterSampleIdx] = Math.Abs(currentJitterTimer - _jitterPrevTime);
_jitterPrevTime = currentJitterTimer;
_jitterSampleIdx = (_jitterSampleIdx + 1) % _jitterSamples.Length;
//reset timer
_jitterTimer.Restart();
Expand Down Expand Up @@ -449,9 +457,9 @@ private bool PreloadNextState()

//get max and middle jitter
_jitterMiddle = 0f;
for (int i = 0; i < _jitterSamples.Length - 1; i++)
for (int i = 0; i < _jitterSamples.Length; i++)
{
float jitter = Math.Abs(_jitterSamples[i] - _jitterSamples[i + 1]);
float jitter = _jitterSamples[i];
if (jitter > NetworkJitter)
NetworkJitter = jitter;
_jitterMiddle += jitter;
Expand Down

0 comments on commit 88be45f

Please sign in to comment.