diff --git a/Assets/Scripts/Rope/Rope.cs b/Assets/Scripts/Rope/Rope.cs index 4ee9032..5753d8a 100644 --- a/Assets/Scripts/Rope/Rope.cs +++ b/Assets/Scripts/Rope/Rope.cs @@ -30,7 +30,7 @@ public class Rope return sum; } - public static float CalcDiff(Rope r1, Rope r2) + public static float CalcDiff(Rope r1, Rope r2, bool excludeZ = true) { if (r1.points.Length != r2.points.Length) { @@ -38,9 +38,22 @@ public class Rope } float diff = 0; - for (int i = 0; i < r1.points.Length; i++) + if (excludeZ) { - diff += Vector3.Distance(r1.points[i].position, r2.points[i].position); + for (int i = 0; i < r1.points.Length; i++) + { + Vector3 p1, p2; + p1 = new Vector2(r1.points[i].position.x, r1.points[i].position.y); + p2 = new Vector2(r2.points[i].position.x, r2.points[i].position.y); + diff += Vector2.Distance(p1, p2); + } + } + else + { + for (int i = 0; i < r1.points.Length; i++) + { + diff += Vector3.Distance(r1.points[i].position, r2.points[i].position); + } } return diff; } @@ -53,11 +66,11 @@ public class Rope prevPositions = rope.points.Select(p => p.prevPosition).ToArray(), locked = rope.points.Select(p => p.locked).ToArray(), - sPosA = rope.sticks.Select(s => s.A.position).ToArray(), - sPrevPosA = rope.sticks.Select(s => s.A.prevPosition).ToArray(), - sPosB = rope.sticks.Select(s => s.B.position).ToArray(), - sPrevPosB = rope.sticks.Select(s => s.B.prevPosition).ToArray(), - dead = rope.sticks.Select(s => s.dead).ToArray(), + // sPosA = rope.sticks.Select(s => s.A.position).ToArray(), + // sPrevPosA = rope.sticks.Select(s => s.A.prevPosition).ToArray(), + // sPosB = rope.sticks.Select(s => s.B.position).ToArray(), + // sPrevPosB = rope.sticks.Select(s => s.B.prevPosition).ToArray(), + // dead = rope.sticks.Select(s => s.dead).ToArray(), }; } @@ -132,24 +145,24 @@ public struct NetworkRope : INetworkSerializable public bool[] locked; // For rope sticks - public Vector3[] sPosA; - public Vector3[] sPrevPosA; - - public Vector3[] sPosB; - public Vector3[] sPrevPosB; - - public bool[] dead; + // public Vector3[] sPosA; + // public Vector3[] sPrevPosA; + // + // public Vector3[] sPosB; + // public Vector3[] sPrevPosB; + // + // public bool[] dead; public void NetworkSerialize(BufferSerializer serializer) where T : IReaderWriter { int positionsLen = 0; int prevPositionsLen = 0; int lockedLen = 0; - int sPosALen = 0; - int sPrevPosALen = 0; - int sPosBLen = 0; - int sPrevPosBLen = 0; - int deadLen = 0; + // int sPosALen = 0; + // int sPrevPosALen = 0; + // int sPosBLen = 0; + // int sPrevPosBLen = 0; + // int deadLen = 0; if (!serializer.IsReader) { @@ -157,21 +170,21 @@ public struct NetworkRope : INetworkSerializable prevPositionsLen = prevPositions.Length; lockedLen = locked.Length; - sPosALen = sPosA.Length; - sPrevPosALen = sPrevPosA.Length; - sPosBLen = sPosB.Length; - sPrevPosBLen = sPrevPosB.Length; - deadLen = dead.Length; + // sPosALen = sPosA.Length; + // sPrevPosALen = sPrevPosA.Length; + // sPosBLen = sPosB.Length; + // sPrevPosBLen = sPrevPosB.Length; + // deadLen = dead.Length; } serializer.SerializeValue(ref positionsLen); serializer.SerializeValue(ref prevPositionsLen); serializer.SerializeValue(ref lockedLen); - serializer.SerializeValue(ref sPosALen); - serializer.SerializeValue(ref sPrevPosALen); - serializer.SerializeValue(ref sPosBLen); - serializer.SerializeValue(ref sPrevPosBLen); - serializer.SerializeValue(ref deadLen); + // serializer.SerializeValue(ref sPosALen); + // serializer.SerializeValue(ref sPrevPosALen); + // serializer.SerializeValue(ref sPosBLen); + // serializer.SerializeValue(ref sPrevPosBLen); + // serializer.SerializeValue(ref deadLen); if (serializer.IsReader) { @@ -179,11 +192,11 @@ public struct NetworkRope : INetworkSerializable prevPositions = new Vector3[prevPositionsLen]; locked = new bool[lockedLen]; - sPosA = new Vector3[sPosALen]; - sPrevPosA = new Vector3[sPrevPosALen]; - sPosB = new Vector3[sPosBLen]; - sPrevPosB = new Vector3[sPrevPosBLen]; - dead = new bool[deadLen]; + // sPosA = new Vector3[sPosALen]; + // sPrevPosA = new Vector3[sPrevPosALen]; + // sPosB = new Vector3[sPosBLen]; + // sPrevPosB = new Vector3[sPrevPosBLen]; + // dead = new bool[deadLen]; } for (int i = 0; i < positionsLen; i++) @@ -201,7 +214,7 @@ public struct NetworkRope : INetworkSerializable serializer.SerializeValue(ref locked[i]); } - for (int i = 0; i < sPosALen; i++) +/* for (int i = 0; i < sPosALen; i++) { serializer.SerializeValue(ref sPosA[i]); } @@ -224,6 +237,6 @@ public struct NetworkRope : INetworkSerializable for (int i = 0; i < deadLen; i++) { serializer.SerializeValue(ref dead[i]); - } + } */ } } diff --git a/Assets/Scripts/Rope/RopeSimulator.cs b/Assets/Scripts/Rope/RopeSimulator.cs index 163fda1..8342616 100644 --- a/Assets/Scripts/Rope/RopeSimulator.cs +++ b/Assets/Scripts/Rope/RopeSimulator.cs @@ -66,7 +66,7 @@ public class RopeSimulator : NetworkBehaviour [Header("Netcode")] private const int k_rngSeed = 6969; private const float k_sendRopeDataRPCThreshold = 0.1f; - private const float k_ropeReconciliateThreshold = 0.1f; + private const float k_ropeReconciliateThreshold = 0.25f; private System.Random rng = new System.Random(k_rngSeed); private int[] order; @@ -280,12 +280,15 @@ public class RopeSimulator : NetworkBehaviour private void ServerRopeDataReceivedRpc(NetworkRope nrope) { Debug.Log($"Received rope data from server: {nrope}"); - Debug.Log(nrope.sPosA[1]); Rope serverRope = Rope.FromNetworkRope(nrope, distBetweenRopePoints); - if (Rope.CalcDiff(this.rope, serverRope) > k_ropeReconciliateThreshold) + + float diff = Rope.CalcDiff(this.rope, serverRope); + Debug.Log(diff); + if (diff > k_ropeReconciliateThreshold) { Debug.LogWarning("Reconciliating rope!"); this.rope = serverRope; + Debug.Log(Rope.CalcDiff(this.rope, serverRope)); } }