Skip to content

physics: a native trace replayed in wasm (P7) - #698

Merged
transfix merged 4 commits into
masterfrom
feat/physics-wasm-replay
Oct 19, 2026
Merged

transfix merged 4 commits into
masterfrom
feat/physics-wasm-replay

Conversation

@transfix

Copy link
Copy Markdown
Owner

P7 of the vehicle-physics plan: one physics trace gives the same state_hash at every step natively and in wasm-mt under node.

What it adds

  • physics_replay_test (plain main(), since googletest has no wasm build) replays src/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 is 70b2cc0bd34ea4be.
    • 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 that save()/restore() reproduces its second half.
  • It runs natively under ctest on every recipe platform.
  • CI job 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. Like cvcgl-wasm-js, it runs only when physics or its build changed.
  • publish-cvcgl-wasm.yml: installs joltphysics, 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 if cvcConfig.cmake doesn't name Jolt.

Two determinism leaks it found

  1. Uninitialized wheel state in the hash. Jolt leaves a wheel's contact position, normal, lateral direction and suspension length uninitialized until the wheel first lands. VehicleConstraint::SaveState writes 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.
    • Fix: the Ackermann controller overrides ConstructWheel with defined_wheel, which defines those fields.
  2. libm. With (1) fixed, the vehicle scenes still left the native trace in wasm, at step 8 (RoboRacer) and step 12 (Humvee). The cause: std::tan/atan/tanh/expm1/pow are not correctly rounded, and MSVC's and musl's differ.
    • Fix: everything that feeds the hash now calls the new 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 and nav_plant's spawn rotation and pose.
    • Accuracy: within 1–4 ULP of MSVC's libm (measured, no margin).
    • physics_det_math_test pins 14 output bit patterns; a wasm build of det_math.cpp gives the same 14.
    • Those sources are built with -ffp-contract=off / /fp:precise.

Verified locally (Windows MSVC 19.4x, emscripten 5.0.7 + node 22.16)

  • Native replay: all 1240 steps match. wasm-mt under node: all 1240 match.
  • Before the libm fix, wasm parted at steps 8 and 12. Before the wheel fix, two native runs parted at step 1.
  • ctest -R "Physics|Plant|physics_replay": 121/121 pass. The 2 skips are the build-conditional ones.
  • git-clang-format 18 on the changed lines is clean.
  • Not run locally: macOS arm64 and Linux. The recipe jobs run physics_replay_test there. 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.

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.
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.
A fast runner built the fixed 1536 x 1024 pyramid in 17 ms, under the
20 ms the check needs to judge, and failed pycvc-linux on #698. The side
now doubles until one untimed build takes 0.15 s (at most 4096 x 6144),
as the store check's rungs do (c943120).
@transfix
transfix merged commit cba0f50 into master Oct 19, 2026
17 checks passed
@transfix
transfix deleted the feat/physics-wasm-replay branch October 19, 2026 01:29
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:.
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.

1 participant