Optimized lag compensation

This commit is contained in:
Albin Corén 2018-03-06 13:48:23 +01:00
parent e4d9a0166d
commit acbadc763f

View File

@ -10,7 +10,7 @@ namespace MLAPI.MonoBehaviours.Core
public class TrackedObject : MonoBehaviour public class TrackedObject : MonoBehaviour
{ {
internal Dictionary<float, TrackedPointData> FrameData = new Dictionary<float, TrackedPointData>(); internal Dictionary<float, TrackedPointData> FrameData = new Dictionary<float, TrackedPointData>();
internal List<float> Framekeys = new List<float>() { 0 }; internal LinkedList<float> Framekeys = new LinkedList<float>();
private Vector3 savedPosition; private Vector3 savedPosition;
private Quaternion savedRotation; private Quaternion savedRotation;
@ -22,15 +22,21 @@ namespace MLAPI.MonoBehaviours.Core
float targetTime = currentTime - secondsAgo; float targetTime = currentTime - secondsAgo;
float previousTime = 0; float previousTime = 0;
float nextTime = 0; float nextTime = 0;
for (int i = 1; i < Framekeys.Count; i++) LinkedListNode<float> node = Framekeys.First;
float previousValue = 0f;
while(node != null)
{ {
if (Framekeys[i - 1] <= targetTime && Framekeys[i] >= targetTime) if(previousValue <= targetTime && node.Value >= targetTime)
{ {
previousTime = Framekeys[i]; previousTime = previousValue;
nextTime = Framekeys[i + 1]; nextTime = node.Value;
break; break;
} }
else
{
previousValue = node.Value;
node = node.Next;
}
} }
float timeBetweenFrames = nextTime - previousTime; float timeBetweenFrames = nextTime - previousTime;
@ -48,35 +54,35 @@ namespace MLAPI.MonoBehaviours.Core
void Start() void Start()
{ {
Framekeys.AddFirst(0);
LagCompensationManager.SimulationObjects.Add(this); LagCompensationManager.SimulationObjects.Add(this);
} }
void OnDestroy() void OnDestroy()
{ {
Framekeys.Clear();
FrameData.Clear();
LagCompensationManager.SimulationObjects.Remove(this); LagCompensationManager.SimulationObjects.Remove(this);
} }
internal void AddFrame() internal void AddFrame()
{ {
float currentTime = Time.time; float currentTime = Time.time;
for (int i = 0; i < Framekeys.Count; i++) LinkedListNode<float> node = Framekeys.First;
LinkedListNode<float> nextNode = node.Next;
while (currentTime - node.Value >= NetworkingManager.singleton.NetworkConfig.SecondsHistory)
{ {
if (currentTime - Framekeys[i] >= NetworkingManager.singleton.NetworkConfig.SecondsHistory) nextNode = node.Next;
{ FrameData.Remove(node.Value);
for (int j = 0; j < i; j++) Framekeys.RemoveFirst();
{ node = nextNode;
FrameData.Remove(Framekeys[0]);
//This is not good for performance. Other datatypes should be concidered.
Framekeys.RemoveAt(0);
}
}
} }
FrameData.Add(Time.time, new TrackedPointData() FrameData.Add(Time.time, new TrackedPointData()
{ {
position = transform.position, position = transform.position,
rotation = transform.rotation rotation = transform.rotation
}); });
Framekeys.Add(Time.time); Framekeys.AddLast(Time.time);
} }
} }
} }