From 22cdece64a56d39a6a6305bb3372bb3d128bd72f Mon Sep 17 00:00:00 2001 From: mischa Date: Wed, 27 Mar 2024 22:42:30 +0800 Subject: [PATCH] perf(PredictedRigidbody): OnReceivedState compares sqr magnitude --- .../Components/PredictedRigidbody/PredictedRigidbody.cs | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/Assets/Mirror/Components/PredictedRigidbody/PredictedRigidbody.cs b/Assets/Mirror/Components/PredictedRigidbody/PredictedRigidbody.cs index f1fc1fd46..889be42b2 100644 --- a/Assets/Mirror/Components/PredictedRigidbody/PredictedRigidbody.cs +++ b/Assets/Mirror/Components/PredictedRigidbody/PredictedRigidbody.cs @@ -701,8 +701,10 @@ void OnReceivedState(double timestamp, RigidbodyState state)//, bool sleeping) // this is as fast as it gets for skipping idle objects. // // if this ever causes issues, feel free to disable it. + float positionToStateDistanceSqr = Vector3.SqrMagnitude(state.position - physicsPosition); if (compareLastFirst && - Vector3.Distance(state.position, physicsPosition) < positionCorrectionThreshold && + // Vector3.Distance(state.position, physicsPosition) < positionCorrectionThreshold && // slow comparison + positionToStateDistanceSqr < positionCorrectionThresholdSqr && // fast comparison Quaternion.Angle(state.rotation, physicsRotation) < rotationCorrectionThreshold) { // Debug.Log($"OnReceivedState for {name}: taking optimized early return!"); @@ -754,7 +756,8 @@ void OnReceivedState(double timestamp, RigidbodyState state)//, bool sleeping) // we clamp it to 'now'. // but only correct if off by threshold. // TODO maybe we should interpolate this back to 'now'? - if (Vector3.Distance(state.position, physicsPosition) >= positionCorrectionThreshold) + // if (Vector3.Distance(state.position, physicsPosition) >= positionCorrectionThreshold) // slow comparison + if (positionToStateDistanceSqr >= positionCorrectionThresholdSqr) // fast comparison { // this can happen a lot when latency is ~0. logging all the time allocates too much and is too slow. // double ahead = state.timestamp - newest.timestamp;