Skip to content

dyn_bike_plant: the contact's damper and friction implicit; contact substeps for fast impacts - #1004

Merged
transfix merged 4 commits into
masterfrom
fix/dyn-contact-implicit
Nov 6, 2026
Merged

transfix merged 4 commits into
masterfrom
fix/dyn-contact-implicit

Conversation

@transfix

@transfix transfix commented Nov 3, 2026 •

Copy link
Copy Markdown
Owner

The compliant contact from #983 overshoots fast impacts at the live plant's default 0.01 s substep. This PR fixes that in dyn_bike_plant and dyn_contact.h.

The problem

pycvc's navigator drove the full-size preset (vehicle_desc::humvee()) at 7.8 m/s into a wall that the plant reads from sim_world's field. The scene is make_sim's world from test_pycvc_physics.py, with a wall at x = 0..1 m taken from a second sim_world. At 0.01 s:

  • the plant reversed the vehicle within one tick, to −3.24 m/s (the reverse clamp, −0.25 × max speed);
  • contact_dissipated was −1.52e5 J, so the damper supplied energy;
  • the speed clamp took 2.42e5 J (limiter);
  • the books' residual was 3.1e-2 of the flow.

At 0.005 s and finer, none of this happens.

Cause 1: the damper is explicit. The Hunt-Crossley damper and the friction were held from the substep's start. Their coefficients grow with the penetration (c_k = c (s − d)/s). The friction's secant near zero slip is μ k (s − d)/slide. So h·R_c/m passes 2 at depth, and −R_c·v₀ flips the approach within a step.

Cause 2: nav's grid SDF is steep at a face. φ goes from +1 cell to −1 cell across the cell at an obstacle's face (its distances run between cell centres), so |∇φ| = 2 there. Against sim_world's field, the barrier is therefore 4× and the damper 8× as stiff per metre of approach as against an exact distance. The existing tests used an exact wall at 5 m/s and never got there.

The fix

1. Implicit damper and friction (diff::dyn_contact_implicit).

  • The damper and the friction are read at the twist the step reaches under the bodies' inertia, the barrier's push and themselves. This is implicit Euler with the barrier explicit: (M + h R_c) v_d = M v₀ + h W_c, and W_d = −R_c v_d, with M = diag(m, m, I_z).
  • The friction's secants are taken again at the slide v_d reaches (4 solves). With the start's secant alone, a slide the step slows gets less than Coulomb's μ k (s − d). Re-taking it is a majorize-minimize step on the friction's dissipation potential.
  • Vehicles touching each other are solved together: one small Cholesky system per group, 3 unknowns per body. So W_d stays equal and opposite at each point, and a pair keeps its momentum.
  • dyn_contact_field and dyn_contact_pair hand back each touching point's normal and tangent rows (dyn_contact_row) on request. Their outputs are unchanged.
  • The books are unchanged: W_d is held over the step, and its line is its own work.

2. Contact substeps for fast impacts (dyn_bike_plant_options::contact_step_depth, 0.1 m).

  • The barrier stays explicit. A point that enters the skin within a step meets no force at the step's start, and its energy at the end lands in the residual.
  • So a substep in which any point would close more than 0.1 m on what it meets is cut into equal pieces (dyn_contact_closing and dyn_contact_pair_closing).
  • The piece count holds until nothing touches or closes, so a whole bounce runs at one step. My first try split only while a point closed; that left the rebound at the coarse step, and the compression and rebound errors stopped cancelling. The books came out worse (2.07e-2 → 1.07e-2 instead of 1.2e-3).
  • At the default depth, none of the existing 5 m/s cases split. A Humvee into sim_world's field takes 2 pieces from 7.8 m/s and 3 at 12 m/s.
  • <= 0 turns it off.

The contact lines' port work now uses the step's own hdt (float), as dyn_plant_books' port does. Otherwise the books' identity would miss by float(h/2)'s error in a piece.

Numbers (physics_dyn_contact_test, Windows MSVC 19.44 Release)

pycvc's navigator into sim_world's wall (the probe above, written in C++):

substep master: contact_dissipated master: limiter master: slowest vx master: |Σres|/flow this PR: contact_dissipated this PR: limiter this PR: slowest vx this PR: |Σres|/flow
0.01 −1.52e5 J −2.42e5 J −3.24 3.06e-2 8.16e4 J 0 −0.47 1.33e-3
0.005 8.16e4 0 −0.48 2.36e-3 8.15e4 0 −0.47 2.37e-3
0.0025 8.18e4 0 −0.47 1.28e-3 8.17e4 0 −0.47 1.28e-3
0.00125 8.19e4 0 −0.47 2.58e-4 8.19e4 0 −0.47 2.58e-4

From −30 m (8.0 m/s at impact), at 0.01 s:

contact_dissipated limiter slowest vx |Σres|/flow
master −9.6e4 J −1.8e5 J −4.63 2.35e-2
this PR 8.42e4 J 0 −0.47 3.85e-3
reference (0.00125 s) 8.50e4 J −0.48

Driven raw into sim_world's field, head on, at 0.01 s (the 0.00125 s result in brackets):

impact master: rebound master: contact_dissipated master: limiter this PR: rebound this PR: contact_dissipated this PR: |Σres|/flow
8.45 m/s 0.710 [0.050] −1.15e4 J −5.7e4 J 0.050 9.43e4 J [9.51e4] 4.1e-3
10.4 m/s 0.098 [0.039] 1.38e5 J 0 0.040 1.45e5 J [1.46e5] 7.9e-4
12.1 m/s 0.007 [−0.018] −9.07e5 J −1.15e6 J −0.018 2.00e5 J [2.01e5] 1.7e-3

The existing 5 m/s gates (TheBooksCloseToSecondOrder), master → this PR:

  • head on: residual per step 3.23 / 0.70 / 0.16 J → 3.45 / 0.73 / 0.17 J at 0.01 / 0.005 / 0.0025 s; |Σ|/flow 4.0e-3 → 4.0e-3.
  • 30°: 1.10 / 0.27 / 0.064 J → 1.18 / 0.29 / 0.067 J; |Σ|/flow 3.0e-3 → 2.8e-3.
  • parked Humvee: exchange 237 / 85 / 20 J → 242 / 84 / 20 J; residual 2.5e-5 → 1.7e-5 of the flow.

ComparesWithJoltOnImpacts (5 m/s, 0.01 s):

  • Head on (0.89 m/s after, rebound 0.19) and the parked Humvee (1.90 / 2.88 m/s) are unchanged.
  • 30° at friction 0.2: 0.91 m/s, 0.19, +0.4° → 0.90 m/s, 0.19, +0.3°.
  • 30° at friction 0.6: 0.68 m/s, 0.14, −0.1° → 0.72 m/s, 0.15, −0.5°.
  • On that last slide, v after at 0.01 / 0.005 / 0.0025 / 0.00125 s is 0.675 / 0.686 / 0.690 / 0.693 on master and 0.716 / 0.687 / 0.683 / 0.688 here. Both converge to the same value. This PR's error at 0.01 s is about master's, without the explicit scheme's overshoot.

Tests

  • New DynContact.TheImplicitDamperOnlyDissipates: 846 wall poses and 638 pair poses, at h = 0.01, 0.1 and 1 s, with and without the barrier.
    • The rows' R_c·v equals −Wd to 7.9e-16.
    • The solve M(v_d − v) = h(W_c + W_d) holds to a componentwise backward error of 3.4e-16 (gated at 1e-12; cf73786, after the first gate, scaled by |W_d|, read 1.26e-9 on Linux).
    • W_d·v_d ≤ 0.
    • The pair's forces and moments balance to 1.8e-15.
    • Without the barrier, the kinetic energy at v_d never exceeds that at v.
  • New DynContactPlant.AFastImpactAtTheDefaultSubstep: the navigator case from −20 m and −30 m; raw drives into sim_world's field at 8.3, 10.3 and 12.3 m/s; a 30° impact on an exact wall; a parked Humvee at 10.3 m/s.
    • At 0.01 s each case must have: contact_dissipated > 0, the clamp taking no more than at 0.00125 s, |Σres|/flow < 5e-3, the identity to 1e-8, and the rebound or slowest speed within 0.02 or 0.05 m/s of the 0.00125 s run.
    • It fails on master: every navigator and sim_world-field case misses one or more of these.
  • Existing gates: physics_dyn_contact (all 9 tests), physics_dyn_bike_plant (10), physics_plant_log (15), nav_dyn_spin (13), physics_plant_host (5) and nav_sim_world (3) pass.
  • Goldens: DynContact.Golden regenerates byte-identical (git hash-object --no-filters of the rewritten file is HEAD's blob a8d49445). The contact functions' outputs and dyn_plant_books are untouched, so GRL-SNAM's torch twin fixtures stay valid.
  • clang-format 18 is clean on the changed lines.

Cost

  • While anything touches, each step runs up to 4 small SPD solves: 3×3 per body, 6×6 per touching pair.
  • With contact_step_depth, a fast or steep contact steps every agent in pieces (1 to 3 in the cases above) until nothing touches or closes. A vehicle left resting against what it hit keeps them. This is documented on the option.

Not in this PR

  • nav's grid SDF itself: |∇φ| = 2 in the cell at a face, and flat at −1 cell inside a two-cell wall. The plant now copes with it, but a vehicle that got past half a cell into a thin wall would feel no barrier. A field fix would change navigation, so it isn't attempted here.
  • pycvc: dyn_bike_plant's wheel spin, side slope, compliant contact and ground #1002 (the pycvc binding of the plant options) doesn't expose contact_step_depth. The default applies there.

…ubsteps for fast impacts

The compliant contact (#983) held its Hunt-Crossley damper and friction from the
substep's start. Their coefficients grow with the penetration, so in a fast impact
hdt R_c / m passes 2 at the default 0.01 s substep: pycvc's navigator driving the
full-size preset into a wall in sim_world's field at 7.8 m/s reversed the vehicle
within a tick to -3.24 m/s (the reverse clamp), the damper supplied 1.5e5 J
(contact_dissipated < 0), the speed clamp took 2.4e5 J and the residual was 3.1e-2
of the flow. nav's grid SDF makes it worse: phi runs from +1 cell to -1 cell across
the cell at a face, so there the barrier is 4x and the damper 8x as stiff per metre
of approach as against an exact distance.

The damper and the friction are now read implicitly (dyn_contact_implicit), at the
twist the step reaches under the bodies' inertia, the barrier's push and themselves:
(M + hdt R_c) v_d = M v0 + hdt W_c, W_d = -R_c v_d, with the friction's secants
taken again at the slide v_d reaches. The bodies touching each other are solved
together, so W_d stays equal and opposite between vehicles. The contact functions
report each touching point's damped directions (dyn_contact_row) on request; their
outputs are unchanged.

The barrier stays explicit, so a step that carries a point far into it leaves that
energy in the residual. dyn_bike_plant_options::contact_step_depth (0.1 m) cuts a
substep in which a point would close further into equal pieces, held until nothing
touches or closes, so a bounce runs at one step (finer steps for the approach alone
leave the rebound's errors uncancelled). At the default it splits none of the
existing 5 m/s cases. The contact lines' port work uses the step's own hdt, as
dyn_plant_books' port does, so the books' identity holds in every piece.

Measured (physics_dyn_contact_test, MSVC 19.44 Release): the navigator case at
0.01 s now dissipates 8.16e4 J, limiter 0, slowest -0.47 m/s, residual 1.3e-3 of
the flow (at 0.00125 s: 8.19e4 J, -0.47 m/s). New tests
DynContact.TheImplicitDamperOnlyDissipates and
DynContactPlant.AFastImpactAtTheDefaultSubstep (fails on master); the existing
gates pass and DynContact.Golden regenerates byte-identical.
DynContact.TheImplicitDamperOnlyDissipates checked M (v_d - v) = hdt (W_c + W_d)
against M |v| + hdt (|W_c| + |W_d|). Stiff dampers (hdt to 1 s) leave v_d nearly
across their directions, so W_d = -R_c v_d is far smaller than the terms the solve
summed, and the check read the solve's rounding as 4.9e-10 on Windows and 1.26e-9
on Linux (CI failed it at 1e-9). It now divides by |A| |v_d| + |b|, R_c's entries
bounded by each row's largest coefficient (a friction secant is at most coulomb /
slide): 3.4e-16, gated at 1e-12.
@transfix
transfix enabled auto-merge (squash) November 6, 2026 20:44
@transfix
transfix merged commit ce9aeba into master Nov 6, 2026
16 of 27 checks passed
@transfix
transfix deleted the fix/dyn-contact-implicit branch November 6, 2026 21:31
transfix added a commit that referenced this pull request Nov 7, 2026
…efault substep (#1038)

pycvc.dyn_bike_plant(..., contact_step_depth=None) binds #1004's
dyn_bike_plant_options::contact_step_depth (m; None keeps the plant's default,
<= 0 never substeps), and options() reports it.

test_pycvc_physics.py's contact case, the navigator driving into a wall from
another sim_world's map at about 7.8 m/s, goes back to the default 0.01 s
substep: #1002 ran it at 0.005 s because the damper overshot, and #1004's
implicit damper and contact substeps fixed that. It now also checks the speed
clamp takes nothing, and the new keyword's round trip, default and type.

All 77 checks pass (Windows, MSVC 19.44 Release, Python 3.11, on master
f94a9b2). At 0.01 s the contact dissipates 8.15e4 J, the limiter takes 0 and
the residual is 1.33e-3 of the flow, as #1004's C++ fast-impact test has it.
transfix added a commit that referenced this pull request Nov 7, 2026
Bumps the project version and every recipe's upstream_version from 3.7.11 to 3.7.12. The v3.7.12 tag on this commit is the first release with the multi-car controller template (pycvc_gl.cars, #1031 #1035 #1040), the hidden-subtree fix in cvcGL (#1037), the discrete-gradient dyn_bike step (#978), semi-implicit Euler by default in the nav rollout (#981), the implicit contact damper (#1004, #1038), and generated_world's roads and water (#1032, #1034).
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