Skip to content

physics: cvc::physics facade with null and Jolt backends; state-tree binding (vehicle physics P1) - #641

Merged
transfix merged 3 commits into
masterfrom
feat/physics-p1-jolt
Oct 17, 2026
Merged

transfix merged 3 commits into
masterfrom
feat/physics-p1-jolt

Conversation

@transfix

Copy link
Copy Markdown
Owner

Phase 1 of docs/roadmap/NAV-VEHICLE-PHYSICS-ROADMAP.md: an engine-agnostic rigid-body world with a Jolt backend. The API reference is docs/PHYSICS_API.md.

What

cvc::physics::world (inc/cvc/physics/, std-only headers, no engine named in any of them)

  • Shapes: sphere, box, convex hull, static triangle mesh, and a height field with per-cell materials. Rows run along +y; NaN marks a hole.
  • Stepping and contacts: fixed-quantum step() / follow(world_clock). Contact events are sorted, so worker threads never reorder them. Raycasts.
  • Determinism: save() / restore() / state_hash() over the whole simulation state. A determinism mode the build cannot deliver is refused.
  • Floating origin: positions are doubles relative to an origin; rebase() moves it. City-scale coordinates keep sub-mm resolution.
  • Runtime changes: reconfigure() changes runtime settings, and refuses build-time ones by name. set_material() makes live material changes, e.g. rain.
  • Thread-safe: every call locks the world, and step() holds the lock for the engine step.

Backends

  • null: bookkeeping only, for builds without an engine.
  • jolt: its own target, cvc_physics_jolt, with Jolt::Jolt linked PRIVATE, so JPH_* definitions reach one TU.
    • Linkage: a static archive inside a shared cvc, or an OBJECT library inside a static cvc, where Jolt joins CVC_PRIVATE_LINK_PACKAGES. This is the same shape as cvc_igl.
    • CVC_WITH_JOLT is private and set from find_package(Jolt CONFIG).
    • Z up: the height field's body is turned by the exact float quaternion (0.5, 0.5, 0.5, 0.5). After Jolt compresses the heights, every sample is checked against max_error.
    • One μ table: every shape and cell carries a cvc material, and friction/restitution are combined from it (product by default, so the neutral material 0 takes the terrain's μ).
    • Exceptions: nothing throws across a Jolt callback.
    • Saved state: goes through a recorder whose bytes live in this TU, so there is no C++ runtime layout dependency on libJolt.
    • No singletons: Jolt's process globals (factory, type registry, allocator hooks) are reference-counted by the worlds that use them. An application's own Jolt setup is respected.

world_binding — configuration from the state tree

  • Every configurable setting lives under physics.*, and unset keys are seeded with the defaults:
    • build-time: backend, fixed_dt, threads, determinism, buffer sizes, origin;
    • runtime: gravity, solver steps, slop, combine rules, contact reporting;
    • the material table: materials.<name>.*.
  • pull() applies runtime keys and materials; a changed build-time key sets restart_required.
  • push() publishes telemetry: tick, time, step_ms, bodies, contacts, version, and optionally state_hash.
  • One binding per app and world; thread-safe.

Also

  • The libcvc recipe declares joltphysics for the platforms it is published on.
  • cvc::physics_error.
  • The new header in the header-hygiene test.

The P1 gates (physics_world_test)

  1. A box rests on rolling terrain at the sampled height ±1 cm, and raycasts agree with the raster within 5 mm.
  2. A contact reports the cell's material and the μ the solver used. On a 20° slope (tan = 0.364), μ 0.9 sticks and μ 0.2 slides.
  3. The same trace twice gives the same hash.
  4. 1 thread and 3 give the same hash.
  5. Save → restore → re-run gives the same hash, both in the same world and in a fresh world built the same way.

Plus:

  • concurrent calls from four threads (stepping, reading, pushing forces and adding bodies, tree changes);
  • the floating origin at UTM scale;
  • tree-driven gravity (lunar fall) and materials;
  • two apps configuring two worlds independently;
  • worlds created and destroyed repeatedly with no process-wide setup left behind.

Verification (Windows, MSVC 2022, Jolt 5.6.0+cvc.5 from the local cvcpkg prefix)

  • physics_world_test: 30/30 pass. Two skip by design in a Jolt build: "refused when not built" and "refused when not cross-platform deterministic". It passed 3 repeated runs.
  • Busy-scene hash after 300 steps: d3ebde22539c1c2f. Printed, not yet asserted. A cross-platform golden can be added once CI shows the same value on Linux and macOS.
  • system_macro_headers_test, world_units_test and world_clock_test pass.
  • Full ctest: three known failures, all ImageMagick's coder path on this box, plus two CLI subprocess tests that flake under -j 12 and pass alone.

Related: #640 makes world_clock_binding thread-safe. It is independent of this PR.

Not in this PR: the vehicle (P2), the energy ledger (P3), the nav plant seam (P4) and the pycvc wrappers (P6).

…binding (vehicle physics P1)

An engine-agnostic rigid-body world for the nav vehicle-physics work
(docs/roadmap/NAV-VEHICLE-PHYSICS-ROADMAP.md, docs/PHYSICS_API.md).

- inc/cvc/physics (std-only headers, no engine named anywhere):
  - value types and materials;
  - shapes: sphere, box, convex hull, static triangle mesh, and a height field
    with per-cell materials;
  - bodies; backends with capability bits; the world.
- world:
  - steps in fixed quanta, or follows a world_clock;
  - reports contacts sorted so worker threads never reorder them;
  - raycasts;
  - saves, restores and hashes its whole state;
  - keeps a double-precision floating origin with rebase;
  - reconfigure() for the runtime settings and set_material() for live
    material changes.
- Behaviour:
  - It refuses a determinism mode the build cannot deliver.
  - It is thread-safe: every call locks the world, and step() holds the lock
    for the engine step.
- null backend: bookkeeping without physics, for builds without an engine.
- Jolt backend (src/cvc/physics/jolt), built as its own target, cvc_physics_jolt:
  - Jolt::Jolt is linked PRIVATE, so JPH_* definitions reach that one TU.
  - CVC_WITH_JOLT is a private define, set from find_package(Jolt CONFIG).
  - The target is a static archive in a shared cvc, or an OBJECT library in a
    static cvc (where Jolt joins CVC_PRIVATE_LINK_PACKAGES).
  - Z up: the height field's body is turned by the exact quaternion
    (0.5, 0.5, 0.5, 0.5). Every height sample is checked against max_error
    after Jolt compresses it.
  - Friction and restitution come from cvc materials, with a choice of combine
    rules.
  - Nothing throws across a Jolt callback.
  - Saved state uses a recorder whose bytes live in this TU, not Jolt's
    stringstream recorder, so there is no C++ runtime layout dependency on
    libJolt.
  - Jolt's process globals are reference-counted by the worlds using them.
    There is no singleton, and an application's own Jolt setup is respected.
- world_binding: configuration and materials from the app's state tree under
  "physics.*" (knob seeding, invalid values ignored), runtime keys applied on
  pull(), telemetry published on push(), and restart_required set when a
  build-time key changes. One per app and world; thread-safe.
- The libcvc recipe declares joltphysics for the platforms it is published on.

Tests: physics_world_test, 30 tests (2 skip by design in a Jolt build). They
include the P1 gates:
1. a box rests on rolling terrain at the sampled height +-1 cm;
2. contacts report the cell's material and mu, and mu decides stick (0.9) or
   slide (0.2) on a 20-degree slope;
3. the same trace twice gives the same hash;
4. 1 thread and 3 give the same hash;
5. save, restore and re-run give the same hash, in the same world and in a
   fresh one.
Also tested: concurrent calls from four threads, the floating origin at UTM
scale, and tree-driven gravity and materials. Busy-scene hash after 300 steps
on Windows/MSVC: d3ebde22539c1c2f.
Linux and macOS failed to link: "undefined reference to typeinfo for
JPH::PhysicsMaterial".
- Cause: Jolt is built with -fno-rtti, and the backend subclasses Jolt's
  polymorphic classes (PhysicsMaterial, ContactListener, the layer filters,
  StateRecorder). With RTTI on, the subclasses' typeinfo references base
  typeinfo that libJolt never emitted. MSVC places its RTTI records in the
  referencing TU, so Windows linked.
- Fix: compile cvc_physics_jolt with -fno-rtti, matching Jolt. Exceptions
  still work without RTTI.
- Also: give world_backend an out-of-line destructor in world.cpp, so the
  interface's vtable and typeinfo are emitted in an RTTI TU and never in the
  backend's.
Building the whole Jolt backend with -fno-rtti made GCC/Clang emit weak
copies of the typeinfo for every std exception it throws
(invalid_argument, runtime_error, length_error, bad_alloc). On macOS
those copies bind every reference inside libcvc.dylib, so exceptions
thrown by libcvc stopped matching catch clauses in the tests and
boost::any casts failed: 1029 of 3888 tests on package-macos.

Only cvc_material needs it: JPH::PhysicsMaterial's key function is in
libJolt (built without RTTI), so a cvc_material vtable emitted with RTTI
references a typeinfo that exists nowhere. cvc_material now has an
out-of-line destructor in jolt_material.cpp, the one file built with
-fno-rtti; it throws and catches nothing. jolt_backend.cpp is back on
RTTI. Jolt's other bases here have only inline virtuals.
@transfix
transfix merged commit 5fc7477 into master Oct 17, 2026
15 checks passed
@transfix
transfix deleted the feat/physics-p1-jolt branch October 17, 2026 14:59
transfix added a commit that referenced this pull request Oct 17, 2026
Minor release. It carries:
- the world clock: lockstep and batch, followers, the sim.* binding, and nav
  on the clock (#629, #630, #631, #640);
- the units registry with SI <-> normalized-frame conversions (#632);
- cvc::physics with the Jolt backend (#641);
- the material trainer on the kinematic bicycle and its pycvc bindings
  (#642).
Bumps project(VERSION) 3.5.1 -> 3.6.0 and every cvcpkg recipe's
upstream_version to match. cvc_revision stays 1, per the recipe-version gate.
transfix added a commit that referenced this pull request Nov 27, 2026
libcvc builds on Haiku with what the catalog has there: no gRPC chain
(abseil, protobuf, re2, grpc), so build.sh turns both conference
transports off on Haiku (the WebSocket one needs protobuf's wire too);
no openblas, so no levmar; and no fontconfig, tvision or openmp. Its
other runtime deps now cover haiku (boost, imagemagick, libigl, libiimod
and joltphysics through cy-pca/cvcpkg#644, libsodium through #641).

ari gets a haiku row; arisrv does not, since it needs gRPC.
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