Skip to content

fix(serializing): correct two's-complement unwrap in quaternion decompression - #1067

Merged
FirstGearGames merged 1 commit into
FirstGearGames:mainfrom
ddelgado95:fix/quaternion-negative-unwrap
Aug 21, 2026
Merged

FirstGearGames merged 1 commit into
FirstGearGames:mainfrom
ddelgado95:fix/quaternion-negative-unwrap

Conversation

@ddelgado95

Copy link
Copy Markdown
Contributor

Problem

ScaleToUint encodes each smaller quaternion component as a two's-complement value masked to BitsPerAxis bits, but ScaleToFloat unwraps negatives by subtracting Maximum * 2 — which is 2 * IntScale steps, not the IntMask + 1 steps the mask actually wrapped by. Every negative component therefore decodes exactly two quantization steps too high. Positive components are unaffected.

Worked example (Quaternion32, IntScale = 511, IntMask = 1023):

  • -511 steps encodes as (uint)(-511) & 1023 = 513
  • correct decode: 513 - 1024 = -511
  • current decode: 513 * M/511 - 2M = M * (513 - 1022)/511 = -509

Quaternion64Compression.ScaleToFloat_H/_L carry the same off-by-two.

Evidence

Round-tripping 200,001 values across [-Maximum, +Maximum] through ScaleToUint and back:

decode worst error, negatives worst error, positives
before 0.00345941 0.00069195
after 0.00069195 0.00069195

0.00069195 is exactly half a quantization step — the theoretical optimum. That positives already sit there while negatives are 5× worse is what localizes the defect to the negative decode path; the encoder is correct.

Small magnitudes could flip sign outright:

raw      -> encoded -> before    / after
-0.00100 ->    1023 -> +0.00138  / -0.00138
-0.50000 ->     663 -> -0.49677  / -0.49954
-0.70000 ->     518 -> -0.69742  / -0.70019

Impact

Quaternion32 is the default AutoPackType.Packed path, used by RigidbodyState in every prediction reconcile and by NetworkTransform rotation sync. The constant +0.00277 bias lands as roughly a 0.3° rotation error on clients only — the server never decodes its own state.

Because it only appears on orientations where a smaller component is negative, it presents as intermittent jitter rather than a reproducible axis bug. In a predicted rigidbody it also couples into position: the client builds thrust in a frame tilted ~0.3° from the server's, so lateral thrust acquires a vertical component the server never simulates, and the reconcile pulls it back each tick — visible as a persistent offset under held thrust and a hop on taps.

Compatibility

The encoder is untouched, so this is wire-compatible in both directions. Only the decode of already-negative components changes.

…pression

ScaleToUint encodes the smaller components as two's complement masked to
BitsPerAxis bits, but ScaleToFloat unwrapped negatives by subtracting
Maximum * 2 -- that is 2 * IntScale steps, not the IntMask + 1 steps the
mask actually wrapped by. Every negative component decoded exactly two
quantization steps high; positives were unaffected.

Round-tripping 200k values through Quaternion32:

  before: worst error 0.00345941 on negatives, 0.00069195 on positives
  after:  worst error 0.00069195 on both (== half a step, the optimum)

Small magnitudes could even flip sign: -0.001 decoded as +0.00138.

Quaternion64Compression.ScaleToFloat_H/_L carried the same off-by-two.

The encoder is untouched, so this is wire-compatible in both directions;
only the decode of already-negative components changes.
@FirstGearGames

Copy link
Copy Markdown
Owner

Issue confirmed and patch applied!

@FirstGearGames
FirstGearGames merged commit 73f30cf into FirstGearGames:main Aug 21, 2026
FirstGearGames pushed a commit that referenced this pull request Sep 2, 2026
- Fixed ZigZagDecode improperly decoding values in IL2CPP with specific backing CPP libraries.
- Fixed Scene object expecting prefab ID (#1032).
- Fixed eliminated or significantly reduced wobble in local reconcile resimulations on server-authoritative objects.
- Fixed NetworkAnimator delay/desync on host owned object, observed by clients (#1062).
- Fixed NetworkCollider/2D tick comparison error (#1041).
- Fixed prefab objects generator cannot include folders with spaces in the path (#1045).
- Fixed large writers pooling incorrectly, causing them to not be re-used (#1054).
- Fixed LiteNetLib wrong data length after BasePacketLayer modification (#1064).
- Fixed NetworkTrigger2D by uncommenting entire file.
- Fixed nested NetworkObjects not spawning recursively when spawned via OnStartServer of their parent.
- Fixed SyncType values being reset to default before OnStopServer as clientHost (#1036).
- Fixed disconnecting clients not being cleared on Transport shutdown (#1044).
- Fixed Quaternion ScaleToFloat helper math, cause of marginal rotational errors (#1067).
- Fixed loading scenes by path not loading the scene for clients under certain conditions (#1060).
- Obsoleted SceneLookupData.Name in favor of SceneLookupData.FullName.
- Added NetworkBehaviour.ToString null safety (#1046).
- Added Unity 6.5 scene handle change support (#1052 / #1051 merge).
- Added Unity 6.5 GetEntityId/GetInstanceId support.
- Added NetworkCollider2D use PhysicsScene2D.OverlapCollider as a fallback (#1042).
- Added NetworkCollider/2D.RemoveEnteredCollider.
- Added NetworkCollider/2D InvokeStayOnEnter.
- Added NetworkAnimator channel option - under Beta menu, Animator Channel (#1039).
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants