fix(serializing): correct two's-complement unwrap in quaternion decompression - #1067
Open
ddelgado95 wants to merge 1 commit into
Open
fix(serializing): correct two's-complement unwrap in quaternion decompression#1067ddelgado95 wants to merge 1 commit into
ddelgado95 wants to merge 1 commit into
Conversation
…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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
ScaleToUintencodes each smaller quaternion component as a two's-complement value masked toBitsPerAxisbits, butScaleToFloatunwraps negatives by subtractingMaximum * 2— which is2 * IntScalesteps, not theIntMask + 1steps 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):-511steps encodes as(uint)(-511) & 1023=513513 - 1024=-511✅513 * M/511 - 2M=M * (513 - 1022)/511=-509❌Quaternion64Compression.ScaleToFloat_H/_Lcarry the same off-by-two.Evidence
Round-tripping 200,001 values across
[-Maximum, +Maximum]throughScaleToUintand back:0.003459410.000691950.000691950.000691950.00069195is 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:
Impact
Quaternion32 is the default
AutoPackType.Packedpath, used byRigidbodyStatein every prediction reconcile and byNetworkTransformrotation sync. The constant+0.00277bias 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.