kart: laps count only on an armed forward line crossing
- Commit
7a41b2c8d749803715bace8c3f7533b61e816fe5- Author
- Vayun Mathur <[email protected]>
- When
- 2026-12-01 06:39 UTC (5 days ago)
- Parents
8525882
Lap counting compared wrapped arc positions, which cannot distinguish wrapping forward past the line from moving backward at all: any wrong-way motion banked a lap per tick. Reachable in normal play (a parked kart can turn in place; a shell spin can leave one facing backward): three backward ticks at 60 Hz reached RACE_LAPS, so the race could finish with the backwards kart winning on progress_key. The regression test drove 45 reverse ticks flat-out and banked 44 laps pre-fix; a legitimate lap needs ~19 s. Laps now count only when the raw arc position passes TRACK_LENGTH with a passed_half flag armed — set by a forward halfway crossing, cleared by either line crossing or a backward halfway crossing; spawning past halfway spawns armed, preserving the existing spawn-at-length-minus-5 lap test. Wrong-way line crossings change no lap and force halfway to be re-earned, so oscillation can never farm one. The standings and winner consume kart.lap, so race correctness follows from the physics fix. AI pacing, item dealing and progress banking were audited and cleared. Verified: cargo test 128 pass including the legit full-lap and past-the-line tests (the gate does not over-block); :games:kart:testDevUnitTest 77 tests and lint BUILD SUCCESSFUL.
Diff
games/kart/src/main/rust/src/physics.rs
diff --git a/games/kart/src/main/rust/src/physics.rs b/games/kart/src/main/rust/src/physics.rsindex c5203ff58c..d0d336ac35 100644--- a/games/kart/src/main/rust/src/physics.rs+++ b/games/kart/src/main/rust/src/physics.rs@@ -74,8 +74,12 @@ pub struct KartBody { pub speed: f32, /// Arc distance along the centerline; the source of laps and progress. pub track_s: f32,- /// Completed laps.+ /// Completed laps (forward line crossings past the halfway checkpoint). pub lap: u32,+ /// True once the kart has driven forward past halfway since the last+ /// start/finish crossing; a forward line crossing only counts as a lap+ /// while this holds, so oscillating across the line earns nothing.+ pub passed_half: bool, /// Seconds of continuous drift this corner. Resets on release. pub drift_charge: f32, /// Seconds of boost burn remaining.@@ -96,6 +100,7 @@ impl KartBody { speed: 0.0, track_s, lap: 0,+ passed_half: track_s >= track::TRACK_LENGTH * 0.5, drift_charge: 0.0, boost_left: 0.0, spin_left: 0.0,@@ -178,11 +183,30 @@ impl KartBody { // Snap the arc position back to the centerline: project forward along // the tangent from the old sample. (A full closest-point solve runs in // the renderer path only when standings need exact progress.)- let p = track::point_at(self.track_s);+ let old_s = self.track_s;+ let p = track::point_at(old_s); let along = (self.x - p.x) * p.tx + (self.z - p.z) * p.tz;- let advanced = track::advance(self.track_s, along);- if advanced < self.track_s {- self.lap += 1;+ let advanced = track::advance(old_s, along);+ // Laps count only on a forward crossing of the start/finish line+ // (raw arc position passes TRACK_LENGTH) after the halfway+ // checkpoint has been passed going forward. Comparing the wrapped+ // positions alone cannot tell a forward wrap from ordinary backward+ // motion: a kart driving the wrong way steps `advanced` below+ // `old_s` every tick, and counting those steps banks a lap per tick.+ let raw = old_s + along;+ let half = track::TRACK_LENGTH * 0.5;+ if raw >= track::TRACK_LENGTH {+ if self.passed_half {+ self.lap += 1;+ }+ self.passed_half = false;+ } else if raw < 0.0 {+ // Wrong-way line crossing: no lap, and halfway must be re-earned.+ self.passed_half = false;+ } else if along > 0.0 && old_s < half && advanced >= half {+ self.passed_half = true;+ } else if along < 0.0 && old_s >= half && advanced < half {+ self.passed_half = false; } self.track_s = advanced; }
games/kart/src/main/rust/src/physics/physics_tests.rs
diff --git a/games/kart/src/main/rust/src/physics/physics_tests.rs b/games/kart/src/main/rust/src/physics/physics_tests.rsindex 3bdcb271eb..2bb0131e1f 100644--- a/games/kart/src/main/rust/src/physics/physics_tests.rs+++ b/games/kart/src/main/rust/src/physics/physics_tests.rs@@ -308,6 +308,33 @@ fn grip_scrub_exceeds_drift_speed_loss() { assert!(drift <= TOP_SPEED * DRIFT_KEEP + 0.6); } +#[test]+fn wrong_way_oscillation_across_the_line_earns_no_lap() {+ // Face back down the top straight from the grid slot (s = 8) and drive+ // across the start/finish line the wrong way: the arc position wraps to+ // the end of the lap, but no forward lap has been driven. Turning around+ // and re-crossing the line forwards must still leave lap at 0: only+ // net forward motion of ~zero metres happened (~30 m each way).+ let mut k = KartBody::spawn(8.0, 0.0);+ k.yaw += std::f32::consts::PI;+ k.speed = TOP_SPEED;+ for _ in 0..45 {+ k.tick(&inp(1.0, 0.0, 0.0, false), 1.0 / 60.0);+ }+ assert!(+ k.track_s > track::TRACK_LENGTH - 60.0,+ "wrong-way drive should wrap s to the lap end, got {}",+ k.track_s+ );+ assert_eq!(k.lap, 0);+ k.yaw = track::tangent_yaw(&track::point_at(k.track_s));+ k.speed = TOP_SPEED;+ for _ in 0..45 {+ k.tick(&inp(1.0, 0.0, 0.0, false), 1.0 / 60.0);+ }+ assert_eq!(k.lap, 0, "re-crossing the line after a wrong-way wrap is not a lap");+}+ #[test] fn no_lap_credit_for_mid_track_motion() { let mut k = KartBody::spawn(100.0, 0.0);