Skip to content

Commit 877f4cc

Browse files
update
Made some more adjustments as well as added a predicted smooth dampened value to help with smoothing and to get closer to the target value on the non-authority side. Updated the Quaternion's Approximation to use the dot product instead.
1 parent f3d564e commit 877f4cc

File tree

4 files changed

+36
-23
lines changed

4 files changed

+36
-23
lines changed

com.unity.netcode.gameobjects/Runtime/Components/Interpolator/BufferedLinearInterpolator.cs

Lines changed: 27 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@ public void AddDeltaTime(float deltaTime)
7171
else
7272
{
7373
m_AverageDeltaTime += deltaTime;
74+
m_AverageDeltaTime *= 0.5f;
7475
}
7576
DeltaTime = Math.Min(DeltaTime + m_AverageDeltaTime, TimeToTargetValue);
7677
LerpT = TimeToTargetValue == 0.0f ? 1.0f : DeltaTime / TimeToTargetValue;
@@ -97,7 +98,7 @@ public void Reset(T currentValue)
9798
CurrentValue = currentValue;
9899
PreviousValue = currentValue;
99100
// When reset, we consider ourselves to have already arrived at the target (even if no target is set)
100-
LerpT = 1.0f;
101+
LerpT = 0.0f;
101102
EndTime = 0.0;
102103
StartTime = 0.0;
103104
ResetDelta();
@@ -169,6 +170,11 @@ public void Reset(T currentValue)
169170
/// </summary>
170171
private T m_RateOfChange;
171172

173+
/// <summary>
174+
/// Represents the predicted rate of change for the value being interpolated when smooth dampening is enabled.
175+
/// </summary>
176+
private T m_PredictedRateOfChange;
177+
172178
private bool m_IsAngularValue;
173179

174180
/// <summary>
@@ -225,8 +231,8 @@ private void InternalReset(T targetValue, double serverTime, bool isAngularValue
225231
/// <param name="maxDeltaTime">maximum time delta which defines the maximum time duration when consuming more than one item from the buffer</param>
226232
private void TryConsumeFromBuffer(double renderTime, float minDeltaTime, float maxDeltaTime)
227233
{
228-
if (!InterpolateState.Target.HasValue || (InterpolateState.Target.Value.TimeSent <= renderTime &&
229-
(InterpolateState.TargetTimeAproximatelyReached() || IsAproximately(InterpolateState.CurrentValue, InterpolateState.Target.Value.Item))))
234+
if (!InterpolateState.Target.HasValue || (InterpolateState.Target.Value.TimeSent <= renderTime
235+
&& (InterpolateState.TargetTimeAproximatelyReached() || IsAproximately(InterpolateState.CurrentValue, InterpolateState.Target.Value.Item))))
230236
{
231237
BufferedItem? previousItem = null;
232238
var startTime = 0.0;
@@ -309,7 +315,15 @@ public T Update(float deltaTime, double tickLatencyAsTime, float minDeltaTime, f
309315
if (InterpolateState.Target.HasValue)
310316
{
311317
InterpolateState.AddDeltaTime(deltaTime);
312-
InterpolateState.CurrentValue = SmoothDamp(InterpolateState.CurrentValue, InterpolateState.Target.Value.Item, ref m_RateOfChange, InterpolateState.TimeToTargetValue, deltaTime);
318+
319+
// Smooth dampen our current time
320+
var current = SmoothDamp(InterpolateState.CurrentValue, InterpolateState.Target.Value.Item, ref m_RateOfChange, InterpolateState.TimeToTargetValue, InterpolateState.DeltaTime);
321+
// Smooth dampen a predicted time based on our average delta time
322+
var predict = SmoothDamp(InterpolateState.CurrentValue, InterpolateState.Target.Value.Item, ref m_PredictedRateOfChange, InterpolateState.TimeToTargetValue, InterpolateState.DeltaTime + InterpolateState.AverageDeltaTime);
323+
// Split the difference between the two.
324+
// Note: Since smooth dampening cannot over shoot, both current and predict will eventually become the same or will be very close to the same.
325+
// Upon stopping motion, the final resing value should be a very close aproximation of the authority side.
326+
InterpolateState.CurrentValue = Interpolate(current, predict, 0.5f);
313327
}
314328
m_NbItemsReceivedThisFrame = 0;
315329
return InterpolateState.CurrentValue;
@@ -381,15 +395,6 @@ private void TryConsumeFromBuffer(double renderTime, double serverTime)
381395
}
382396
}
383397
}
384-
#endregion
385-
386-
/// <summary>
387-
/// Used for internal testing
388-
/// </summary>
389-
internal T UpdateInternal(float deltaTime, NetworkTime serverTime)
390-
{
391-
return Update(deltaTime, serverTime.TimeTicksAgo(1).Time, serverTime.Time);
392-
}
393398

394399
/// <summary>
395400
/// Call to update the state of the interpolators using Lerp.
@@ -432,6 +437,15 @@ public T Update(float deltaTime, double renderTime, double serverTime)
432437
m_NbItemsReceivedThisFrame = 0;
433438
return InterpolateState.CurrentValue;
434439
}
440+
#endregion
441+
442+
/// <summary>
443+
/// Used for internal testing
444+
/// </summary>
445+
internal T UpdateInternal(float deltaTime, NetworkTime serverTime)
446+
{
447+
return Update(deltaTime, serverTime.TimeTicksAgo(1).Time, serverTime.Time);
448+
}
435449

436450
/// <summary>
437451
/// Add measurements to be used during interpolation. These will be buffered before being made available to be displayed as "latest value".

com.unity.netcode.gameobjects/Runtime/Components/Interpolator/BufferedLinearInterpolatorQuaternion.cs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -63,10 +63,7 @@ protected internal override Quaternion SmoothDamp(Quaternion current, Quaternion
6363
/// <inheritdoc />
6464
protected internal override bool IsAproximately(Quaternion first, Quaternion second, float precision)
6565
{
66-
return Mathf.Abs(first.x - second.x) <= precision &&
67-
Mathf.Abs(first.y - second.y) <= precision &&
68-
Mathf.Abs(first.z - second.z) <= precision &&
69-
Mathf.Abs(first.w - second.w) <= precision;
66+
return (1.0f - Quaternion.Dot(first, second)) <= precision;
7067
}
7168

7269
/// <inheritdoc />

com.unity.netcode.gameobjects/Runtime/Components/NetworkTransform.cs

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1820,11 +1820,13 @@ private bool CheckForStateChange(ref NetworkTransformState networkState, ref Tra
18201820
var positionThreshold = Vector3.one * PositionThreshold;
18211821
var rotationThreshold = Vector3.one * RotAngleThreshold;
18221822

1823-
if (m_UseRigidbodyForMotion)
1824-
{
1825-
positionThreshold = m_NetworkRigidbodyInternal.GetAdjustedPositionThreshold();
1826-
rotationThreshold = m_NetworkRigidbodyInternal.GetAdjustedRotationThreshold();
1827-
}
1823+
// NSS: Disabling this for the time being
1824+
// TODO: Determine if we actually need this and if not remove this from NetworkRigidBodyBase
1825+
//if (m_UseRigidbodyForMotion)
1826+
//{
1827+
// positionThreshold = m_NetworkRigidbodyInternal.GetAdjustedPositionThreshold();
1828+
// rotationThreshold = m_NetworkRigidbodyInternal.GetAdjustedRotationThreshold();
1829+
//}
18281830
#else
18291831
var position = InLocalSpace ? transformToUse.localPosition : transformToUse.position;
18301832
var rotation = InLocalSpace ? transformToUse.localRotation : transformToUse.rotation;

com.unity.netcode.gameobjects/Runtime/Timing/NetworkTimeSystem.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -225,7 +225,7 @@ public bool Advance(double deltaTimeSec)
225225
if (LastSyncedRttSec > 0.0f)
226226
{
227227
m_TickLatencyAverage = Mathf.Lerp(m_TickLatencyAverage, (float)((LastSyncedRttSec + deltaTimeSec) / m_TickFrequency), (float)deltaTimeSec);
228-
TickLatency = (int)Mathf.Max(1.0f, Mathf.Round(m_TickLatencyAverage));
228+
TickLatency = (int)Mathf.Max(2.0f, Mathf.Round(m_TickLatencyAverage));
229229
}
230230

231231
if (Math.Abs(m_DesiredLocalTimeOffset - m_CurrentLocalTimeOffset) > HardResetThresholdSec || Math.Abs(m_DesiredServerTimeOffset - m_CurrentServerTimeOffset) > HardResetThresholdSec)

0 commit comments

Comments
 (0)