games/kart: braking from speed no longer drives the kart backwards
- Commit
379bc23a7c87a89d603e27bfd8bf8a95f803407a- Author
- Vayun Mathur <[email protected]>
- When
- 2026-11-30 22:24 UTC (5 days ago)
- Parents
9eae3ad
In the placeholder race model (used when the native engine is
absent), human-kart speed was throttle*TOP - brake*TOP/2 with no
lower clamp: braking from progress 0.04 with throttle 0 computed
speed -20 and progress slid backwards toward the start line,
tick after tick. The only guard clamped position at zero, which
is why the existing brake test — braking from rest — passed
while the bug shipped. The native engine clamps speed at 0
('braking holds at standstill'); the placeholder now does the
same.
Regression test drives first, then brakes hard and asserts the
position holds across 6 ticks (failed pre-fix, 0.04 -> 0.02).
Lap banking, input loop, rules and pairing were audited and
cleared. Verified: :games:kart:testDevUnitTest 77 tests and
lint pass.Diff
games/kart/src/main/java/com/vayunmathur/games/kart/domain/PlaceholderRace.kt
diff --git a/games/kart/src/main/java/com/vayunmathur/games/kart/domain/PlaceholderRace.kt b/games/kart/src/main/java/com/vayunmathur/games/kart/domain/PlaceholderRace.ktindex 93b467241b..be7abc3049 100644--- a/games/kart/src/main/java/com/vayunmathur/games/kart/domain/PlaceholderRace.kt+++ b/games/kart/src/main/java/com/vayunmathur/games/kart/domain/PlaceholderRace.kt@@ -154,8 +154,11 @@ class PlaceholderRace( val control = controls[kart.id] ?: KartControl.NEUTRAL val scrub = 1f - kotlin.math.abs(control.steering) * (if (control.drift) 1f - DRIFT_KEEP else 0.18f)- TOP_SPEED * control.throttle * scrub -- TOP_SPEED * 0.5f * control.brake+ // Braking holds at a standstill, never reverses (the native engine+ // clamps speed at 0 the same way): without this, full brake from+ // speed gives a negative speed and the kart slides backwards.+ (TOP_SPEED * control.throttle * scrub -+ TOP_SPEED * 0.5f * control.brake).coerceAtLeast(0f) } kart.progress = (kart.progress + speed * dt).coerceAtLeast(0f) }
games/kart/src/test/java/com/vayunmathur/games/kart/domain/KartLogicExtraTest.kt
diff --git a/games/kart/src/test/java/com/vayunmathur/games/kart/domain/KartLogicExtraTest.kt b/games/kart/src/test/java/com/vayunmathur/games/kart/domain/KartLogicExtraTest.ktindex 189ac479ca..1af072f02f 100644--- a/games/kart/src/test/java/com/vayunmathur/games/kart/domain/KartLogicExtraTest.kt+++ b/games/kart/src/test/java/com/vayunmathur/games/kart/domain/KartLogicExtraTest.kt@@ -145,6 +145,22 @@ class KartLogicExtraTest { assertEquals(0f, race.lapProgress("p1")) } + @Test fun brakeFromSpeedHoldsPositionNeverReverses() {+ // Braking to a standstill holds position (PlaceholderRace.tick doc; the native+ // engine clamps speed at 0 the same way). Starting from rest hides a backwards+ // slide, so drive first: full brake must then leave progress untouched.+ val race = PlaceholderRace(playerIds = listOf("p1"), aiCount = 0)+ race.setControl("p1", KartControl(throttle = 1f))+ race.tick(1f)+ val before = race.lapProgress("p1")+ assertTrue(before > 0f)+ race.setControl("p1", KartControl(throttle = 0f, brake = 1f))+ race.tick(1f)+ assertEquals(before, race.lapProgress("p1"))+ repeat(5) { race.tick(1f) }+ assertEquals(before, race.lapProgress("p1"))+ }+ @Test fun negativeAndHugeDtAreClamped() { val race = PlaceholderRace(playerIds = listOf("p1"), aiCount = 0) race.setControl("p1", KartControl(throttle = 1f))