forked from fbsamples/oculus-networked-physics-sample
-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Snapshot.cs
405 lines (333 loc) · 13.7 KB
/
Snapshot.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
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
/**
* Copyright (c) 2017-present, Facebook, Inc.
* All rights reserved.
*
* This source code is licensed under the BSD-style license found in the
* LICENSE file in the Scripts directory of this source tree. An additional grant
* of patent rights can be found in the PATENTS file in the same directory.
*/
using System;
using UnityEngine;
using System.Collections.Generic;
public struct CubeState
{
public bool active;
public int authorityIndex;
public ushort authoritySequence;
public ushort ownershipSequence;
public int position_x;
public int position_y;
public int position_z;
public uint rotation_largest;
public uint rotation_a;
public uint rotation_b;
public uint rotation_c;
public int linear_velocity_x;
public int linear_velocity_y;
public int linear_velocity_z;
public int angular_velocity_x;
public int angular_velocity_y;
public int angular_velocity_z;
public static CubeState defaults;
};
public struct CubeDelta
{
#if DEBUG_DELTA_COMPRESSION
public int absolute_position_x;
public int absolute_position_y;
public int absolute_position_z;
#endif // #if DEBUG_DELTA_COMPRESSION
public int position_delta_x;
public int position_delta_y;
public int position_delta_z;
public int linear_velocity_delta_x;
public int linear_velocity_delta_y;
public int linear_velocity_delta_z;
public int angular_velocity_delta_x;
public int angular_velocity_delta_y;
public int angular_velocity_delta_z;
};
public class Snapshot
{
public CubeState[] cubeState = new CubeState[Constants.NumCubes];
public static void QuaternionToSmallestThree( Quaternion quaternion, out uint largest, out uint integer_a, out uint integer_b, out uint integer_c )
{
const float minimum = - 1.0f / 1.414214f; // 1.0f / sqrt(2)
const float maximum = + 1.0f / 1.414214f;
const float scale = (float) ( ( 1 << Constants.RotationBits ) - 1 );
float x = quaternion.x;
float y = quaternion.y;
float z = quaternion.z;
float w = quaternion.w;
float abs_x = Math.Abs( x );
float abs_y = Math.Abs( y );
float abs_z = Math.Abs( z );
float abs_w = Math.Abs( w );
float largest_value = abs_x;
largest = 0;
if ( abs_y > largest_value )
{
largest = 1;
largest_value = abs_y;
}
if ( abs_z > largest_value )
{
largest = 2;
largest_value = abs_z;
}
if ( abs_w > largest_value )
{
largest = 3;
largest_value = abs_w;
}
float a = 0;
float b = 0;
float c = 0;
switch ( largest )
{
case 0:
if ( x >= 0 )
{
a = y;
b = z;
c = w;
}
else
{
a = -y;
b = -z;
c = -w;
}
break;
case 1:
if ( y >= 0 )
{
a = x;
b = z;
c = w;
}
else
{
a = -x;
b = -z;
c = -w;
}
break;
case 2:
if ( z >= 0 )
{
a = x;
b = y;
c = w;
}
else
{
a = -x;
b = -y;
c = -w;
}
break;
case 3:
if ( w >= 0 )
{
a = x;
b = y;
c = z;
}
else
{
a = -x;
b = -y;
c = -z;
}
break;
}
float normal_a = ( a - minimum ) / ( maximum - minimum );
float normal_b = ( b - minimum ) / ( maximum - minimum );
float normal_c = ( c - minimum ) / ( maximum - minimum );
integer_a = (uint) Math.Floor( normal_a * scale + 0.5f );
integer_b = (uint) Math.Floor( normal_b * scale + 0.5f );
integer_c = (uint) Math.Floor( normal_c * scale + 0.5f );
}
public static Quaternion SmallestThreeToQuaternion( uint largest, uint integer_a, uint integer_b, uint integer_c )
{
const float minimum = - 1.0f / 1.414214f; // 1.0f / sqrt(2)
const float maximum = + 1.0f / 1.414214f;
const float scale = (float) ( ( 1 << Constants.RotationBits ) - 1 );
const float inverse_scale = 1.0f / scale;
float a = integer_a * inverse_scale * ( maximum - minimum ) + minimum;
float b = integer_b * inverse_scale * ( maximum - minimum ) + minimum;
float c = integer_c * inverse_scale * ( maximum - minimum ) + minimum;
float x = 0.0f;
float y = 0.0f;
float z = 0.0f;
float w = 0.0f;
switch ( largest )
{
case 0:
{
x = (float) Math.Sqrt( 1 - a*a - b*b - c*c );
y = a;
z = b;
w = c;
}
break;
case 1:
{
x = a;
y = (float) Math.Sqrt( 1 - a*a - b*b - c*c );
z = b;
w = c;
}
break;
case 2:
{
x = a;
y = b;
z = (float) Math.Sqrt( 1 - a*a - b*b - c*c );
w = c;
}
break;
case 3:
{
x = a;
y = b;
z = c;
w = (float) Math.Sqrt( 1 - a*a - b*b - c*c );
}
break;
}
// IMPORTANT: We must normalize the quaternion here because it will have slight drift otherwise due to being quantized
float norm = x*x + y*y + z*z + w*w;
if ( norm > 0.000001f )
{
var quaternion = new Quaternion( x, y, z, w );
float length = (float) Math.Sqrt( norm );
quaternion.x /= length;
quaternion.y /= length;
quaternion.z /= length;
quaternion.w /= length;
return quaternion;
}
else
{
return new Quaternion( 0, 0, 0, 1 );
}
}
public static void ClampPosition( ref int position_x, ref int position_y, ref int position_z )
{
if ( position_x < Constants.PositionMinimumXZ )
position_x = Constants.PositionMinimumXZ;
else if ( position_x > Constants.PositionMaximumXZ )
position_x = Constants.PositionMaximumXZ;
if ( position_y < Constants.PositionMinimumY )
position_y = Constants.PositionMinimumY;
else if ( position_y > Constants.PositionMaximumY )
position_y = Constants.PositionMaximumY;
if ( position_z < Constants.PositionMinimumXZ )
position_z = Constants.PositionMinimumXZ;
else if ( position_z > Constants.PositionMaximumXZ )
position_z = Constants.PositionMaximumXZ;
}
public static void ClampLinearVelocity( ref int linear_velocity_x, ref int linear_velocity_y, ref int linear_velocity_z )
{
if ( linear_velocity_x < Constants.LinearVelocityMinimum )
linear_velocity_x = Constants.LinearVelocityMinimum;
else if ( linear_velocity_x > Constants.LinearVelocityMaximum )
linear_velocity_x = Constants.LinearVelocityMaximum;
if ( linear_velocity_y < Constants.LinearVelocityMinimum )
linear_velocity_y = Constants.LinearVelocityMinimum;
else if ( linear_velocity_y > Constants.LinearVelocityMaximum )
linear_velocity_y = Constants.LinearVelocityMaximum;
if ( linear_velocity_z < Constants.LinearVelocityMinimum )
linear_velocity_z = Constants.LinearVelocityMinimum;
else if ( linear_velocity_z > Constants.LinearVelocityMaximum )
linear_velocity_z = Constants.LinearVelocityMaximum;
}
public static void ClampAngularVelocity( ref int angular_velocity_x, ref int angular_velocity_y, ref int angular_velocity_z )
{
if ( angular_velocity_x < Constants.AngularVelocityMinimum )
angular_velocity_x = Constants.AngularVelocityMinimum;
else if ( angular_velocity_x > Constants.AngularVelocityMaximum )
angular_velocity_x = Constants.AngularVelocityMaximum;
if ( angular_velocity_y < Constants.AngularVelocityMinimum )
angular_velocity_y = Constants.AngularVelocityMinimum;
else if ( angular_velocity_y > Constants.AngularVelocityMaximum )
angular_velocity_y = Constants.AngularVelocityMaximum;
if ( angular_velocity_z < Constants.AngularVelocityMinimum )
angular_velocity_z = Constants.AngularVelocityMinimum;
else if ( angular_velocity_z > Constants.AngularVelocityMaximum )
angular_velocity_z = Constants.AngularVelocityMaximum;
}
public static void ClampLocalPosition( ref int position_x, ref int position_y, ref int position_z )
{
if ( position_x < Constants.LocalPositionMinimum )
position_x = Constants.LocalPositionMinimum;
else if ( position_x > Constants.LocalPositionMaximum )
position_x = Constants.LocalPositionMaximum;
if ( position_y < Constants.LocalPositionMinimum )
position_y = Constants.LocalPositionMinimum;
else if ( position_y > Constants.LocalPositionMaximum )
position_y = Constants.LocalPositionMaximum;
if ( position_z < Constants.LocalPositionMinimum )
position_z = Constants.LocalPositionMinimum;
else if ( position_z > Constants.LocalPositionMaximum )
position_z = Constants.LocalPositionMaximum;
}
public static void GetCubeState( Rigidbody rigidBody, NetworkInfo networkInfo, ref CubeState cubeState, ref Vector3 origin )
{
cubeState.active = !rigidBody.IsSleeping();
cubeState.authorityIndex = networkInfo.GetAuthorityIndex();
cubeState.authoritySequence = networkInfo.GetAuthoritySequence();
cubeState.ownershipSequence = networkInfo.GetOwnershipSequence();
Vector3 position = rigidBody.position - origin;
cubeState.position_x = (int) Math.Floor( position.x * Constants.UnitsPerMeter + 0.5f );
cubeState.position_y = (int) Math.Floor( position.y * Constants.UnitsPerMeter + 0.5f );
cubeState.position_z = (int) Math.Floor( position.z * Constants.UnitsPerMeter + 0.5f );
Snapshot.QuaternionToSmallestThree( rigidBody.rotation,
out cubeState.rotation_largest,
out cubeState.rotation_a,
out cubeState.rotation_b,
out cubeState.rotation_c );
cubeState.linear_velocity_x = (int) Math.Floor( rigidBody.velocity.x * Constants.UnitsPerMeter + 0.5f );
cubeState.linear_velocity_y = (int) Math.Floor( rigidBody.velocity.y * Constants.UnitsPerMeter + 0.5f );
cubeState.linear_velocity_z = (int) Math.Floor( rigidBody.velocity.z * Constants.UnitsPerMeter + 0.5f );
cubeState.angular_velocity_x = (int) Math.Floor( rigidBody.angularVelocity.x * Constants.UnitsPerMeter + 0.5f );
cubeState.angular_velocity_y = (int) Math.Floor( rigidBody.angularVelocity.y * Constants.UnitsPerMeter + 0.5f );
cubeState.angular_velocity_z = (int) Math.Floor( rigidBody.angularVelocity.z * Constants.UnitsPerMeter + 0.5f );
ClampPosition( ref cubeState.position_x, ref cubeState.position_y, ref cubeState.position_z );
ClampLinearVelocity( ref cubeState.linear_velocity_x, ref cubeState.linear_velocity_y, ref cubeState.linear_velocity_z );
ClampAngularVelocity( ref cubeState.angular_velocity_x, ref cubeState.angular_velocity_y, ref cubeState.angular_velocity_z );
}
public static void ApplyCubeState( Rigidbody rigidBody, NetworkInfo networkInfo, ref CubeState cubeState, ref Vector3 origin, bool smoothing = false )
{
if ( networkInfo.IsHeldByPlayer() )
networkInfo.DetachCubeFromPlayer();
if ( cubeState.active )
{
if ( rigidBody.IsSleeping() )
rigidBody.WakeUp();
}
if ( !cubeState.active )
{
if ( !rigidBody.IsSleeping() )
rigidBody.Sleep();
}
networkInfo.SetAuthorityIndex( cubeState.authorityIndex );
networkInfo.SetAuthoritySequence( cubeState.authoritySequence );
networkInfo.SetOwnershipSequence( cubeState.ownershipSequence );
Vector3 position = new Vector3( cubeState.position_x, cubeState.position_y, cubeState.position_z ) * 1.0f / Constants.UnitsPerMeter + origin;
Quaternion rotation = SmallestThreeToQuaternion( cubeState.rotation_largest, cubeState.rotation_a, cubeState.rotation_b, cubeState.rotation_c );
if ( smoothing )
{
networkInfo.MoveWithSmoothing( position, rotation );
}
else
{
rigidBody.position = position;
rigidBody.rotation = rotation;
}
rigidBody.velocity = new Vector3( cubeState.linear_velocity_x, cubeState.linear_velocity_y, cubeState.linear_velocity_z ) * 1.0f / Constants.UnitsPerMeter;
rigidBody.angularVelocity = new Vector3( cubeState.angular_velocity_x, cubeState.angular_velocity_y, cubeState.angular_velocity_z ) * 1.0f / Constants.UnitsPerMeter;
}
};