Compare commits

...

9 Commits

Author SHA1 Message Date
MrGadget
bc43fe9272
Merge 376a733386 into 04bb95311b 2024-11-04 23:54:58 +01:00
MrGadget
376a733386 Revert change to CalculateAngularVelocity
Quaternion doesn't have `normalize`
2024-04-15 12:24:49 -04:00
MrGadget
25b6366cc0 Merged master 2024-04-15 12:19:09 -04:00
MrGadget
c4d138aaa0
Update Assets/Mirror/Components/NetworkTransform/NetworkTransformBase.cs
Co-authored-by: mischa <16416509+miwarnec@users.noreply.github.com>
2024-04-15 11:19:40 -04:00
MrGadget
e93e2eae4d Better CalculateAngularVelocity 2024-04-10 12:54:57 -04:00
MrGadget
80f2d1e641 Use GetPositionAndRotation 2024-04-10 11:03:50 -04:00
MrGadget
f8a4e1150f Ensure target is not null 2024-04-10 08:47:03 -04:00
MrGadget
12ffdae3eb virtuals and overrides 2024-04-10 08:33:19 -04:00
MrGadget
bab41931ee feat(NetworkTransformBase): Add Velocity and AngularVelocity 2024-04-10 08:28:33 -04:00
3 changed files with 64 additions and 2 deletions

View File

@ -156,6 +156,54 @@ protected virtual void Awake()
Configure(); Configure();
} }
// 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 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.
target.GetPositionAndRotation(out lastPosition, out lastRotation);
}
protected virtual void Update()
{
// set target to self if none yet
if (target == null) target = transform;
// Use global coordinates for velocity and angular velocity.
target.GetPositionAndRotation(out Vector3 pos, out Quaternion rot);
// Update velocity and angular velocity
velocity = (pos - lastPosition) / 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);
//Quaternion deltaRotation = (currentRot * Quaternion.Inverse(lastRotation)).normalize;
// 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 ////////////////////////////////////////////////// // snapshot functions //////////////////////////////////////////////////
// get local/world position // get local/world position
protected virtual Vector3 GetPosition() => protected virtual Vector3 GetPosition() =>
@ -414,6 +462,16 @@ public virtual void ResetState()
// so let's clear the buffers. // so let's clear the buffers.
serverSnapshots.Clear(); serverSnapshots.Clear();
clientSnapshots.Clear(); clientSnapshots.Clear();
// set target to self if none yet
if (target == null) target = transform;
// Reset last position and rotation
target.GetPositionAndRotation(out lastPosition, out lastRotation);
// Reset velocity / angular velocity
velocity = Vector3.zero;
angularVelocity = Vector3.zero;
} }
public virtual void Reset() public virtual void Reset()

View File

@ -43,13 +43,15 @@ public class NetworkTransformReliable : NetworkTransformBase
protected TransformSnapshot last; protected TransformSnapshot last;
// update ////////////////////////////////////////////////////////////// // update //////////////////////////////////////////////////////////////
void Update() protected override void Update()
{ {
// if server then always sync to others. // if server then always sync to others.
if (isServer) UpdateServer(); if (isServer) UpdateServer();
// 'else if' because host mode shouldn't send anything to server. // 'else if' because host mode shouldn't send anything to server.
// it is the server. don't overwrite anything there. // it is the server. don't overwrite anything there.
else if (isClient) UpdateClient(); else if (isClient) UpdateClient();
base.Update();
} }
void LateUpdate() void LateUpdate()

View File

@ -27,13 +27,15 @@ public class NetworkTransformUnreliable : NetworkTransformBase
// update ////////////////////////////////////////////////////////////// // update //////////////////////////////////////////////////////////////
// Update applies interpolation // Update applies interpolation
void Update() protected override void Update()
{ {
if (isServer) UpdateServerInterpolation(); if (isServer) UpdateServerInterpolation();
// for all other clients (and for local player if !authority), // for all other clients (and for local player if !authority),
// we need to apply snapshots from the buffer. // we need to apply snapshots from the buffer.
// 'else if' because host mode shouldn't interpolate client // 'else if' because host mode shouldn't interpolate client
else if (isClient && !IsClientWithAuthority) UpdateClientInterpolation(); else if (isClient && !IsClientWithAuthority) UpdateClientInterpolation();
base.Update();
} }
// LateUpdate broadcasts. // LateUpdate broadcasts.