try to interpolate based on local time

This commit is contained in:
in0finite 2022-05-04 11:10:00 +02:00
parent 550c6544f7
commit 730456a83b

View file

@ -76,6 +76,9 @@ namespace SanAndreasUnity.Net
// timestamp of server when this data was sent (batch timestamp)
public double RemoteTimeStamp;
// estimated local time for this snapshot
public double EstimatedLocalTime;
public void Apply(Transform tr)
{
tr.localPosition = this.Position;
@ -204,6 +207,7 @@ namespace SanAndreasUnity.Net
if (m_snapshotBuffer.Count == 0)
{
syncData.EstimatedLocalTime = NetworkTime.localTime;
m_snapshotBuffer.Enqueue(syncData);
m_lastAddedSnapshot = syncData;
return;
@ -215,6 +219,7 @@ namespace SanAndreasUnity.Net
return;
}
syncData.EstimatedLocalTime = m_lastAddedSnapshot.EstimatedLocalTime + (syncData.RemoteTimeStamp - m_lastAddedSnapshot.RemoteTimeStamp);
m_snapshotBuffer.Enqueue(syncData);
m_lastAddedSnapshot = syncData;
}
@ -259,7 +264,7 @@ namespace SanAndreasUnity.Net
if (null == m_snapshotBuffer || m_snapshotBuffer.Count == 0)
return;
double currentNetworkTime = NetworkTime.time - m_parameters.snapshotLatency;
double currentNetworkTime = NetworkTime.localTime - m_parameters.snapshotLatency;
// find higher and lower snapshots - those are snapshots that surround the 'currentNetworkTime'
@ -271,7 +276,7 @@ namespace SanAndreasUnity.Net
bool foundFirstHigher = false;
foreach (SyncData snapshot in m_snapshotBuffer)
{
if (snapshot.RemoteTimeStamp >= currentNetworkTime)
if (snapshot.EstimatedLocalTime >= currentNetworkTime)
{
syncDataOfHigher = snapshot;
if (isFirst)
@ -292,7 +297,7 @@ namespace SanAndreasUnity.Net
}
// interpolate between lower and higher syncdata
double ratio = (currentNetworkTime - syncDataOfLower.RemoteTimeStamp) / (syncDataOfHigher.RemoteTimeStamp - syncDataOfLower.RemoteTimeStamp);
double ratio = (currentNetworkTime - syncDataOfLower.EstimatedLocalTime) / (syncDataOfHigher.EstimatedLocalTime - syncDataOfLower.EstimatedLocalTime);
if (double.IsNaN(ratio))
ratio = 1;
ratio = Mathd.Clamp01(ratio);
@ -300,7 +305,7 @@ namespace SanAndreasUnity.Net
this.Apply(interpolated);
// remove old snapshots, but be careful not to remove the current lower snapshot
this.RemoveOldSnapshots(System.Math.Min(currentNetworkTime, syncDataOfLower.RemoteTimeStamp));
this.RemoveOldSnapshots(System.Math.Min(currentNetworkTime, syncDataOfLower.EstimatedLocalTime));
}
@ -312,7 +317,7 @@ namespace SanAndreasUnity.Net
break;
SyncData syncData = m_snapshotBuffer.Peek();
if (syncData.RemoteTimeStamp >= currentNetworkTime)
if (syncData.EstimatedLocalTime >= currentNetworkTime)
break;
m_snapshotBuffer.Dequeue();