Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -464,6 +464,8 @@ Integer only, with no libc. Host-tested (no console run). See
scale (`SH2Z_VD_FORCE_FRAC`) that carries the engine force with more fraction bits (default 16: unchanged code).
`vehicle-dynamics-6` is a build-time stop (`SH2Z_VD_LOCATE_FAR`) for a closest-point search that circles when the
body is far off the line, as over a gap (default 0: unchanged code).
`vehicle-dynamics-7` adds two build-time changes (`SH2Z_VD_LOCATE_DAMP`, `SH2Z_VD_LOCATE_FLIP`) for the two other ways that
search runs all its passes, a swing and a flip between two nodes (default 0: unchanged code).
- [`sh2zam/body_contact_experimental.h`](saturn/include/sh2zam/body_contact_experimental.h)
(`body-contact-1`) resolves contact between vehicles that touch as spheres: shared impact damage,
a push while they close, attackers and contact credit. `vehicle-dynamics-3` feeds it into the
Expand Down
1 change: 1 addition & 0 deletions saturn/VALIDATION.md
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ and scope.
- [vehicle-dynamics-4](validation/vehicle-dynamics-4.md): 2026-11-22 (engine force carried in Q(16+n), vehicle-dynamics-4)
- [vehicle-dynamics-5](validation/vehicle-dynamics-5.md): 2026-11-22 (half-pipe road shape, vehicle-dynamics-5)
- [vehicle-dynamics-6](validation/vehicle-dynamics-6.md): 2026-11-22 (search stop for a body far off the line, vehicle-dynamics-6)
- [vehicle-dynamics-7](validation/vehicle-dynamics-7.md): 2026-11-23 (search damping and flip stop, vehicle-dynamics-7)
- [vehicle-kernels-1](validation/vehicle-kernels-1.md): 2026-11-17 (vehicle kernels, vehicle-kernels-1)
- [view-camera-1](validation/view-camera-1.md): 2026-11-17 (view from a camera, view-camera-1)
- [view-merge-2](validation/view-merge-2.md): 2026-11-17 (tier texture-merge planner, view-merge-2)
Expand Down
35 changes: 35 additions & 0 deletions saturn/VEHICLE_DYNAMICS.md
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,41 @@ units for RUN passes in a row. It keeps the state it has; `sh2z_vd_locate` then
(every pass, with the residual).
- Every translation unit of one image must use the same values.

## Search damping and flip stop (`vehicle-dynamics-7`, `SH2Z_VD_LOCATE_DAMP`, `SH2Z_VD_LOCATE_FLIP`)

The search steps the spline parameter by the projection of the residual on the tangent, `t += d.f / |f|^2`, and leaves out
the curvature term of the Newton step, `|f|^2 + (p - x).p''`. A body hundreds of units off the line, such as one over a gap,
runs the whole 99 passes in three ways, two of them under the `vehicle-dynamics-6` line (128 units) and so not stopped by it:

1. **Swing.** Near the centre of curvature (`-disp.p'' / |f|^2` near 1) the step is about twice the Newton step: the residual
alternates in sign and closes by about 0.94 a pass (36 units to 1 in 54 passes, 883 units off the line).
2. **Join flip.** The closest point is the join of two nodes, with a different tangent length either side; the search flips
between them, back to the same parameter every second pass, with a residual that does not fall (47 units, drifting by
0.01 a pass). There is no exact repeat, so the cycle cannot be skipped exactly.
3. **Past the centre of curvature.** The curvature term passes -1, the minimum is gone, and the residual falls and rises
again. Not changed by this tag.

`SH2Z_VD_LOCATE_DAMP` (build time, passes 0 to 99, default 0 = off): after that many passes of the call, the secant step
through the last two passes, `-along * prev_dt / (along - prev_along)`, replaces the pass's step when it is not longer. It has
the step's sign (the slope is negative), so the search is only ever damped, never pushed further than before. A residual that
falls one way takes the longer step. A node change forgets the last pass. A search of DAMP passes or fewer is unchanged.

`SH2Z_VD_LOCATE_FLIP` (build time, flips 1 or more, default 0 = off): the search ends once it has flipped between two nodes
that many times in a row, each time back to the parameter it had two passes before (within 2^-10 of a node), and on a pass
of the parity the 99th pass has. The state is then the one the 99-pass search would end on, to the drift of the cycle: the
join is on one side or the other of the closest point, and which side is the parity of the pass.

- Off: the object code of every earlier tag is unchanged (the three vehicle units compared with `cmp`, CI flags).
- Both on (DAMP 8, FLIP 3): +504 bytes of text in `sh2zam_vehicle_dynamics.o`. A secant step costs one division
(`sh2z_divu_div64`) on a pass; the flip test, three compares.
- A search neither change acted on is the earlier search word for word. Where the earlier search converged within 700 units
of the line and a change acted on it, the changed search converged too, within 1 unit of its end state, in 339 of 340
cases in the host test; the exception ended 12.5 units off it (a flip stop at 40 passes; the earlier search closed in 59 more).
Where the earlier search did not converge there is no right end state: the changed one may end elsewhere. The caller
checks its outcome (the validation record gives the one made on a road with a jump).
- Hooks for host builds, empty by default: `SH2Z_VD_LOCATE_DAMP_HIT(old_dt, new_dt)` and `SH2Z_VD_LOCATE_FLIP_HIT()`.
- Every translation unit of one image must use the same values.

## Formats

| Quantity | Format |
Expand Down
26 changes: 26 additions & 0 deletions saturn/include/sh2zam/vehicle_dynamics_experimental.h
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,32 @@
#error "SH2Z_VD_LOCATE_FAR must be 0..32767 and SH2Z_VD_LOCATE_FAR_RUN at least 1"
#endif

/* Contract tag vehicle-dynamics-7. The other two ways the closest-point search runs all 99 passes of a body hundreds
* of units off the line, both with a residual under the vehicle-dynamics-6 line:
* - the step of a pass ignores the curvature of the line, so where the body sits near the centre of curvature it is
* about twice the Newton step and the residual swings from side to side, closing by 0.94 a pass.
* SH2Z_VD_LOCATE_DAMP (build time, passes 0..99, default 0 = off) replaces the step, after that many passes of
* the call, by the secant step through the last two passes when that step is not longer. It has the step's
* sign. Searches of DAMP passes or fewer are unchanged.
* - the closest point is the join of two nodes, and the search flips between them with a residual that does not
* fall. SH2Z_VD_LOCATE_FLIP (build time, flips >= 1, default 0 = off) ends the search once it has flipped that many
* times in a row, each time returning to the parameter it had two passes before to within 2^-10 of a node, and
* on a pass of the parity the 99th pass has: the state is the one the 99-pass search would end on, to the drift
* of the cycle.
* Over 120,000 randomised searches (a stadium and rough roads, targets up to 4,000 units off the line) a search
* that neither change acted on is the earlier search word for word (107,587 of them); where the earlier search
* converged within 700 units of the line the changed search converged too, within 1 unit of its end state
* (the host test states the bound it measured). Where the earlier search did not converge there is no
* right end state to compare with, and a changed search can end elsewhere. Off, the object code of every
* earlier tag is unchanged. Every translation unit of one image must use the same values.
* SH2Z_VD_LOCATE_DAMP_HIT(old, new) and SH2Z_VD_LOCATE_FLIP_HIT() are empty statements unless a build defines them. */
#ifndef SH2Z_VD_LOCATE_DAMP
#define SH2Z_VD_LOCATE_DAMP 0
#endif
#ifndef SH2Z_VD_LOCATE_FLIP
#define SH2Z_VD_LOCATE_FLIP 0
#endif

#ifdef __cplusplus
extern "C" {
#endif
Expand Down
59 changes: 59 additions & 0 deletions saturn/src/sh2zam_vehicle_dynamics.c
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,16 @@
#ifndef SH2Z_VD_LOCATE_PASS
#define SH2Z_VD_LOCATE_PASS(d)
#endif
/* vehicle-dynamics-7: host-test hooks, empty statements unless a build defines them. DAMP_HIT(old, new) is invoked
* with the step of the pass (Q30 of t) and the secant step that replaces it; FLIP_HIT() when the flip stop ends a search. */
#ifndef SH2Z_VD_LOCATE_DAMP_HIT
#define SH2Z_VD_LOCATE_DAMP_HIT(old_dt, new_dt)
#endif
#ifndef SH2Z_VD_LOCATE_FLIP_HIT
#define SH2Z_VD_LOCATE_FLIP_HIT()
#endif
/* The parameter distance (Q30) under which a flip-flop returns to the same point (vehicle-dynamics-7). */
#define VD_FLIP_T (ONE30 >> 10)
/* The squared residual (Q32) that SH2Z_VD_LOCATE_FAR units make. */
#define VD_FAR_SQ (((int64_t)SH2Z_VD_LOCATE_FAR * SH2Z_VD_LOCATE_FAR) << 32)

Expand Down Expand Up @@ -152,6 +162,15 @@ int sh2z_vd_locate(sh2z_vd_body_t *b, const sh2z_vd_track_t *k, const int32_t ta
int i;
#if SH2Z_VD_LOCATE_FAR
int far_run = 0;
#endif
#if SH2Z_VD_LOCATE_DAMP
int pass = 0;
int32_t prev_along = 0, prev_dt = 0;
#endif
#if SH2Z_VD_LOCATE_FLIP
int flips = 0;
int32_t t1 = b->t, t2 = b->t;
uint32_t node1 = b->node, node2 = b->node;
#endif
for(i = 0; i < 3; i++) d[i] = target[i] - b->ground[i];
while(sumsq(d) > tolerance && --iterations != 0) {
Expand All @@ -167,17 +186,57 @@ int sh2z_vd_locate(sh2z_vd_body_t *b, const sh2z_vd_track_t *k, const int32_t ta
SH2Z_VD_LOCATE_FAR_HIT();
break;
}
#endif
#if SH2Z_VD_LOCATE_FLIP
/* vehicle-dynamics-7: the join of two nodes holds the closest point when the search keeps flipping between
* them and returns to the same parameter, to 2^-10 of a node, every second pass. */
flips = (b->node == node2 && b->node != node1 && b->t - t2 < VD_FLIP_T && t2 - b->t < VD_FLIP_T) ? flips + 1 : 0;
node2 = node1;
node1 = b->node;
t2 = t1;
t1 = b->t;
if(flips >= SH2Z_VD_LOCATE_FLIP && (iterations & 1) == 0) {
SH2Z_VD_LOCATE_FLIP_HIT();
break;
}
#endif
const int64_t dot = (int64_t)d[0] * b->fwd[0] + (int64_t)d[1] * b->fwd[1] + (int64_t)d[2] * b->fwd[2];
const int32_t along = vd_div(dot, b->fwd_mag); /* Q16 */
#if SH2Z_VD_LOCATE_DAMP
int32_t dt = vd_divq(along, b->fwd_mag, 30);
/* vehicle-dynamics-7: after SH2Z_VD_LOCATE_DAMP passes, the secant step through the last two passes
* replaces the step when it is not longer (it then has the step's sign: the slope is negative). A residual that swings from side
* to side (the step is then about twice the Newton step) closes in two or three passes; a residual
* that falls one way keeps the step. */
if(++pass > SH2Z_VD_LOCATE_DAMP && prev_dt != 0) {
const int32_t da = along - prev_along;
if(da != 0 && (da < 0) != (prev_dt < 0)) {
const int32_t s = vd_div(-(int64_t)along * prev_dt, da);
if((s < 0 ? -(int64_t)s : (int64_t)s) <= (dt < 0 ? -(int64_t)dt : (int64_t)dt)) {
SH2Z_VD_LOCATE_DAMP_HIT(dt, s);
dt = s;
}
}
}
prev_along = along;
prev_dt = dt;
int64_t t = (int64_t)b->t + dt;
#else
int64_t t = (int64_t)b->t + vd_divq(along, b->fwd_mag, 30);
#endif
int32_t dist, fhat[3], perp[3], n[3];
if(t >= ONE30) {
if(t > (int64_t)ONE30 * 19 / 10) return -1;
#if SH2Z_VD_LOCATE_DAMP
prev_dt = 0;
#endif
b->node = wrap(k, b->node + 1u);
t = (int64_t)mu(vd_sat(t - ONE30), vd_divq(b->fwd_mag, tangent_mag(k, b->node, 0), 30));
} else if(t < 0) {
if(t < -(int64_t)ONE30 * 9 / 10) return -1;
#if SH2Z_VD_LOCATE_DAMP
prev_dt = 0;
#endif
b->node = wrap(k, b->node + k->count - 1u);
t = (int64_t)mu(vd_sat(t), vd_divq(b->fwd_mag, tangent_mag(k, b->node, ONE30), 30)) + ONE30;
}
Expand Down
37 changes: 37 additions & 0 deletions saturn/tests/mutate_vehicle_locate7.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
#!/bin/sh
# Mutation controls for test_vehicle_locate7.c: each mutated copy of the search (or each extreme configuration) must make the test fail. Usage: mutate_vehicle_locate7.sh [CC] (run from the saturn directory)
set -u
CC=${1:-gcc}
T=$(mktemp -d)
SRC=src/sh2zam_vehicle_dynamics.c
build() { # name VD_SRC extra...
name=$1; src=$2; shift 2
$CC -std=c11 -O2 -Iinclude tests/test_vehicle_locate7.c $SRC src/sh2zam_unit30.c src/sh2zam_divu.c \
-DVD_SRC="\"$src\"" "$@" -lm -o "$T/$name" 2>"$T/$name.err" || { echo "mutant $name: build failed"; cat "$T/$name.err" | head -5; return 1; }
if "$T/$name" >"$T/$name.out" 2>&1; then
if [ "$name" = clean ]; then echo "passes $name"; else echo "SURVIVED $name"; fail=1; fi
else
if [ "$name" = clean ]; then echo "FAILED $name"; fail=1; else echo "caught $name: $(tail -1 "$T/$name.out")"; fi
fi
}
mut() { # name sed-expression
sed "$2" $SRC > "$T/$1.c"; cmp -s $SRC "$T/$1.c" && { echo "mutant $1 did not change the source"; fail=1; return; }
build "$1" "$T/$1.c"
}
fail=0
# the unmutated source must pass
build clean "$(pwd)/$SRC" || fail=1
build ctl_damp_one "$(pwd)/$SRC" -DB_DAMP=1
build ctl_flip_one "$(pwd)/$SRC" -DB_FLIP=1
build ctl_flip_two "$(pwd)/$SRC" -DB_FLIP=2
mut step_twice_as_long 's/<= (dt < 0 ? -(int64_t)dt : (int64_t)dt)) {/<= 2 * (dt < 0 ? -(int64_t)dt : (int64_t)dt)) {/'
mut secant_sign 's/vd_div(-(int64_t)along \* prev_dt, da)/vd_div((int64_t)along * prev_dt, da)/'
mut damp_from_second_pass 's/if(++pass > SH2Z_VD_LOCATE_DAMP \&\& prev_dt != 0) {/if(++pass > 0 \&\& prev_dt != 0) {/'
mut secant_slope_sign 's/(da < 0) != (prev_dt < 0)/(da < 0) == (prev_dt < 0)/'
mut no_damp_hook 's/SH2Z_VD_LOCATE_DAMP_HIT(dt, s);//'
mut flip_without_parity 's/ \&\& (iterations \& 1) == 0) {/) {/'
mut flip_no_return_test 's/ \&\& b->t - t2 < VD_FLIP_T \&\& t2 - b->t < VD_FLIP_T//'
mut flip_returns_minus_one 's/SH2Z_VD_LOCATE_FLIP_HIT();/SH2Z_VD_LOCATE_FLIP_HIT(); return -1;/'
mut no_flip_hook 's/SH2Z_VD_LOCATE_FLIP_HIT();//'
rm -rf "$T"
exit $fail
Loading
Loading