interp while ahead; slow down while behind

This commit is contained in:
mischa 2024-04-25 13:16:59 +08:00
parent 5441ed85d2
commit c7fe9a939f
3 changed files with 34 additions and 10 deletions

View File

@ -214,6 +214,7 @@ protected override void Update()
Vector3 lastSetPosition = Vector3.zero; Vector3 lastSetPosition = Vector3.zero;
Vector3 interpolatedPosition = Vector3.zero; Vector3 interpolatedPosition = Vector3.zero;
Vector3 lastValidVelocity = Vector3.zero;
protected override void ApplySnapshot(NTSnapshot interpolated) protected override void ApplySnapshot(NTSnapshot interpolated)
{ {
interpolatedPosition = interpolated.position; interpolatedPosition = interpolated.position;
@ -274,21 +275,44 @@ protected override void ApplySnapshot(NTSnapshot interpolated)
// would the new position be ahead of us, or behind us? // would the new position be ahead of us, or behind us?
Vector3 velocity = predictedRigidbody.velocity; // this is where we are going Vector3 velocity = predictedRigidbody.velocity; // this is where we are going
// we slow down velocity while server state is behind us.
// if it slows down to zero then the 'isAhead' check won't work since it compares velocity.
// solution: always store the last valid velocity, and use that if needed.
if (velocity == Vector3.zero)
{
velocity = lastValidVelocity;
}
else
{
lastValidVelocity = velocity;
}
Vector3 targetDelta = targetPosition - currentPosition; // this is where we would go Vector3 targetDelta = targetPosition - currentPosition; // this is where we would go
float dot = Vector3.Dot(velocity, targetDelta); float dot = Vector3.Dot(velocity, targetDelta);
bool ahead = dot > 0; bool ahead = dot > 0; // true if the new position is ahead of us
// if (!ahead) return; // visualize 'ahead' for easier debugging
// visualize 'ahead'
if (debugColors) if (debugColors)
{ {
rend.material.color = ahead ? blendingAheadColor : blendingBehindColor; rend.material.color = ahead ? blendingAheadColor : blendingBehindColor;
} }
// assign position and rotation together. faster than accessing manually. // do the best possible smoothing depending on ahead / behind
// TODO reuse ApplySnapshot for consistency? // if ahead: interpolate towards remote state more and more
//tf.SetPositionAndRotation(newPosition, newRotation); if (ahead)
//predictedRigidbody.MovePosition(newPosition); // smooth {
//predictedRigidbody.MoveRotation(newRotation); // smooth // TODO reuse ApplySnapshot for consistency?
// tf.SetPositionAndRotation(newPosition, newRotation);
predictedRigidbody.MovePosition(newPosition); // smooth
predictedRigidbody.MoveRotation(newRotation); // smooth
}
// if behind: only slow down. never move backwards, as that would
// cause a highly noticable jitter effect!
else
{
Vector3 newVelocity = Vector3.MoveTowards(velocity, Vector3.zero, positionStep);
predictedRigidbody.velocity = newVelocity;
}
} }
// directly apply server snapshots while following // directly apply server snapshots while following
else if (state == ForecastState.FOLLOWING) else if (state == ForecastState.FOLLOWING)

View File

@ -189,7 +189,7 @@ MonoBehaviour:
showOverlay: 0 showOverlay: 0
overlayColor: {r: 0, g: 0, b: 0, a: 0.5} overlayColor: {r: 0, g: 0, b: 0, a: 0.5}
predictedRigidbody: {fileID: -177125271246800426} predictedRigidbody: {fileID: -177125271246800426}
blendingRttMultiplier: 5 blendingRttMultiplier: 2
blendingCurve: blendingCurve:
serializedVersion: 2 serializedVersion: 2
m_Curve: m_Curve:

View File

@ -326,7 +326,7 @@ MonoBehaviour:
showOverlay: 0 showOverlay: 0
overlayColor: {r: 0, g: 0, b: 0, a: 0.5} overlayColor: {r: 0, g: 0, b: 0, a: 0.5}
predictedRigidbody: {fileID: 1848203816128897140} predictedRigidbody: {fileID: 1848203816128897140}
blendingRttMultiplier: 5 blendingRttMultiplier: 2
blendingCurve: blendingCurve:
serializedVersion: 2 serializedVersion: 2
m_Curve: m_Curve: