-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathClientShipPhysics.cs
269 lines (211 loc) · 9.27 KB
/
ClientShipPhysics.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
using Improbable.Unity;
using Improbable.Unity.Visualizer;
using Improbable.Worker;
using NetworkOptimization;
using RogueFleet.Ship;
using System.Collections.Generic;
using UnityEngine;
namespace Assets.GameLogic.Ship
{
[WorkerType(WorkerPlatform.UnityClient), DontAutoEnable]
public class ClientShipPhysics : MonoBehaviour
{
[Header("Exponentialy Smoothed Moving Average Error")]
[Range(0, 100)]
public uint positionSmoothing = 0;
[Range(0, 100)]
public uint rotationSmoothing = 0;
[Header("Position Error Reduction")]
public float maxReductionAtDistance = 1f;
[Range(0, 100)]
public uint maxPositionReduction = 100;
[Range(0, 100)]
public uint minPositionReduction = 100;
[Header("Rotation Error Reduction")]
[Range(0.01f, 1f)]
public float maxReductionAtMagnitude = 0.01f;
[Range(0, 100)]
public uint maxRotationReduction = 100;
[Range(0, 100)]
public uint minRotationReduction = 100;
[Header("Misc.")]
public bool useRemoteStateBuffer = true;
public bool showDebugInfo = false;
public Rigidbody shipRigidbodyPrefab;
Rigidbody shipExternalRigidbody;
const byte bufferCount = 5;
Queue<Vector3> positionBuffer = new Queue<Vector3>(bufferCount);
Queue<Quaternion> rotationBuffer = new Queue<Quaternion>(bufferCount);
Queue<Vector3> velocityBuffer = new Queue<Vector3>(bufferCount);
Queue<Vector3> angularVelocityBuffer = new Queue<Vector3>(bufferCount);
Vector3 positionErrorOffset;
Vector3 smoothedPosition;
float biggestPositionError;
Quaternion rotationErrorOffset;
Quaternion smoothedRotation;
float biggestRotationError;
[Require] ShipPhysics.Reader ShipPhysicsReader;
void OnEnable()
{
ShipPhysicsReader.PositionUpdated.AddAndInvoke(OnPositionUpdated);
ShipPhysicsReader.RotationUpdated.AddAndInvoke(OnRotationUpdated);
ShipPhysicsReader.LinearVelocityUpdated.AddAndInvoke(OnVelocityUpdated);
ShipPhysicsReader.AngularVelocityUpdated.AddAndInvoke(OnAngularVelocityUpdated);
}
void Start()//Called only once in the object's lifetime which is desired behavior in this case
{
shipExternalRigidbody = Instantiate(shipRigidbodyPrefab, transform.position, transform.rotation);
var engine = GetComponentInChildren<ShipEngine>();
shipExternalRigidbody.centerOfMass = engine.ShipBody.centerOfMass;
shipExternalRigidbody.inertiaTensor = engine.ShipBody.inertiaTensor;
shipExternalRigidbody.inertiaTensorRotation = engine.ShipBody.inertiaTensorRotation;
engine.ShipBody.isKinematic = true;
engine.ShipBody = shipExternalRigidbody;
}
void OnDisable()
{
ShipPhysicsReader.PositionUpdated.Remove(OnPositionUpdated);
ShipPhysicsReader.RotationUpdated.Remove(OnRotationUpdated);
ShipPhysicsReader.LinearVelocityUpdated.Remove(OnVelocityUpdated);
ShipPhysicsReader.AngularVelocityUpdated.Remove(OnAngularVelocityUpdated);
}
void OnPositionUpdated(Bytes stateUpdate)
{
var decoded = Decode.Vector3f(stateUpdate.BackingArray);
var position = new Vector3(decoded[0], decoded[1], decoded[2]);
if (useRemoteStateBuffer)
{
if (positionBuffer.Count >= bufferCount)
{
positionBuffer.Dequeue();
}
positionBuffer.Enqueue(position);
}
else
{
shipExternalRigidbody.position = position;
}
}
void OnRotationUpdated(Bytes stateUpdate)
{
var decoded = Decode.Quaternion(stateUpdate.BackingArray);
var rotation = new Quaternion(decoded[0], decoded[1], decoded[2], decoded[3]);
if (useRemoteStateBuffer)
{
if (rotationBuffer.Count >= bufferCount)
{
rotationBuffer.Dequeue();
}
rotationBuffer.Enqueue(rotation);
}
else
{
shipExternalRigidbody.rotation = rotation;
}
}
void OnVelocityUpdated(Bytes stateUpdate)
{
var decoded = Decode.Velocity(stateUpdate.BackingArray, ShipPhysicsReader.Data.maxLinearVelocity);
var velocity = new Vector3(decoded[0], decoded[1], decoded[2]);
if (useRemoteStateBuffer)
{
if (velocityBuffer.Count >= bufferCount)
{
velocityBuffer.Dequeue();
}
velocityBuffer.Enqueue(velocity);
}
else
{
shipExternalRigidbody.velocity = velocity;
}
}
void OnAngularVelocityUpdated(Bytes stateUpdate)
{
var decoded = Decode.Velocity(stateUpdate.BackingArray, ShipPhysicsReader.Data.maxAngularVelocity);
var velocity = new Vector3(decoded[0], decoded[1], decoded[2]);
if (useRemoteStateBuffer)
{
if (angularVelocityBuffer.Count >= bufferCount)
{
angularVelocityBuffer.Dequeue();
}
angularVelocityBuffer.Enqueue(velocity);
}
else
{
shipExternalRigidbody.angularVelocity = velocity;
}
}
void FixedUpdate()
{
UpdateRigidbody();
}
void Update()
{
transform.SetPositionAndRotation(smoothedPosition, smoothedRotation);
}
void LateUpdate()
{
ErrorCorrection();
}
void UpdateRigidbody()
{
if (useRemoteStateBuffer)
{
if (positionBuffer.Count > 0)
{
var stateUpdate = positionBuffer.Dequeue();
shipExternalRigidbody.position = stateUpdate;
}
if (rotationBuffer.Count > 0)
{
var stateUpdate = rotationBuffer.Dequeue();
shipExternalRigidbody.rotation = stateUpdate;
}
if (velocityBuffer.Count > 0)
{
var stateUpdate = velocityBuffer.Dequeue();
shipExternalRigidbody.velocity = stateUpdate;
}
if (angularVelocityBuffer.Count > 0)
{
var stateUpdate = angularVelocityBuffer.Dequeue();
shipExternalRigidbody.angularVelocity = stateUpdate;
}
}
}
void ErrorCorrection()
{
//Error
var posError = transform.position - shipExternalRigidbody.position;
var rotError = transform.rotation * Quaternion.Inverse(shipExternalRigidbody.rotation);
//Exponentialy smoothed moving average error
positionErrorOffset += ((1f - (positionSmoothing / 100f)) * (posError - positionErrorOffset));
rotationErrorOffset *= Quaternion.Slerp(Quaternion.identity, rotError * Quaternion.Inverse(rotationErrorOffset), (1f - (rotationSmoothing / 100f)));
//Error magnitude
var posErrorMag = positionErrorOffset.sqrMagnitude;
var rotErrorMag = 1f - Quaternion.Dot(Quaternion.identity, rotationErrorOffset);//dot product of 1 == same rotation
//Dynamic Error Reduction Ratio
var posMin = 1f - minPositionReduction / 100f;
var posMax = 1f - maxPositionReduction / 100f;
var rotMin = 1f - minRotationReduction / 100f;
var rotMax = 1f - maxRotationReduction / 100f;
var dynamicPositionErrorReduction = Mathf.Lerp(posMin, posMax, posErrorMag / maxReductionAtDistance);
var dynamicRotationErrorReduction = Mathf.Lerp(rotMin, rotMax, rotErrorMag / maxReductionAtMagnitude);
if (showDebugInfo)
{
if (biggestPositionError < posErrorMag) biggestPositionError = posErrorMag;
if (biggestRotationError < rotErrorMag) biggestRotationError = rotErrorMag;
Debug.Log(Time.frameCount + " Positional Error " + posErrorMag + " Reduction " + dynamicPositionErrorReduction + " Largest " + biggestPositionError);
Debug.Log(Time.frameCount + " Rotational Error " + rotErrorMag + " Reduction " + dynamicRotationErrorReduction + " Largest " + biggestRotationError);
}
//Actual Reduction of Error
positionErrorOffset *= dynamicPositionErrorReduction;
rotationErrorOffset = Quaternion.Slerp(Quaternion.identity, rotationErrorOffset, dynamicRotationErrorReduction);
//Apply result
smoothedPosition = shipExternalRigidbody.position + positionErrorOffset;
smoothedRotation = shipExternalRigidbody.rotation * rotationErrorOffset;
}
}
}