Skip to content

Commit 287b0e8

Browse files
committed
fix(serialization): Fixes reading and writing rotations
Fixes: #178
1 parent 5d59eb3 commit 287b0e8

File tree

2 files changed

+33
-20
lines changed

2 files changed

+33
-20
lines changed

MLAPI/NetworkingManagerComponents/Binary/BitReader.cs

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,7 @@ public object ReadObjectPacked(Type type)
126126
if (type == typeof(Ray))
127127
return ReadRayPacked();
128128
if (type == typeof(Quaternion))
129-
return ReadRotation(3);
129+
return ReadRotationPacked();
130130
if (type == typeof(char))
131131
return ReadCharPacked();
132132
if (type.IsEnum)
@@ -332,19 +332,24 @@ public double ReadRangedDouble(double minValue, double maxValue, int bytes)
332332
}
333333

334334
/// <summary>
335-
/// Read a rotation from the stream.
335+
/// Reads the rotation from the stream
336336
/// </summary>
337-
/// <param name="bytesPerAngle">How many bytes each angle occupies. Must be between 1 and 4 (inclusive)</param>
338337
/// <returns>The rotation read from the stream</returns>
338+
public Quaternion ReadRotationPacked()
339+
{
340+
float x = ReadSinglePacked();
341+
float y = ReadSinglePacked();
342+
float z = ReadSinglePacked();
343+
344+
float w = Mathf.Sqrt(1 - ((Mathf.Pow(x, 2) - (Mathf.Pow(y, 2) - (Mathf.Pow(z, 2))))));
345+
346+
return new Quaternion(x, y, z, w);
347+
}
348+
349+
[Obsolete("Use ReadRotationPacked instead")]
339350
public Quaternion ReadRotation(int bytesPerAngle)
340351
{
341-
if (bytesPerAngle < 1 || bytesPerAngle > 4) throw new ArgumentOutOfRangeException("Bytes per angle must be at least 1 byte and at most 4 bytes!");
342-
if (bytesPerAngle == 4) return Quaternion.Euler(ReadVector3());
343-
else return Quaternion.Euler(
344-
ReadRangedSingle(0f, 360f, bytesPerAngle), // X
345-
ReadRangedSingle(0f, 360f, bytesPerAngle), // Y
346-
ReadRangedSingle(0f, 360f, bytesPerAngle) // Z
347-
);
352+
return ReadRotationPacked();
348353
}
349354

350355
/// <summary>

MLAPI/NetworkingManagerComponents/Binary/BitWriter.cs

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,7 @@ public void WriteObjectPacked(object value)
141141
}
142142
else if (value is Quaternion)
143143
{
144-
WriteRotation((Quaternion)value, 3);
144+
WriteRotationPacked((Quaternion)value);
145145
return;
146146
}
147147
else if (value is char)
@@ -389,23 +389,31 @@ public void WriteRangedDouble(double value, double minValue, double maxValue, in
389389
}
390390

391391
/// <summary>
392-
/// Write a rotation to the stream.
392+
/// Writes the rotation to the stream.
393393
/// </summary>
394394
/// <param name="rotation">Rotation to write</param>
395-
/// <param name="bytesPerAngle">How many bytes each written angle should occupy. Must be between 1 and 4 (inclusive)</param>
396-
public void WriteRotation(Quaternion rotation, int bytesPerAngle)
395+
public void WriteRotationPacked(Quaternion rotation)
397396
{
398-
if (bytesPerAngle < 1 || bytesPerAngle > 4) throw new ArgumentOutOfRangeException("Bytes per angle must be at least 1 byte and at most 4 bytes!");
399-
if (bytesPerAngle == 4) WriteVector3(rotation.eulerAngles);
397+
if (Mathf.Sign(rotation.w) < 0)
398+
{
399+
WriteSinglePacked(-rotation.x);
400+
WriteSinglePacked(-rotation.y);
401+
WriteSinglePacked(-rotation.z);
402+
}
400403
else
401404
{
402-
Vector3 rot = rotation.eulerAngles;
403-
WriteRangedSingle(rot.x, 0f, 360f, bytesPerAngle);
404-
WriteRangedSingle(rot.y, 0f, 360f, bytesPerAngle);
405-
WriteRangedSingle(rot.z, 0f, 360f, bytesPerAngle);
405+
WriteSinglePacked(rotation.x);
406+
WriteSinglePacked(rotation.y);
407+
WriteSinglePacked(rotation.z);
406408
}
407409
}
408410

411+
[Obsolete("Use WriteRotationPacked instead")]
412+
public void WriteRotation(Quaternion rotation, int bytesPerAngle)
413+
{
414+
WriteRotationPacked(rotation);
415+
}
416+
409417
/// <summary>
410418
/// Writes a single bit
411419
/// </summary>

0 commit comments

Comments
 (0)