PredictedRigidbody: add user callbacks

This commit is contained in:
mischa 2024-01-11 19:45:28 +01:00
parent 4dde5bf7d8
commit 7fae26ff6f

View File

@ -273,6 +273,10 @@ void RecordState()
lastRecorded = state; lastRecorded = state;
} }
// optional user callbacks, in case people need to know about events.
protected virtual void OnSnappedIntoPlace() {}
protected virtual void OnCorrected() {}
void ApplyState(Vector3 position, Quaternion rotation, Vector3 velocity) void ApplyState(Vector3 position, Quaternion rotation, Vector3 velocity)
{ {
// fix rigidbodies seemingly dancing in place instead of coming to rest. // fix rigidbodies seemingly dancing in place instead of coming to rest.
@ -285,6 +289,9 @@ void ApplyState(Vector3 position, Quaternion rotation, Vector3 velocity)
rb.position = position; rb.position = position;
rb.rotation = rotation; rb.rotation = rotation;
rb.velocity = Vector3.zero; rb.velocity = Vector3.zero;
// user callback
OnSnappedIntoPlace();
return; return;
} }
@ -423,6 +430,9 @@ void OnReceivedState(double timestamp, RigidbodyState state)
// Debug.Log($"Correcting {name}: {correctedAmount} / {stateHistory.Count} states to final position from: {rb.position} to: {last.position}"); // Debug.Log($"Correcting {name}: {correctedAmount} / {stateHistory.Count} states to final position from: {rb.position} to: {last.position}");
Debug.DrawLine(rb.position, recomputed.position, Color.green, lineTime); Debug.DrawLine(rb.position, recomputed.position, Color.green, lineTime);
ApplyState(recomputed.position, recomputed.rotation, recomputed.velocity); ApplyState(recomputed.position, recomputed.rotation, recomputed.velocity);
// user callback
OnCorrected();
} }
} }