Skip to content

armgit / 67.js

Public Imported from GitHub
0

3D engine that AI agents drive

Player controllers: ignore a zero-length step

Nguyen Xuan An0c276b0bbbeb
diff --git a/changes/772-zero-step.md b/changes/772-zero-step.md
new file mode 100644
index 00000000..822c7456
--- /dev/null
+++ b/changes/772-zero-step.md
@@ -0,0 +1 @@
+- Fix: the vehicle and flight controllers ignore a zero-length step, so the runtime's first variable-mode tick no longer turns their speed and velocity into NaN.
diff --git a/src/player.js b/src/player.js
index 43b5c48d..eaf34591 100644
--- a/src/player.js
+++ b/src/player.js
@@ -227,6 +227,7 @@ export function createCharacterController(options = {}) {
}
function advance(stepSeconds) {
+ if (!(stepSeconds > 0)) return;
state.previousPosition = [...state.position];
const sideInput = input.getAxis(names.moveX);
const forwardInput = input.getAxis(names.moveForward);
@@ -537,7 +538,8 @@ function finishController(parts) {
init: writeNode,
fixedUpdate: advance,
update(deltaSeconds, context) {
- if (context?.mode === 'variable') advance(deltaSeconds);
+ if (context?.mode !== 'variable' || !(deltaSeconds > 0)) return;
+ advance(deltaSeconds);
},
};
return controller;
@@ -611,6 +613,7 @@ export function createVehicleController(options = {}) {
}
function advance(stepSeconds) {
+ if (!(stepSeconds > 0)) return;
state.previousPosition = [...state.position];
updateSpeed(clamp(input.getAxis(names.throttle), -1, 1), stepSeconds);
const steerWanted = clamp(input.getAxis(names.steer), -1, 1);
@@ -732,6 +735,7 @@ export function createFlightController(options = {}) {
}
function advance(stepSeconds) {
+ if (!(stepSeconds > 0)) return;
state.previousPosition = [...state.position];
steer(stepSeconds);
updateSpeed(stepSeconds);
diff --git a/tests/player-rigs.test.js b/tests/player-rigs.test.js
index cb8996f1..673fc01b 100644
--- a/tests/player-rigs.test.js
+++ b/tests/player-rigs.test.js
@@ -193,6 +193,25 @@ test('the flyer stops at the wall when it flies into it low, and skims the groun
assert.ok(controller.state.position[1] >= 0);
});
+test('a zero-length variable update leaves vehicle and flyer state finite', () => {
+ const makers = [
+ input => createVehicleController({ input, position: [0, 0, 6], ground: courseGround, obstacles: OBSTACLES }),
+ input => createFlightController({ input, position: [0, 3, 4], ground: courseGround, obstacles: OBSTACLES }),
+ ];
+ for (const makeController of makers) {
+ const game = createGame(makeController);
+ game.input.press('script/forward');
+ stepOnce(game);
+ stepOnce(game);
+ game.controller.update(0, { mode: 'variable' });
+ game.controller.fixedUpdate(0);
+ stepOnce(game);
+ const { state } = game.controller;
+ const numbers = [...state.position, ...state.velocity, state.speed, state.yaw];
+ assert.ok(numbers.every(Number.isFinite), `${game.controller.kind} state is not finite: ${numbers}`);
+ }
+});
+
test('flight and driving are deterministic for the same inputs', () => {
const finalStates = [0, 1].map(() => {
const game = createGame(input => createFlightController({ input }));