Skip to content

Commit 6a5a48e

Browse files
authored
fix: preventing NaN quaternions due to numerical precision issues (#498)
1 parent 54393f3 commit 6a5a48e

1 file changed

Lines changed: 4 additions & 1 deletion

File tree

  • com.unity.multiplayer.mlapi/Runtime/Serialization

com.unity.multiplayer.mlapi/Runtime/Serialization/BitReader.cs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -393,7 +393,10 @@ public Quaternion ReadRotationPacked()
393393
float y = ReadSinglePacked();
394394
float z = ReadSinglePacked();
395395

396-
float w = Mathf.Sqrt(1f - Mathf.Pow(x, 2) - Mathf.Pow(y, 2) - Mathf.Pow(z, 2));
396+
// numerical precision issues can make the remainder very slightly negative.
397+
// In this case, use 0 for w as, otherwise, w would be NaN.
398+
float remainder = 1f - Mathf.Pow(x, 2) - Mathf.Pow(y, 2) - Mathf.Pow(z, 2);
399+
float w = (remainder > 0f) ? Mathf.Sqrt(remainder) : 0.0f;
397400

398401
return new Quaternion(x, y, z, w);
399402
}

0 commit comments

Comments
 (0)