From bab41931eed8f9a4a9560ceb75bd4240a312e3bf Mon Sep 17 00:00:00 2001 From: MrGadget <9826063+MrGadget1024@users.noreply.github.com> Date: Wed, 10 Apr 2024 08:28:33 -0400 Subject: [PATCH 1/7] feat(NetworkTransformBase): Add Velocity and AngularVelocity --- .../NetworkTransform/NetworkTransformBase.cs | 38 +++++++++++++++++++ 1 file changed, 38 insertions(+) diff --git a/Assets/Mirror/Components/NetworkTransform/NetworkTransformBase.cs b/Assets/Mirror/Components/NetworkTransform/NetworkTransformBase.cs index 52a622394..1e0da20ea 100644 --- a/Assets/Mirror/Components/NetworkTransform/NetworkTransformBase.cs +++ b/Assets/Mirror/Components/NetworkTransform/NetworkTransformBase.cs @@ -121,6 +121,36 @@ protected override void OnValidate() if (coordinateSpace == CoordinateSpace.World) syncScale = false; } + // These are in global coordinates for calculating velocity and angular velocity. + Vector3 lastPosition; + Quaternion lastRotation; + + public Vector3 velocity { get; internal set; } = Vector3.zero; + public Vector3 angularVelocity { get; internal set; } = Vector3.zero; + + protected virtual void Start() + { + // Set last position and rotation to current values + // so we can calculate velocity and angular velocity. + lastPosition = target.position; + lastRotation = target.rotation; + } + + void Update() + { + // Use global coordinates for velocity and angular velocity. + Vector3 pos = target.position; + Quaternion rot = target.rotation; + + // Update velocity and angular velocity + velocity = (pos - lastPosition) / Time.deltaTime; + angularVelocity = (rot.eulerAngles - lastRotation.eulerAngles) / Time.deltaTime; + + // Update last position and rotation + lastPosition = pos; + lastRotation = rot; + } + // snapshot functions ////////////////////////////////////////////////// // get local/world position protected virtual Vector3 GetPosition() => @@ -371,6 +401,14 @@ public virtual void ResetState() // so let's clear the buffers. serverSnapshots.Clear(); clientSnapshots.Clear(); + + // Reset last position and rotation + lastPosition = target.position; + lastRotation = target.rotation; + + // Reset velocity / angular velocity + velocity = Vector3.zero; + angularVelocity = Vector3.zero; } public virtual void Reset() From 12ffdae3eb7cc2f4949e0b543af6fe8a9a1660da Mon Sep 17 00:00:00 2001 From: MrGadget <9826063+MrGadget1024@users.noreply.github.com> Date: Wed, 10 Apr 2024 08:33:19 -0400 Subject: [PATCH 2/7] virtuals and overrides --- .../Components/NetworkTransform/NetworkTransformBase.cs | 2 +- .../Components/NetworkTransform/NetworkTransformReliable.cs | 4 +++- .../Components/NetworkTransform/NetworkTransformUnreliable.cs | 4 +++- 3 files changed, 7 insertions(+), 3 deletions(-) diff --git a/Assets/Mirror/Components/NetworkTransform/NetworkTransformBase.cs b/Assets/Mirror/Components/NetworkTransform/NetworkTransformBase.cs index 1e0da20ea..2acb8798a 100644 --- a/Assets/Mirror/Components/NetworkTransform/NetworkTransformBase.cs +++ b/Assets/Mirror/Components/NetworkTransform/NetworkTransformBase.cs @@ -136,7 +136,7 @@ protected virtual void Start() lastRotation = target.rotation; } - void Update() + protected virtual void Update() { // Use global coordinates for velocity and angular velocity. Vector3 pos = target.position; diff --git a/Assets/Mirror/Components/NetworkTransform/NetworkTransformReliable.cs b/Assets/Mirror/Components/NetworkTransform/NetworkTransformReliable.cs index f6e851bf0..29adcff85 100644 --- a/Assets/Mirror/Components/NetworkTransform/NetworkTransformReliable.cs +++ b/Assets/Mirror/Components/NetworkTransform/NetworkTransformReliable.cs @@ -44,13 +44,15 @@ public class NetworkTransformReliable : NetworkTransformBase protected int lastClientCount = 1; // update ////////////////////////////////////////////////////////////// - void Update() + protected override void Update() { // if server then always sync to others. if (isServer) UpdateServer(); // 'else if' because host mode shouldn't send anything to server. // it is the server. don't overwrite anything there. else if (isClient) UpdateClient(); + + base.Update(); } void LateUpdate() diff --git a/Assets/Mirror/Components/NetworkTransform/NetworkTransformUnreliable.cs b/Assets/Mirror/Components/NetworkTransform/NetworkTransformUnreliable.cs index ec5d3e14a..1d6c3a78a 100644 --- a/Assets/Mirror/Components/NetworkTransform/NetworkTransformUnreliable.cs +++ b/Assets/Mirror/Components/NetworkTransform/NetworkTransformUnreliable.cs @@ -33,13 +33,15 @@ public class NetworkTransformUnreliable : NetworkTransformBase // update ////////////////////////////////////////////////////////////// // Update applies interpolation - void Update() + protected override void Update() { if (isServer) UpdateServerInterpolation(); // for all other clients (and for local player if !authority), // we need to apply snapshots from the buffer. // 'else if' because host mode shouldn't interpolate client else if (isClient && !IsClientWithAuthority) UpdateClientInterpolation(); + + base.Update(); } // LateUpdate broadcasts. From f8a4e1150f6b12dc08a2fbde4a7c1e76ad192d18 Mon Sep 17 00:00:00 2001 From: MrGadget <9826063+MrGadget1024@users.noreply.github.com> Date: Wed, 10 Apr 2024 08:47:03 -0400 Subject: [PATCH 3/7] Ensure target is not null --- .../Components/NetworkTransform/NetworkTransformBase.cs | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/Assets/Mirror/Components/NetworkTransform/NetworkTransformBase.cs b/Assets/Mirror/Components/NetworkTransform/NetworkTransformBase.cs index 2acb8798a..3c386fee5 100644 --- a/Assets/Mirror/Components/NetworkTransform/NetworkTransformBase.cs +++ b/Assets/Mirror/Components/NetworkTransform/NetworkTransformBase.cs @@ -130,6 +130,9 @@ protected override void OnValidate() protected virtual void Start() { + // set target to self if none yet + if (target == null) target = transform; + // Set last position and rotation to current values // so we can calculate velocity and angular velocity. lastPosition = target.position; @@ -138,6 +141,9 @@ protected virtual void Start() protected virtual void Update() { + // set target to self if none yet + if (target == null) target = transform; + // Use global coordinates for velocity and angular velocity. Vector3 pos = target.position; Quaternion rot = target.rotation; @@ -402,6 +408,9 @@ public virtual void ResetState() serverSnapshots.Clear(); clientSnapshots.Clear(); + // set target to self if none yet + if (target == null) target = transform; + // Reset last position and rotation lastPosition = target.position; lastRotation = target.rotation; From 80f2d1e6413bce6b30b9539a09600437153cedfa Mon Sep 17 00:00:00 2001 From: MrGadget <9826063+MrGadget1024@users.noreply.github.com> Date: Wed, 10 Apr 2024 11:03:50 -0400 Subject: [PATCH 4/7] Use GetPositionAndRotation --- .../Components/NetworkTransform/NetworkTransformBase.cs | 9 +++------ 1 file changed, 3 insertions(+), 6 deletions(-) diff --git a/Assets/Mirror/Components/NetworkTransform/NetworkTransformBase.cs b/Assets/Mirror/Components/NetworkTransform/NetworkTransformBase.cs index 3c386fee5..d389620cd 100644 --- a/Assets/Mirror/Components/NetworkTransform/NetworkTransformBase.cs +++ b/Assets/Mirror/Components/NetworkTransform/NetworkTransformBase.cs @@ -135,8 +135,7 @@ protected virtual void Start() // Set last position and rotation to current values // so we can calculate velocity and angular velocity. - lastPosition = target.position; - lastRotation = target.rotation; + target.GetPositionAndRotation(out lastPosition, out lastRotation); } protected virtual void Update() @@ -145,8 +144,7 @@ protected virtual void Update() if (target == null) target = transform; // Use global coordinates for velocity and angular velocity. - Vector3 pos = target.position; - Quaternion rot = target.rotation; + target.GetPositionAndRotation(out Vector3 pos, out Quaternion rot); // Update velocity and angular velocity velocity = (pos - lastPosition) / Time.deltaTime; @@ -412,8 +410,7 @@ public virtual void ResetState() if (target == null) target = transform; // Reset last position and rotation - lastPosition = target.position; - lastRotation = target.rotation; + target.GetPositionAndRotation(out lastPosition, out lastRotation); // Reset velocity / angular velocity velocity = Vector3.zero; From e93e2eae4da1a7d2ecee7ee0da555d62592e7ce5 Mon Sep 17 00:00:00 2001 From: MrGadget <9826063+MrGadget1024@users.noreply.github.com> Date: Wed, 10 Apr 2024 12:54:57 -0400 Subject: [PATCH 5/7] Better CalculateAngularVelocity --- .../NetworkTransform/NetworkTransformBase.cs | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/Assets/Mirror/Components/NetworkTransform/NetworkTransformBase.cs b/Assets/Mirror/Components/NetworkTransform/NetworkTransformBase.cs index d389620cd..b3e8a6681 100644 --- a/Assets/Mirror/Components/NetworkTransform/NetworkTransformBase.cs +++ b/Assets/Mirror/Components/NetworkTransform/NetworkTransformBase.cs @@ -148,13 +148,26 @@ protected virtual void Update() // Update velocity and angular velocity velocity = (pos - lastPosition) / Time.deltaTime; - angularVelocity = (rot.eulerAngles - lastRotation.eulerAngles) / Time.deltaTime; + CalculateAngularVelocity(rot); // Update last position and rotation lastPosition = pos; lastRotation = rot; } + void CalculateAngularVelocity(Quaternion currentRot) + { + // calculate angle between two rotations + Quaternion deltaRotation = currentRot * Quaternion.Inverse(lastRotation); + + // convert to angle axis + deltaRotation.ToAngleAxis(out float angle, out Vector3 axis); + + // we assume the angle is always the shortest path + // we don't need to check for 360 degree rotations + angularVelocity = axis * angle * Mathf.Deg2Rad / Time.deltaTime; + } + // snapshot functions ////////////////////////////////////////////////// // get local/world position protected virtual Vector3 GetPosition() => From c4d138aaa0774b00b8d6909ae19a5580bbc85d2f Mon Sep 17 00:00:00 2001 From: MrGadget <9826063+MrGadget1024@users.noreply.github.com> Date: Mon, 15 Apr 2024 11:19:40 -0400 Subject: [PATCH 6/7] Update Assets/Mirror/Components/NetworkTransform/NetworkTransformBase.cs Co-authored-by: mischa <16416509+miwarnec@users.noreply.github.com> --- .../Mirror/Components/NetworkTransform/NetworkTransformBase.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Assets/Mirror/Components/NetworkTransform/NetworkTransformBase.cs b/Assets/Mirror/Components/NetworkTransform/NetworkTransformBase.cs index b3e8a6681..376e94265 100644 --- a/Assets/Mirror/Components/NetworkTransform/NetworkTransformBase.cs +++ b/Assets/Mirror/Components/NetworkTransform/NetworkTransformBase.cs @@ -158,7 +158,7 @@ protected virtual void Update() void CalculateAngularVelocity(Quaternion currentRot) { // calculate angle between two rotations - Quaternion deltaRotation = currentRot * Quaternion.Inverse(lastRotation); + Quaternion deltaRotation = (currentRot * Quaternion.Inverse(lastRotation)).normalize; // convert to angle axis deltaRotation.ToAngleAxis(out float angle, out Vector3 axis); From 376a7333863109126d8a9f9f8e684bff1b76899f Mon Sep 17 00:00:00 2001 From: MrGadget <9826063+MrGadget1024@users.noreply.github.com> Date: Mon, 15 Apr 2024 12:24:49 -0400 Subject: [PATCH 7/7] Revert change to CalculateAngularVelocity Quaternion doesn't have `normalize` --- .../Mirror/Components/NetworkTransform/NetworkTransformBase.cs | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/Assets/Mirror/Components/NetworkTransform/NetworkTransformBase.cs b/Assets/Mirror/Components/NetworkTransform/NetworkTransformBase.cs index 54681229f..f58b69c08 100644 --- a/Assets/Mirror/Components/NetworkTransform/NetworkTransformBase.cs +++ b/Assets/Mirror/Components/NetworkTransform/NetworkTransformBase.cs @@ -181,7 +181,8 @@ protected virtual void Update() void CalculateAngularVelocity(Quaternion currentRot) { // calculate angle between two rotations - Quaternion deltaRotation = (currentRot * Quaternion.Inverse(lastRotation)).normalize; + Quaternion deltaRotation = currentRot * Quaternion.Inverse(lastRotation); + //Quaternion deltaRotation = (currentRot * Quaternion.Inverse(lastRotation)).normalize; // convert to angle axis deltaRotation.ToAngleAxis(out float angle, out Vector3 axis);