fix: Fix flaky Transform2D random round-trip test tolerance#3917
Merged
Conversation
Replace the incorrect statistical error propagation formula with a derivation based on the actual floating-point error source. Both Vector2 and Matrix4 use Float32List storage, so the intermediate global Vector2 returned by localToGlobal is rounded to float32 precision. That rounding error (~epsilon * globalMagnitude) is then amplified by the inverse transform factor matrixNorm / |det|. The old formula used only the local point magnitude as a proxy, which fails when scale is tiny: with scale ~0.001, globalMagnitude is dominated by translation (~7.4 vs local ~1) and matrixNorm / |det| reaches ~949, making the actual error ~12x larger than the tolerance.
ufrshubham
approved these changes
May 10, 2026
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.
Description
The `Transform2D random` test was flaky because the tolerance formula in
`transform2dRoundTripUncertainty` used only the local point magnitude, which is wrong
when scale is tiny.
Both `Vector2` and `Matrix4` use `Float32List` storage. The dominant error in
`globalToLocal(localToGlobal(point))` is float32 rounding of the intermediate `Vector2`
returned by `localToGlobal`. That rounding error (`~epsilon * globalMagnitude`) is then
amplified by the inverse transform's factor `matrixNorm / |det|`.
With seed `3633814569`, iteration 0 has scale ~0.001, so `matrixNorm / |det|` ≈ 949 and
the global magnitude is dominated by translation (~7.4) rather than the local point (~0.5).
The old formula produced a tolerance of 9.2e-6 while the actual error was 1.12e-4.
The fix computes the actual intermediate global magnitude and uses it directly:
```
tolerance = 3 * epsilon * globalMagnitude * matrixNorm / |det| + epsilon
```
To reproduce the failure before this fix:
```
flutter test packages/flame/test/game/transform2d_test.dart
--dart-define=RANDOM_SEED=3633814569
```
Checklist
Breaking Change?
Related Issues