Skip to content

Commit 47e5340

Browse files
committed
Fix stutter under jitter!
Add lag simulation in DefaultNetworkHelper; Remove missing component in ExampleScene
1 parent 8f48ffb commit 47e5340

File tree

10 files changed

+178
-117
lines changed

10 files changed

+178
-117
lines changed

Core/Game/Managers/ClientManager.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ public static void Initialize(NetworkHelper networkHelper)
5959
NetworkHelper = networkHelper;
6060
NetworkHelper.OnFrameData += HandleFrameData;
6161
NetworkHelper.OnInitData += HandleInitData;
62+
NetworkHelper.Initialize ();
6263

6364
LSServer.Initialize();
6465
GameStarted = false;

Core/Game/Managers/Network/NetworkHelper.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,11 @@ public abstract class NetworkHelper : MonoBehaviour{
1919
public Action<byte[]> OnMatchmakingData; //Matchmaking data NU
2020
public Action<byte[]> OnRegisterData; //For registering clients NU
2121
public Action<byte[]> OnTestData; //For test purposes
22-
23-
public NetworkHelper () {
24-
25-
}
2622

23+
public virtual void Initialize ()
24+
{
25+
26+
}
2727
/// <summary>
2828
/// Connecting to a server with IP address of ip. Note: Not all NetworkHelpers will require Connect.
2929
/// </summary>

Core/Game/Player/Commands/Command.cs.meta

100755100644
File mode changed.

Core/Game/Player/Commands/CommandManager.cs.meta

100755100644
File mode changed.

Core/Game/Player/Commands/Default.meta

100755100644
File mode changed.

Core/Game/Player/Commands/ICommandData.cs.meta

100755100644
File mode changed.

Core/Simulation/Physics/Core/LSBody.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -593,12 +593,12 @@ public void SetVisuals()
593593
DoSetVisualPosition(
594594
_position.ToVector3(HeightPos.ToFloat())
595595
);
596-
PositionalTransform.position = Vector3.SmoothDamp(lastVisualPos, _visualPosition, ref velocityPosition, Time.fixedDeltaTime);
596+
PositionalTransform.position = Vector3.SmoothDamp (lastVisualPos, _visualPosition, ref velocityPosition, Time.fixedDeltaTime);
597597
}
598598

599599
if (this.SetVisualRotation) {
600600
this.DoSetVisualRotation(_rotation);
601-
RotationalTransform.rotation = Quaternion.Slerp(lastVisualRot, visualRot, 1f/0.032f * Time.deltaTime);
601+
RotationalTransform.rotation = Quaternion.Slerp(lastVisualRot, visualRot, 1f/Time.fixedDeltaTime);
602602
}
603603

604604
}

Example/DefaultNetworkHelper.cs

Lines changed: 102 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,41 +1,106 @@
11
using UnityEngine;
22
using System.Collections;
3+
using System.Collections.Generic;
4+
namespace Lockstep.NetworkHelpers
5+
{
6+
public class DefaultNetworkHelper : NetworkHelper
7+
{
38

4-
namespace Lockstep.NetworkHelpers {
5-
public class DefaultNetworkHelper : NetworkHelper {
6-
public override void Connect (string ip) {
7-
8-
}
9-
public override void Disconnect () {
10-
11-
}
12-
public override void Host (int roomSize) {
13-
14-
}
15-
public override int ID {
16-
get {
17-
return 0;
18-
}
19-
}
20-
public override bool IsConnected {
21-
get {
22-
return true;
23-
}
24-
}
25-
public override bool IsServer {
26-
get {
27-
return true;
28-
}
29-
}
30-
public override int PlayerCount {
31-
get {
32-
return 1;
33-
}
34-
}
35-
protected override void OnSendMessageToAll(MessageType messageType, byte[] data)
36-
{
37-
base.OnSendMessageToAll(messageType,data);
38-
}
39-
40-
}
9+
public bool SimulateLag;
10+
/// <summary>
11+
/// Latency in milliseconds.
12+
/// </summary>
13+
public double Latency;
14+
public double Jitter;
15+
16+
public override void Initialize ()
17+
{
18+
19+
}
20+
21+
public override void Connect (string ip)
22+
{
23+
24+
}
25+
public override void Disconnect ()
26+
{
27+
28+
}
29+
public override void Host (int roomSize)
30+
{
31+
32+
}
33+
public override int ID {
34+
get {
35+
return 0;
36+
}
37+
}
38+
public override bool IsConnected {
39+
get {
40+
return true;
41+
}
42+
}
43+
public override bool IsServer {
44+
get {
45+
return true;
46+
}
47+
}
48+
public override int PlayerCount {
49+
get {
50+
return 1;
51+
}
52+
}
53+
54+
List<Packet> Packets = new List<Packet> ();
55+
56+
protected override void OnSendMessageToAll (MessageType messageType, byte [] data)
57+
{
58+
if (SimulateLag) {
59+
Packets.Add (new Packet (messageType, data, Latency + Random.Range(0f,(float)Jitter)));
60+
} else {
61+
base.OnSendMessageToAll (messageType, data);
62+
}
63+
}
64+
65+
void Update ()
66+
{
67+
if (SimulateLag == false) return;
68+
69+
/*FastList<int> list = new FastList<int> () { 1, 2, 3, 4, 5, 6, 7, 8 };
70+
string ss = "";
71+
for (int j = 0; j < list.Count; j++) {
72+
ss += list [j] + ", ";
73+
74+
list.RemoveAt (j);
75+
j--;
76+
}
77+
Debug.Log (ss);
78+
return;*/
79+
for (int i = 0; i < Packets.Count; i++) {
80+
var packet = Packets [i];
81+
packet.TimeTillArrival -= Time.unscaledDeltaTime * 1000d;
82+
if (packet.TimeTillArrival <= 0) {
83+
base.OnSendMessageToAll (packet.MessageType, packet.Data);
84+
Packets.RemoveAt (i);
85+
i--;
86+
} else {
87+
Packets [i] = packet;
88+
}
89+
}
90+
}
91+
92+
struct Packet
93+
{
94+
public Packet (MessageType messageType, byte [] data, double timeTillArrival)
95+
{
96+
TimeTillArrival = timeTillArrival;
97+
MessageType = messageType;
98+
Data = data;
99+
}
100+
public double TimeTillArrival;
101+
public MessageType MessageType;
102+
public byte [] Data;
103+
}
104+
105+
}
41106
}

Example/ExampleNetworkHelper.cs.meta renamed to Example/DefaultNetworkHelper.cs.meta

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)