SMOOTH rope reconciliation

No collisions or enemies tho
This commit is contained in:
Sveske_Juice 2024-05-27 13:40:48 +02:00
parent 15d0079a2d
commit 3364a2785d
5 changed files with 82 additions and 14 deletions

View File

@ -179,7 +179,7 @@ LineRenderer:
m_Curve: m_Curve:
- serializedVersion: 3 - serializedVersion: 3
time: 0 time: 0
value: 0.2280693 value: 0.2160211
inSlope: 0 inSlope: 0
outSlope: 0 outSlope: 0
tangentMode: 0 tangentMode: 0

View File

@ -2238,9 +2238,9 @@ GameObject:
serializedVersion: 6 serializedVersion: 6
m_Component: m_Component:
- component: {fileID: 34894283} - component: {fileID: 34894283}
- component: {fileID: 34894286}
- component: {fileID: 34894284} - component: {fileID: 34894284}
- component: {fileID: 34894285} - component: {fileID: 34894285}
- component: {fileID: 34894286}
m_Layer: 0 m_Layer: 0
m_Name: Rope m_Name: Rope
m_TagString: Untagged m_TagString: Untagged
@ -2276,12 +2276,14 @@ MonoBehaviour:
m_Name: m_Name:
m_EditorClassIdentifier: m_EditorClassIdentifier:
solveIterations: 10 solveIterations: 10
subDivision: 20 subDivision: 30
distBetweenRopePoints: 1.5 distBetweenRopePoints: 0.75
lineRenderer: {fileID: 34894285} lineRenderer: {fileID: 34894285}
rope: rope:
points: [] points: []
sticks: [] sticks: []
reconciliateThreshold: 2
reconciliateLerpFrameDuration: 9
stateBuffer: stateBuffer:
buffer: [] buffer: []
bufferSize: 0 bufferSize: 0
@ -2294,7 +2296,7 @@ LineRenderer:
m_PrefabAsset: {fileID: 0} m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 34894282} m_GameObject: {fileID: 34894282}
m_Enabled: 1 m_Enabled: 1
m_CastShadows: 1 m_CastShadows: 0
m_ReceiveShadows: 1 m_ReceiveShadows: 1
m_DynamicOccludee: 1 m_DynamicOccludee: 1
m_StaticShadowCaster: 0 m_StaticShadowCaster: 0
@ -2308,7 +2310,7 @@ LineRenderer:
m_RenderingLayerMask: 1 m_RenderingLayerMask: 1
m_RendererPriority: 0 m_RendererPriority: 0
m_Materials: m_Materials:
- {fileID: 10306, guid: 0000000000000000f000000000000000, type: 0} - {fileID: 2100000, guid: 62e10f2f26f232f49affb7663a8064fb, type: 2}
m_StaticBatchInfo: m_StaticBatchInfo:
firstSubMesh: 0 firstSubMesh: 0
subMeshCount: 0 subMeshCount: 0
@ -2338,7 +2340,7 @@ LineRenderer:
m_Curve: m_Curve:
- serializedVersion: 3 - serializedVersion: 3
time: 0 time: 0
value: 0.10041046 value: 0.21601537
inSlope: 0 inSlope: 0
outSlope: 0 outSlope: 0
tangentMode: 0 tangentMode: 0
@ -2381,7 +2383,7 @@ LineRenderer:
numCornerVertices: 0 numCornerVertices: 0
numCapVertices: 0 numCapVertices: 0
alignment: 0 alignment: 0
textureMode: 0 textureMode: 1
textureScale: {x: 1, y: 1} textureScale: {x: 1, y: 1}
shadowBias: 0.5 shadowBias: 0.5
generateLightingData: 0 generateLightingData: 0

View File

@ -93,6 +93,22 @@ public class Rope
return builder.Build(); return builder.Build();
} }
public Rope Copy(float stickLength)
{
RopeBuilder builder = new();
foreach (var point in points)
{
builder.AddPoint(new Point(point.position, point.locked));
}
for (int i = 0; i < points.Length - 1; i++)
{
builder.ConnectPointsWithDesiredLength(i, i + 1, stickLength);
}
return builder.Build();
}
/* public void NetworkSerialize<T>(BufferSerializer<T> serializer) where T : IReaderWriter /* public void NetworkSerialize<T>(BufferSerializer<T> serializer) where T : IReaderWriter
{ {
int pLen = 0; int pLen = 0;

View File

@ -2,6 +2,7 @@ using UnityEngine;
using UnityEngine.Assertions; using UnityEngine.Assertions;
using System.Linq; using System.Linq;
using Unity.Netcode; using Unity.Netcode;
using System.Collections;
public class RopeSimulator : NetworkBehaviour public class RopeSimulator : NetworkBehaviour
{ {
@ -19,11 +20,16 @@ public class RopeSimulator : NetworkBehaviour
[SerializeField] Rope rope; [SerializeField] Rope rope;
[Header("Networking")] [Header("Networking")]
[SerializeField] CircularBuffer<RopeState> stateBuffer;
[SerializeField] float reconciliateThreshold = 1f; [SerializeField] float reconciliateThreshold = 1f;
[SerializeField] int reconciliateLerpFrameDuration = 5;
[SerializeField] CircularBuffer<RopeState> stateBuffer;
[SerializeField] float reconciliateCooldownDuration = 1f;
float reconciliateCooldown = -3f; // First reconciliate after n seconds after its begun to populate stateBuffer
private const int k_stateBufferSize = 128; private const int k_stateBufferSize = 128;
private int currentTick => NetworkManager.Singleton.NetworkTickSystem.LocalTime.Tick; private int currentTick => NetworkManager.Singleton.NetworkTickSystem.LocalTime.Tick;
bool initted = false;
private void Awake() private void Awake()
{ {
this.rope = null; this.rope = null;
@ -45,8 +51,9 @@ public class RopeSimulator : NetworkBehaviour
private void Update() private void Update()
{ {
if (rope == null) return; if (!initted) return;
reconciliateCooldown += Time.deltaTime;
rope.points.First().position = ropeStart.position; rope.points.First().position = ropeStart.position;
rope.points.Last().position = ropeEnd.position; rope.points.Last().position = ropeEnd.position;
Simulate(this.rope, Time.deltaTime, this.distBetweenRopePoints, this.solveIterations); Simulate(this.rope, Time.deltaTime, this.distBetweenRopePoints, this.solveIterations);
@ -61,6 +68,8 @@ public class RopeSimulator : NetworkBehaviour
this.rope = RopeSimulator.BuildRope(this.ropeStart, this.ropeEnd, this.subDivision, this.distBetweenRopePoints); this.rope = RopeSimulator.BuildRope(this.ropeStart, this.ropeEnd, this.subDivision, this.distBetweenRopePoints);
NetworkManager.Singleton.NetworkTickSystem.Tick += NetworkTick; NetworkManager.Singleton.NetworkTickSystem.Tick += NetworkTick;
initted = true;
} }
private void NetworkTick() private void NetworkTick()
@ -68,7 +77,8 @@ public class RopeSimulator : NetworkBehaviour
RopeState ropeState = new() RopeState ropeState = new()
{ {
tick = currentTick, tick = currentTick,
nrope = Rope.ToNetworkRope(this.rope) nrope = Rope.ToNetworkRope(this.rope),
playerPositions = GameObject.FindObjectsByType<PlayerMovement>(FindObjectsSortMode.None).Select(p => new Vector3(p.transform.position.x, p.transform.position.y, p.GetComponent<NetworkObject>().NetworkObjectId)).ToArray()
}; };
stateBuffer.Add(ropeState, currentTick); stateBuffer.Add(ropeState, currentTick);
@ -84,13 +94,52 @@ public class RopeSimulator : NetworkBehaviour
{ {
RopeState clientState = this.stateBuffer.Get(serverState.tick); RopeState clientState = this.stateBuffer.Get(serverState.tick);
Rope serverRope = Rope.FromNetworkRope(serverState.nrope, this.distBetweenRopePoints); Rope serverRope = Rope.FromNetworkRope(serverState.nrope, this.distBetweenRopePoints);
// Not enough information on client to reconcile
if (clientState.nrope.Equals(default(NetworkRope)))
return;
Rope previousClientRope = Rope.FromNetworkRope(clientState.nrope, this.distBetweenRopePoints); Rope previousClientRope = Rope.FromNetworkRope(clientState.nrope, this.distBetweenRopePoints);
float diff = Rope.CalcDiff(previousClientRope, serverRope); float diff = Rope.CalcDiff(previousClientRope, serverRope);
Debug.Log($"Diff: {diff}, ({serverState.tick})/({clientState.tick})"); // Debug.Log($"Diff: {diff}, ({serverState.tick})/({currentTick})");
if (diff > reconciliateThreshold) if (diff > reconciliateThreshold && reconciliateCooldown >= reconciliateCooldownDuration)
{ {
this.rope = serverRope; reconciliateCooldown = 0f;
// StopCoroutine("ReconciliateLerp");
// StartCoroutine(ReconciliateLerp(serverRope));
Rope localCopy = serverRope.Copy(this.distBetweenRopePoints);
int ticksToResimulate = currentTick - serverState.tick;
Debug.Log($"Resimulating {ticksToResimulate} ticks");
for (int i = 1; i <= ticksToResimulate; i++)
{
RopeState intermediateState = stateBuffer.Get(serverState.tick + i);
Vector3 intermediateRopeStart = new Vector3(intermediateState.playerPositions[0].x, intermediateState.playerPositions[0].y, ropeStart.position.z);
Vector3 intermediateRopeEnd = new Vector3(intermediateState.playerPositions[1].x, intermediateState.playerPositions[1].y, ropeEnd.position.z);
rope.points.First().position = intermediateRopeStart;
rope.points.Last().position = intermediateRopeEnd;
Simulate(localCopy, 1f / (float) NetworkManager.Singleton.NetworkTickSystem.TickRate, this.distBetweenRopePoints, this.solveIterations);
}
localCopy.points.First().position = ropeStart.position;
localCopy.points.Last().position = ropeEnd.position;
StopCoroutine("LerpRope");
LerpRope(this.rope, localCopy);
}
}
IEnumerator LerpRope(Rope from, Rope to)
{
for (int i = 1; i <= reconciliateLerpFrameDuration; i++)
{
float t = (float) i / (float) reconciliateLerpFrameDuration;
for (int j = 0; j < this.rope.points.Length; j++)
{
from.points[j].position = Vector3.Lerp(this.rope.points[j].position, to.points[j].position, t);
from.points[j].prevPosition = Vector3.Lerp(this.rope.points[j].prevPosition, to.points[j].prevPosition, t);
}
yield return new WaitForEndOfFrame();
} }
} }

View File

@ -6,6 +6,7 @@ public struct RopeState : INetworkSerializable
{ {
public int tick; public int tick;
public NetworkRope nrope; public NetworkRope nrope;
public Vector3[] playerPositions;
public override int GetHashCode() public override int GetHashCode()
{ {