ahead-detection & visualization

This commit is contained in:
mischa 2024-04-25 00:52:02 +08:00
parent 285a645b7e
commit 83ffaffb45
3 changed files with 91 additions and 35 deletions

View File

@ -68,7 +68,8 @@ enum ForecastState
public bool debugColors = false;
Color originalColor = Color.white;
public Color predictingColor = Color.green;
public Color blendingColor = Color.yellow; // when actually interpolating towards a blend in front of us
public Color blendingAheadColor = Color.yellow; // when actually interpolating towards a blend in front of us
public Color blendingBehindColor = Color.red; // when actually interpolating towards a blend in front of us
protected override void Awake()
{
@ -212,8 +213,11 @@ protected override void Update()
}
Vector3 lastSetPosition = Vector3.zero;
Vector3 interpolatedPosition = Vector3.zero;
protected override void ApplySnapshot(NTSnapshot interpolated)
{
interpolatedPosition = interpolated.position;
// ignore server snapshots while simulating physics
if (state == ForecastState.PREDICTING)
{
@ -229,10 +233,10 @@ protected override void ApplySnapshot(NTSnapshot interpolated)
// blend between local and remote position
// set debug color
if (debugColors)
{
rend.material.color = blendingColor;
}
// if (debugColors)
// {
// rend.material.color = blendingColor;
// }
// sample the blending curve to find out how much to blend right now
float blendingElapsed = (float)(NetworkTime.localTime - blendingStartTime);
@ -268,9 +272,21 @@ protected override void ApplySnapshot(NTSnapshot interpolated)
// Quaternions always need to be normalized in order to be a valid rotation after operations
Quaternion newRotation = Quaternion.Slerp(currentRotation, targetRotation, p).normalized;
// would the new position be ahead of us, or behind us?
Vector3 velocity = predictedRigidbody.velocity; // this is where we are going
Vector3 targetDelta = targetPosition - currentPosition; // this is where we would go
float dot = Vector3.Dot(velocity, targetDelta);
bool ahead = dot > 0;
// if (!ahead) return;
// visualize 'ahead'
if (debugColors)
{
rend.material.color = ahead ? blendingAheadColor : blendingBehindColor;
}
// assign position and rotation together. faster than accessing manually.
// TODO reuse ApplySnapshot for consistency?
tf.SetPositionAndRotation(newPosition, newRotation);
//tf.SetPositionAndRotation(newPosition, newRotation);
//predictedRigidbody.MovePosition(newPosition); // smooth
//predictedRigidbody.MoveRotation(newRotation); // smooth
}
@ -284,10 +300,10 @@ protected override void ApplySnapshot(NTSnapshot interpolated)
if (Vector3.Distance(interpolated.position, lastSetPosition) >= epsilon)
{
base.ApplySnapshot(interpolated);
lastSetPosition = interpolated.position;
}
// END CUSTOM CHANGE
}
lastSetPosition = interpolated.position;
}
// Prediction uses a Rigidbody, which needs to be moved in FixedUpdate() even while kinematic.
@ -372,6 +388,12 @@ void OnCollisionEnter(Collision collision)
}
*/
private void OnDrawGizmos()
{
Bounds bounds = GetComponent<Collider>().bounds;
Gizmos.DrawWireCube(interpolatedPosition,bounds.size );
}
// optional user callbacks, in case people need to know about events.
protected virtual void OnBeginPrediction() {}
protected virtual void OnBeginBlending() {}

View File

@ -164,6 +164,30 @@ MonoBehaviour:
syncDirection: 0
syncMode: 0
syncInterval: 0
target: {fileID: 3429911415116987812}
clientAuthority: 0
sendRate: 30
bufferTimeMultiplier: 2
bufferSizeLimit: 64
catchupNegativeThreshold: -1
catchupPositiveThreshold: 1
catchupSpeed: 0.009999999776482582
slowdownSpeed: 0.009999999776482582
driftEmaDuration: 1
dynamicAdjustment: 1
dynamicAdjustmentTolerance: 1
deliveryTimeEmaDuration: 2
onlySyncOnChange: 1
bufferResetMultiplier: 5
positionSensitivity: 0.01
rotationSensitivity: 0.01
scaleSensitivity: 0.01
syncPosition: 1
syncRotation: 1
syncScale: 0
showGizmos: 0
showOverlay: 0
overlayColor: {r: 0, g: 0, b: 0, a: 0.5}
predictedRigidbody: {fileID: -177125271246800426}
blendingRttMultiplier: 2
blendingCurve:
@ -199,19 +223,12 @@ MonoBehaviour:
m_PreInfinity: 2
m_PostInfinity: 2
m_RotationOrder: 4
localForceDampening: 0.2
motionSmoothingVelocityThreshold: 0.1
motionSmoothingAngularVelocityThreshold: 5
motionSmoothingTimeTolerance: 0.5
stateHistoryLimit: 32
recordInterval: 0.05
onlyRecordChanges: 1
compareLastFirst: 1
positionCorrectionThreshold: 0.1
rotationCorrectionThreshold: 5
oneFrameAhead: 1
snapThreshold: 2
reduceSendsWhileIdle: 1
localForceDampening: 0.5
collisionChaining: 1
maxCollisionChainingDepth: 2
velocitySensitivity: 0.1
angularVelocitySensitivity: 5
debugColors: 1
predictingColor: {r: 0, g: 1, b: 0, a: 1}
blendingColor: {r: 1, g: 0.92156863, b: 0.015686275, a: 1}
blendingAheadColor: {r: 1, g: 0.92156863, b: 0.015686275, a: 1}
blendingBehindColor: {r: 1, g: 0, b: 0, a: 1}

View File

@ -301,6 +301,30 @@ MonoBehaviour:
syncDirection: 0
syncMode: 0
syncInterval: 0
target: {fileID: 3429911415116987812}
clientAuthority: 0
sendRate: 30
bufferTimeMultiplier: 2
bufferSizeLimit: 64
catchupNegativeThreshold: -1
catchupPositiveThreshold: 1
catchupSpeed: 0.009999999776482582
slowdownSpeed: 0.009999999776482582
driftEmaDuration: 1
dynamicAdjustment: 1
dynamicAdjustmentTolerance: 1
deliveryTimeEmaDuration: 2
onlySyncOnChange: 1
bufferResetMultiplier: 5
positionSensitivity: 0.01
rotationSensitivity: 0.01
scaleSensitivity: 0.01
syncPosition: 1
syncRotation: 1
syncScale: 0
showGizmos: 0
showOverlay: 0
overlayColor: {r: 0, g: 0, b: 0, a: 0.5}
predictedRigidbody: {fileID: 1848203816128897140}
blendingRttMultiplier: 2
blendingCurve:
@ -336,19 +360,12 @@ MonoBehaviour:
m_PreInfinity: 2
m_PostInfinity: 2
m_RotationOrder: 4
localForceDampening: 0.2
motionSmoothingVelocityThreshold: 0.1
motionSmoothingAngularVelocityThreshold: 5
motionSmoothingTimeTolerance: 0.5
stateHistoryLimit: 32
recordInterval: 0.05
onlyRecordChanges: 1
compareLastFirst: 1
positionCorrectionThreshold: 0.1
rotationCorrectionThreshold: 5
oneFrameAhead: 1
snapThreshold: 2
reduceSendsWhileIdle: 1
localForceDampening: 0.5
collisionChaining: 1
maxCollisionChainingDepth: 2
velocitySensitivity: 0.1
angularVelocitySensitivity: 5
debugColors: 1
predictingColor: {r: 0, g: 1, b: 0, a: 1}
blendingColor: {r: 1, g: 0.92156863, b: 0.015686275, a: 1}
blendingAheadColor: {r: 1, g: 0.92156863, b: 0.015686275, a: 1}
blendingBehindColor: {r: 1, g: 0, b: 0, a: 1}