diff --git a/QSB/JellyfishSync/TransformSync/JellyfishTransformSync.cs b/QSB/JellyfishSync/TransformSync/JellyfishTransformSync.cs index 1d7e52e5..fc009a6c 100644 --- a/QSB/JellyfishSync/TransformSync/JellyfishTransformSync.cs +++ b/QSB/JellyfishSync/TransformSync/JellyfishTransformSync.cs @@ -95,8 +95,8 @@ namespace QSB.JellyfishSync.TransformSync var pos = ReferenceTransform.FromRelPos(transform.position); AttachedRigidbody.SetPosition(pos); AttachedRigidbody.SetRotation(ReferenceTransform.FromRelRot(transform.rotation)); - AttachedRigidbody.SetVelocity(ReferenceRigidbody.FromRelVel(_relativeVelocity, pos)); - AttachedRigidbody.SetAngularVelocity(ReferenceRigidbody.FromRelAngVel(_relativeAngularVelocity)); + AttachedRigidbody.SetVelocity(ReferenceRigidbody.FromRelVel(Velocity, pos)); + AttachedRigidbody.SetAngularVelocity(ReferenceRigidbody.FromRelAngVel(AngularVelocity)); _qsbJellyfish.SetIsRising(_isRising); } diff --git a/QSB/ShipSync/TransformSync/ShipTransformSync.cs b/QSB/ShipSync/TransformSync/ShipTransformSync.cs index 8f0676da..d4dd1bc1 100644 --- a/QSB/ShipSync/TransformSync/ShipTransformSync.cs +++ b/QSB/ShipSync/TransformSync/ShipTransformSync.cs @@ -48,8 +48,8 @@ namespace QSB.ShipSync.TransformSync AttachedRigidbody.SetRotation(targetRot); } - var targetVelocity = ReferenceRigidbody.FromRelVel(_relativeVelocity, targetPos); - var targetAngularVelocity = ReferenceRigidbody.FromRelAngVel(_relativeAngularVelocity); + var targetVelocity = ReferenceRigidbody.FromRelVel(Velocity, targetPos); + var targetAngularVelocity = ReferenceRigidbody.FromRelAngVel(AngularVelocity); SetVelocity(AttachedRigidbody, targetVelocity); AttachedRigidbody.SetAngularVelocity(targetAngularVelocity); diff --git a/QSB/Syncs/Occasional/OccasionalTransformSync.cs b/QSB/Syncs/Occasional/OccasionalTransformSync.cs index cb355295..c90ba326 100644 --- a/QSB/Syncs/Occasional/OccasionalTransformSync.cs +++ b/QSB/Syncs/Occasional/OccasionalTransformSync.cs @@ -72,8 +72,8 @@ namespace QSB.Syncs.Occasional var pos = ReferenceTransform.FromRelPos(transform.position); AttachedRigidbody.SetPosition(pos); AttachedRigidbody.SetRotation(ReferenceTransform.FromRelRot(transform.rotation)); - AttachedRigidbody.SetVelocity(ReferenceRigidbody.FromRelVel(_relativeVelocity, pos)); - AttachedRigidbody.SetAngularVelocity(ReferenceRigidbody.FromRelAngVel(_relativeAngularVelocity)); + AttachedRigidbody.SetVelocity(ReferenceRigidbody.FromRelVel(Velocity, pos)); + AttachedRigidbody.SetAngularVelocity(ReferenceRigidbody.FromRelAngVel(AngularVelocity)); Move(); } diff --git a/QSB/Syncs/QSBNetworkTransform.cs b/QSB/Syncs/QSBNetworkTransform.cs index 240a423e..65165d5f 100644 --- a/QSB/Syncs/QSBNetworkTransform.cs +++ b/QSB/Syncs/QSBNetworkTransform.cs @@ -8,8 +8,11 @@ namespace QSB.Syncs { protected override float SendInterval => 0.05f; - protected Vector3 _prevPosition; - protected Quaternion _prevRotation; + private const float PositionChangeThreshold = 0.05f; + private const float RotationChangeThreshold = 0.05f; + + private Vector3 _prevPosition; + private Quaternion _prevRotation; protected override void UpdatePrevData() { @@ -18,8 +21,8 @@ namespace QSB.Syncs } protected override bool HasChanged() => - Vector3.Distance(transform.position, _prevPosition) > 1E-05f || - Quaternion.Angle(transform.rotation, _prevRotation) > 1E-05f; + Vector3.Distance(transform.position, _prevPosition) > PositionChangeThreshold || + Quaternion.Angle(transform.rotation, _prevRotation) > RotationChangeThreshold; protected override void Serialize(NetworkWriter writer) { diff --git a/QSB/Syncs/QSBNetworkTransformChild.cs b/QSB/Syncs/QSBNetworkTransformChild.cs index e137ad1d..bae71dae 100644 --- a/QSB/Syncs/QSBNetworkTransformChild.cs +++ b/QSB/Syncs/QSBNetworkTransformChild.cs @@ -10,8 +10,11 @@ namespace QSB.Syncs protected override float SendInterval => 0.05f; - protected Vector3 _prevPosition; - protected Quaternion _prevRotation; + private const float PositionChangeThreshold = 0.05f; + private const float RotationChangeThreshold = 0.05f; + + private Vector3 _prevPosition; + private Quaternion _prevRotation; protected override void UpdatePrevData() { @@ -20,8 +23,8 @@ namespace QSB.Syncs } protected override bool HasChanged() => - Vector3.Distance(Target.localPosition, _prevPosition) > 1E-05f || - Quaternion.Angle(Target.localRotation, _prevRotation) > 1E-05f; + Vector3.Distance(Target.localPosition, _prevPosition) > PositionChangeThreshold || + Quaternion.Angle(Target.localRotation, _prevRotation) > RotationChangeThreshold; protected override void Serialize(NetworkWriter writer) { diff --git a/QSB/Syncs/Sectored/Rigidbodies/SectoredRigidbodySync.cs b/QSB/Syncs/Sectored/Rigidbodies/SectoredRigidbodySync.cs index 1e1cb42a..c07fd72d 100644 --- a/QSB/Syncs/Sectored/Rigidbodies/SectoredRigidbodySync.cs +++ b/QSB/Syncs/Sectored/Rigidbodies/SectoredRigidbodySync.cs @@ -6,13 +6,11 @@ namespace QSB.Syncs.Sectored.Rigidbodies { public abstract class SectoredRigidbodySync : BaseSectoredSync { - private const float PositionMovedThreshold = 0.05f; - private const float AngleRotatedThreshold = 0.05f; private const float VelocityChangeThreshold = 0.05f; - private const float AngVelocityChangeThreshold = 0.05f; + private const float AngularVelocityChangeThreshold = 0.05f; - protected Vector3 _relativeVelocity; - protected Vector3 _relativeAngularVelocity; + protected Vector3 Velocity; + protected Vector3 AngularVelocity; private Vector3 _prevVelocity; private Vector3 _prevAngularVelocity; @@ -41,22 +39,22 @@ namespace QSB.Syncs.Sectored.Rigidbodies protected override void UpdatePrevData() { base.UpdatePrevData(); - _prevVelocity = _relativeVelocity; - _prevAngularVelocity = _relativeAngularVelocity; + _prevVelocity = Velocity; + _prevAngularVelocity = AngularVelocity; } protected override void Serialize(NetworkWriter writer) { base.Serialize(writer); - writer.Write(_relativeVelocity); - writer.Write(_relativeAngularVelocity); + writer.Write(Velocity); + writer.Write(AngularVelocity); } protected override void Deserialize(NetworkReader reader) { base.Deserialize(reader); - _relativeVelocity = reader.ReadVector3(); - _relativeAngularVelocity = reader.ReadVector3(); + Velocity = reader.ReadVector3(); + AngularVelocity = reader.ReadVector3(); } protected override void GetFromAttached() @@ -69,8 +67,8 @@ namespace QSB.Syncs.Sectored.Rigidbodies transform.position = ReferenceTransform.ToRelPos(AttachedRigidbody.GetPosition()); transform.rotation = ReferenceTransform.ToRelRot(AttachedRigidbody.GetRotation()); - _relativeVelocity = ReferenceRigidbody.ToRelVel(AttachedRigidbody.GetVelocity(), AttachedRigidbody.GetPosition()); - _relativeAngularVelocity = ReferenceRigidbody.ToRelAngVel(AttachedRigidbody.GetAngularVelocity()); + Velocity = ReferenceRigidbody.ToRelVel(AttachedRigidbody.GetVelocity(), AttachedRigidbody.GetPosition()); + AngularVelocity = ReferenceRigidbody.ToRelAngVel(AttachedRigidbody.GetAngularVelocity()); } protected override void ApplyToAttached() @@ -96,36 +94,16 @@ namespace QSB.Syncs.Sectored.Rigidbodies AttachedRigidbody.MoveToPosition(positionToSet); AttachedRigidbody.MoveToRotation(rotationToSet); - var targetVelocity = ReferenceRigidbody.FromRelVel(_relativeVelocity, targetPos); - var targetAngularVelocity = ReferenceRigidbody.FromRelAngVel(_relativeAngularVelocity); + var targetVelocity = ReferenceRigidbody.FromRelVel(Velocity, targetPos); + var targetAngularVelocity = ReferenceRigidbody.FromRelAngVel(AngularVelocity); AttachedRigidbody.SetVelocity(targetVelocity); AttachedRigidbody.SetAngularVelocity(targetAngularVelocity); } - protected override bool HasChanged() - { - if (Vector3.Distance(transform.position, _prevPosition) > PositionMovedThreshold) - { - return true; - } - - if (Quaternion.Angle(transform.rotation, _prevRotation) > AngleRotatedThreshold) - { - return true; - } - - if (Vector3.Distance(_relativeVelocity, _prevVelocity) > VelocityChangeThreshold) - { - return true; - } - - if (Vector3.Distance(_relativeAngularVelocity, _prevAngularVelocity) > AngVelocityChangeThreshold) - { - return true; - } - - return false; - } + protected override bool HasChanged() => + base.HasChanged() || + Vector3.Distance(Velocity, _prevVelocity) > VelocityChangeThreshold || + Vector3.Distance(AngularVelocity, _prevAngularVelocity) > AngularVelocityChangeThreshold; } } diff --git a/QSB/Syncs/Unsectored/Rigidbodies/UnsectoredRigidbodySync.cs b/QSB/Syncs/Unsectored/Rigidbodies/UnsectoredRigidbodySync.cs index 563f4057..266822e2 100644 --- a/QSB/Syncs/Unsectored/Rigidbodies/UnsectoredRigidbodySync.cs +++ b/QSB/Syncs/Unsectored/Rigidbodies/UnsectoredRigidbodySync.cs @@ -6,17 +6,15 @@ namespace QSB.Syncs.Unsectored.Rigidbodies { public abstract class UnsectoredRigidbodySync : BaseUnsectoredSync { - private const float PositionMovedThreshold = 0.05f; - private const float AngleRotatedThreshold = 0.05f; private const float VelocityChangeThreshold = 0.05f; - private const float AngVelocityChangeThreshold = 0.05f; + private const float AngularVelocityChangeThreshold = 0.05f; - protected Vector3 _relativeVelocity; - protected Vector3 _relativeAngularVelocity; + protected Vector3 Velocity; + protected Vector3 AngularVelocity; private Vector3 _prevVelocity; private Vector3 _prevAngularVelocity; - protected OWRigidbody AttachedRigidbody { get; private set; } + public OWRigidbody AttachedRigidbody { get; private set; } public OWRigidbody ReferenceRigidbody { get; private set; } protected abstract OWRigidbody InitAttachedRigidbody(); @@ -41,30 +39,30 @@ namespace QSB.Syncs.Unsectored.Rigidbodies protected override void UpdatePrevData() { base.UpdatePrevData(); - _prevVelocity = _relativeVelocity; - _prevAngularVelocity = _relativeAngularVelocity; + _prevVelocity = Velocity; + _prevAngularVelocity = AngularVelocity; } protected override void Serialize(NetworkWriter writer) { base.Serialize(writer); - writer.Write(_relativeVelocity); - writer.Write(_relativeAngularVelocity); + writer.Write(Velocity); + writer.Write(AngularVelocity); } protected override void Deserialize(NetworkReader reader) { base.Deserialize(reader); - _relativeVelocity = reader.ReadVector3(); - _relativeAngularVelocity = reader.ReadVector3(); + Velocity = reader.ReadVector3(); + AngularVelocity = reader.ReadVector3(); } protected override void GetFromAttached() { transform.position = ReferenceTransform.ToRelPos(AttachedRigidbody.GetPosition()); transform.rotation = ReferenceTransform.ToRelRot(AttachedRigidbody.GetRotation()); - _relativeVelocity = ReferenceRigidbody.ToRelVel(AttachedRigidbody.GetVelocity(), AttachedRigidbody.GetPosition()); - _relativeAngularVelocity = ReferenceRigidbody.ToRelAngVel(AttachedRigidbody.GetAngularVelocity()); + Velocity = ReferenceRigidbody.ToRelVel(AttachedRigidbody.GetVelocity(), AttachedRigidbody.GetPosition()); + AngularVelocity = ReferenceRigidbody.ToRelAngVel(AttachedRigidbody.GetAngularVelocity()); } protected override void ApplyToAttached() @@ -84,36 +82,16 @@ namespace QSB.Syncs.Unsectored.Rigidbodies AttachedRigidbody.MoveToPosition(positionToSet); AttachedRigidbody.MoveToRotation(rotationToSet); - var targetVelocity = ReferenceRigidbody.FromRelVel(_relativeVelocity, targetPos); - var targetAngularVelocity = ReferenceRigidbody.FromRelAngVel(_relativeAngularVelocity); + var targetVelocity = ReferenceRigidbody.FromRelVel(Velocity, targetPos); + var targetAngularVelocity = ReferenceRigidbody.FromRelAngVel(AngularVelocity); AttachedRigidbody.SetVelocity(targetVelocity); AttachedRigidbody.SetAngularVelocity(targetAngularVelocity); } - protected override bool HasChanged() - { - if (Vector3.Distance(transform.position, _prevPosition) > PositionMovedThreshold) - { - return true; - } - - if (Quaternion.Angle(transform.rotation, _prevRotation) > AngleRotatedThreshold) - { - return true; - } - - if (Vector3.Distance(_relativeVelocity, _prevVelocity) > VelocityChangeThreshold) - { - return true; - } - - if (Vector3.Distance(_relativeAngularVelocity, _prevAngularVelocity) > AngVelocityChangeThreshold) - { - return true; - } - - return false; - } + protected override bool HasChanged() => + base.HasChanged() || + Vector3.Distance(Velocity, _prevVelocity) > VelocityChangeThreshold || + Vector3.Distance(AngularVelocity, _prevAngularVelocity) > AngularVelocityChangeThreshold; } }