Repository navigation
physics: a native trace replayed in wasm (P7) - #698
Merged
Merged
Conversation
physics_replay_test replays a trace recorded natively, the state_hash after every one of 1240 steps in three scenes: the golden scene, a Humvee through the driver model across a rebase, and a RoboRacer on raw pedals over two terrain materials, through mud that keeps ruts, past a crate floating in water. It runs under ctest natively and, built with CVC_BUILD_PHYSICS_REPLAY in a minimal wasm-mt libcvc, under node in CI's new physics-wasm job. Locally, MSVC x86_64 and emscripten agree at every step. Two determinism leaks it found, both fixed: - Jolt leaves a wheel's contact position, normal, lateral direction and suspension length uninitialized until the wheel first lands, but VehicleConstraint::SaveState writes them every step. Every vehicle hashed heap garbage for its first steps; two identical runs in one process disagreed. The Ackermann controller now constructs its wheels (defined_wheel) with those fields defined. - libm's transcendentals are not correctly rounded and differ by an ULP between MSVC, glibc, Apple and musl, so the vehicle scenes parted from the native trace in wasm at steps 8 and 12. The vehicle step, the fields, Bekker sinkage and nav_plant now call det_math.h: sin, cos, tan, atan, atan2, asin, tanh, expm1 and cbrt from correctly rounded operations only, within a few ULP (physics_det_math_test, which also pins their bits; the same bits in a wasm build). Those sources are built without FMA contraction. The wasm-mt publish installs joltphysics (the recipe already declared it for wasm-mt) and fails if the build loses Jolt.
transfix
enabled auto-merge (squash)
October 18, 2026 22:42
transfix
disabled auto-merge
October 18, 2026 22:42
glibc's tanh near 0 and cbrt are an ULP further from det_math than MSVC's (3 against 2), which failed the Linux recipe jobs. Each bound is now the most measured against MSVC's and glibc's libm plus one ULP for the next libm's. The pinned bits and physics_replay_test passed on Linux.
transfix
added a commit
to cy-pca/cvcpkg
that referenced
this pull request
Oct 19, 2026
…vc.6) (#364) JPH::Wheel leaves its contact position, point velocity, normal, longitudinal and lateral directions and axle-plane constant uninitialized until the wheel first touches the ground, but VehicleConstraint::SaveState writes the position, normal and lateral direction for every wheel on every call. A vehicle saved before its wheels landed (every vehicle, for its first steps) wrote heap garbage into its state, so identical runs saved and hashed different bytes. libcvc's physics replay test found it (transfix/libcvc#698). patches/wheel-contact-init.patch gives those members zero initializers. That matches what libcvc's ConstructWheel workaround sets, so a consumer's state is the same with or without the patch. The smoke test now builds one vehicle twice, each on a heap scribbled with a different byte, and requires identical saved states. On Windows (MSVC) the published cvc.5 fails it and the patched build passes, with the same JPH_VERSION_ID and sphere-scene hash (9dba41244979cf92). The issue and the workaround are recorded in the recipe's notes:.
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.
P7 of the vehicle-physics plan: one physics trace gives the same
state_hashat every step natively and in wasm-mt under node.What it adds
physics_replay_test(plainmain(), since googletest has no wasm build) replayssrc/cvc/tests/data/physics_replay_trace.json. The trace holds the hash after each of 1240 steps across three scenes:exact: the cross-platform golden scene. Its last step is70b2cc0bd34ea4be.humvee: through the driver model (steering actuator, Ackermann, engine, gearbox, energy ledger), with a rebase of the floating origin halfway.roboracer: raw pedals (throttle, brake, hand brake, reverse) over terrain of two cell materials, through mud that keeps ruts (Bekker sinkage), past a crate floating in water. Each run also checks thatsave()/restore()reproduces its second half.physics-wasm: builds a minimal wasm-mt libcvc (CVC_BUILD_PHYSICS_REPLAY=ON, no cvcGL or codecs; deps from cvcpkg) and runs the replay under the emsdk's node. Likecvcgl-wasm-js, it runs only when physics or its build changed.publish-cvcgl-wasm.yml: installsjoltphysics, which the recipe already declared for wasm-mt but the build never installed, so the published wasm libcvc had only the null backend. The job now fails ifcvcConfig.cmakedoesn't name Jolt.Two determinism leaks it found
VehicleConstraint::SaveStatewrites them every step anyway. So every vehicle hashed heap garbage for its first steps, and two identical runs in one process disagreed for steps 1–8.ConstructWheelwithdefined_wheel, which defines those fields.std::tan/atan/tanh/expm1/poware not correctly rounded, and MSVC's and musl's differ.src/cvc/physics/det_math.h: sin, cos, tan, atan, atan2, asin, tanh, expm1 and cbrt, built from correctly rounded operations only. The callers are the vehicle step, the fields' drag, Bekker sinkage andnav_plant's spawn rotation and pose.physics_det_math_testpins 14 output bit patterns; a wasm build ofdet_math.cppgives the same 14.-ffp-contract=off//fp:precise.Verified locally (Windows MSVC 19.4x, emscripten 5.0.7 + node 22.16)
ctest -R "Physics|Plant|physics_replay": 121/121 pass. The 2 skips are the build-conditional ones.git-clang-format18 on the changed lines is clean.physics_replay_testthere. A mismatch on either is a leak to fix, not a trace to re-record.Hash changes
Vehicle and field results move by ULPs, so any vehicle's state hash changes. The golden scene's hash is unchanged. The pycvc parity values are regenerated from C++ at test time, so they follow.