Replace snapshots with hash-based cross-platform determinism test #555
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.
Objective
Currently, Avian has a cross-platform determinism test that produces insta snapshots, which are compared between platforms in CI with GitHub Actions.
However, these snapshots are quite large, and always pollute diffs in PRs (such as this one) whenever changes affecting simulation behavior are made. Insta must also be installed to generate these snapshots, making it annoying for new contributors to deal with. Overall, it's a hassle.
The test itself is also very simplistic and only tests collisions.
Instead of using snapshots, we should just run the simulation for a while, and compute a single transform hash based on the position and rotation of all bodies. This is much simpler and more convenient.
Solution
Add a 2D test for cross-platform determinism, simulating pairs of objects constrained via revolute joints falling to the ground. After 500 steps, a transform hash is computed and compared against the expected value. If they don't match, the test will fail with a message indicating that the expected hash should be updated if the change in behavior was intended.
This test is based on Box2D's
FallingHinges
test and sample.There is also a new
determinism_2d
example, which is a visual representation of the test.2024-11-11.20-21-34.mp4
While making this, I stumbled upon some bugs as well:
PhysicsSchedule
runs,Time<Substeps>
is only set once the substepping loop starts. Earlier in the schedule, the previous value is used. However, some systems likeupdate_contact_softness
might need the substep time before that. Using the previous value can lead to seemingly indeterministic results e.g. when resetting scenes.I have fixed both of these issues.