From 9e41697796930ffc256450f7ce36477def717806 Mon Sep 17 00:00:00 2001 From: mischa Date: Thu, 4 Apr 2024 19:36:57 +0800 Subject: [PATCH] copy forecast rigid state --- .../ForecastRigidbody/ForecastRigidbody.cs | 6 +- .../ForecastRigidbodyState.cs | 60 +++++++++++++++++++ .../ForecastRigidbodyState.cs.meta | 3 + 3 files changed, 66 insertions(+), 3 deletions(-) create mode 100644 Assets/Mirror/Components/ForecastRigidbody/ForecastRigidbodyState.cs create mode 100644 Assets/Mirror/Components/ForecastRigidbody/ForecastRigidbodyState.cs.meta diff --git a/Assets/Mirror/Components/ForecastRigidbody/ForecastRigidbody.cs b/Assets/Mirror/Components/ForecastRigidbody/ForecastRigidbody.cs index 4d0cc9a11..1b6487bf1 100644 --- a/Assets/Mirror/Components/ForecastRigidbody/ForecastRigidbody.cs +++ b/Assets/Mirror/Components/ForecastRigidbody/ForecastRigidbody.cs @@ -367,8 +367,8 @@ protected virtual void OnBeginFollow() {} // process a received server state. // compares it against our history and applies corrections if needed. - RigidbodyState lastReceivedState; - void OnReceivedState(double timestamp, RigidbodyState data)//, bool sleeping) + ForecastRigidbodyState lastReceivedState; + void OnReceivedState(double timestamp, ForecastRigidbodyState data)//, bool sleeping) { // store last time lastReceivedState = data; @@ -448,7 +448,7 @@ public override void OnDeserialize(NetworkReader reader, bool initialState) if (oneFrameAhead) timestamp += serverDeltaTime; // process received state - OnReceivedState(timestamp, new RigidbodyState(timestamp, Vector3.zero, position, Quaternion.identity, rotation, Vector3.zero, velocity, Vector3.zero, angularVelocity));//, sleeping); + OnReceivedState(timestamp, new ForecastRigidbodyState(timestamp, Vector3.zero, position, Quaternion.identity, rotation, Vector3.zero, velocity, Vector3.zero, angularVelocity));//, sleeping); } protected override void OnValidate() diff --git a/Assets/Mirror/Components/ForecastRigidbody/ForecastRigidbodyState.cs b/Assets/Mirror/Components/ForecastRigidbody/ForecastRigidbodyState.cs new file mode 100644 index 000000000..c2a09de3f --- /dev/null +++ b/Assets/Mirror/Components/ForecastRigidbody/ForecastRigidbodyState.cs @@ -0,0 +1,60 @@ +// PredictedRigidbody stores a history of its rigidbody states. +using System.Runtime.CompilerServices; +using UnityEngine; + +namespace Mirror +{ + // inline everything because this is performance critical! + public struct ForecastRigidbodyState : PredictedState + { + public double timestamp { [MethodImpl(MethodImplOptions.AggressiveInlining)] get; [MethodImpl(MethodImplOptions.AggressiveInlining)] private set; } + + // we want to store position delta (last + delta = current), and current. + // this way we can apply deltas on top of corrected positions to get the corrected final position. + public Vector3 positionDelta { [MethodImpl(MethodImplOptions.AggressiveInlining)] get; [MethodImpl(MethodImplOptions.AggressiveInlining)] set; } // delta to get from last to this position + public Vector3 position { [MethodImpl(MethodImplOptions.AggressiveInlining)] get; [MethodImpl(MethodImplOptions.AggressiveInlining)] set; } + + public Quaternion rotationDelta { [MethodImpl(MethodImplOptions.AggressiveInlining)] get; [MethodImpl(MethodImplOptions.AggressiveInlining)] set; } // delta to get from last to this rotation + public Quaternion rotation { [MethodImpl(MethodImplOptions.AggressiveInlining)] get; [MethodImpl(MethodImplOptions.AggressiveInlining)] set; } + + public Vector3 velocityDelta { [MethodImpl(MethodImplOptions.AggressiveInlining)] get; [MethodImpl(MethodImplOptions.AggressiveInlining)] set; } // delta to get from last to this velocity + public Vector3 velocity { [MethodImpl(MethodImplOptions.AggressiveInlining)] get; [MethodImpl(MethodImplOptions.AggressiveInlining)] set; } + + public Vector3 angularVelocityDelta { [MethodImpl(MethodImplOptions.AggressiveInlining)] get; [MethodImpl(MethodImplOptions.AggressiveInlining)] set; } // delta to get from last to this velocity + public Vector3 angularVelocity { [MethodImpl(MethodImplOptions.AggressiveInlining)] get; [MethodImpl(MethodImplOptions.AggressiveInlining)] set; } + + public ForecastRigidbodyState( + double timestamp, + Vector3 positionDelta, + Vector3 position, + Quaternion rotationDelta, + Quaternion rotation, + Vector3 velocityDelta, + Vector3 velocity, + Vector3 angularVelocityDelta, + Vector3 angularVelocity) + { + this.timestamp = timestamp; + this.positionDelta = positionDelta; + this.position = position; + this.rotationDelta = rotationDelta; + this.rotation = rotation; + this.velocityDelta = velocityDelta; + this.velocity = velocity; + this.angularVelocityDelta = angularVelocityDelta; + this.angularVelocity = angularVelocity; + } + + public static ForecastRigidbodyState Interpolate(ForecastRigidbodyState a, ForecastRigidbodyState b, float t) + { + return new ForecastRigidbodyState + { + position = Vector3.Lerp(a.position, b.position, t), + // Quaternions always need to be normalized in order to be a valid rotation after operations + rotation = Quaternion.Slerp(a.rotation, b.rotation, t).normalized, + velocity = Vector3.Lerp(a.velocity, b.velocity, t), + angularVelocity = Vector3.Lerp(a.angularVelocity, b.angularVelocity, t) + }; + } + } +} diff --git a/Assets/Mirror/Components/ForecastRigidbody/ForecastRigidbodyState.cs.meta b/Assets/Mirror/Components/ForecastRigidbody/ForecastRigidbodyState.cs.meta new file mode 100644 index 000000000..0a93cd470 --- /dev/null +++ b/Assets/Mirror/Components/ForecastRigidbody/ForecastRigidbodyState.cs.meta @@ -0,0 +1,3 @@ +fileFormatVersion: 2 +guid: 1187705e5eed4e9ab94c1788b8d33e08 +timeCreated: 1712230572 \ No newline at end of file