copy forecast rigid state

This commit is contained in:
mischa 2024-04-04 19:36:57 +08:00
parent 5322b59846
commit 9e41697796
3 changed files with 66 additions and 3 deletions

View File

@ -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()

View File

@ -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)
};
}
}
}

View File

@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: 1187705e5eed4e9ab94c1788b8d33e08
timeCreated: 1712230572