Skip to content

3D engine that AI agents drive

Read input axes through one check that drops non-finite samples (#2612)

abu-tahir-0d6446f988e90
diff --git a/changes/2612.md b/changes/2612.md
new file mode 100644
index 00000000..411d8026
--- /dev/null
+++ b/changes/2612.md
@@ -0,0 +1 @@
+- One non-finite sample from an input axis counts as released, so a vehicle, flyer, boat or orbit camera stays finite after a broken gamepad bridge sample (#2612)
diff --git a/src/player.js b/src/player.js
index 684e982a..c720977d 100644
--- a/src/player.js
+++ b/src/player.js
@@ -65,6 +65,13 @@ function clamp(value, minimum, maximum) {
return Math.max(minimum, Math.min(maximum, value));
}
+// Reads an axis and treats a non-finite sample as released: one bad reading from a gamepad bridge or a custom source must not
+// leave a controller's yaw, steer or camera angles NaN for every later step.
+function readAxis(input, name) {
+ const value = input.getAxis(name);
+ return Number.isFinite(value) ? value : 0;
+}
+
function blend(from, to, amount) {
return from.map((value, index) => value + (to[index] - value) * amount);
}
@@ -229,8 +236,8 @@ 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);
+ const sideInput = readAxis(input, names.moveX);
+ const forwardInput = readAxis(input, names.moveForward);
const intentLength = Math.min(1, Math.hypot(sideInput, forwardInput));
const intentScale = intentLength > 0 ? intentLength / Math.hypot(sideInput, forwardInput) : 0;
const viewYaw = currentCameraYaw();
@@ -377,13 +384,13 @@ function createCameraRig(kind, options) {
// A zero, negative or NaN step would leave the look angles or the focus NaN for good, as the controllers already refuse.
if (!(stepSeconds > 0)) return;
if (wantsLook) {
- rig.yaw = wrapAngle(rig.yaw + input.getAxis(lookNames.x) * lookSpeed * stepSeconds);
+ rig.yaw = wrapAngle(rig.yaw + readAxis(input, lookNames.x) * lookSpeed * stepSeconds);
}
if (lookNames.y) {
- rig.pitch = clamp(rig.pitch + input.getAxis(lookNames.y) * lookSpeed * stepSeconds, minimumPitch, maximumPitch);
+ rig.pitch = clamp(rig.pitch + readAxis(input, lookNames.y) * lookSpeed * stepSeconds, minimumPitch, maximumPitch);
}
if (zoomName !== null) {
- rig.distance = clamp(rig.distance - input.getAxis(zoomName) * zoomSpeed * stepSeconds, minimumDistance, maximumDistance);
+ rig.distance = clamp(rig.distance - readAxis(input, zoomName) * zoomSpeed * stepSeconds, minimumDistance, maximumDistance);
}
if (yawFollowSeconds > 0) {
const yawAmount = 1 - Math.exp(-stepSeconds / yawFollowSeconds);
@@ -623,8 +630,8 @@ 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);
+ updateSpeed(clamp(readAxis(input, names.throttle), -1, 1), stepSeconds);
+ const steerWanted = clamp(readAxis(input, names.steer), -1, 1);
state.steer += (steerWanted - state.steer) * (1 - Math.exp(-stepSeconds / settings.steerSeconds));
const yawRate = (state.forwardSpeed / settings.wheelbase) * Math.tan(state.steer * settings.maximumSteer);
state.yaw = wrapAngle(state.yaw + yawRate * stepSeconds);
@@ -724,8 +731,8 @@ export function createFlightController(options = {}) {
}
function steer(stepSeconds) {
- const yawInput = clamp(input.getAxis(names.yaw), -1, 1);
- const pitchInput = clamp(input.getAxis(names.pitch), -1, 1);
+ const yawInput = clamp(readAxis(input, names.yaw), -1, 1);
+ const pitchInput = clamp(readAxis(input, names.pitch), -1, 1);
state.yaw = wrapAngle(state.yaw + yawInput * settings.turnRate * stepSeconds);
if (pitchInput !== 0) state.pitch += pitchInput * settings.pitchRate * stepSeconds;
else state.pitch -= state.pitch * (1 - Math.exp(-stepSeconds / settings.levelSeconds));
@@ -842,8 +849,8 @@ export function createBoatController(options = {}) {
function advance(stepSeconds) {
if (!(stepSeconds > 0)) return;
- state.throttle = clamp(input.getAxis(names.throttle), -1, 1);
- state.steer = clamp(input.getAxis(names.rudder), -1, 1);
+ state.throttle = clamp(readAxis(input, names.throttle), -1, 1);
+ state.steer = clamp(readAxis(input, names.rudder), -1, 1);
physics.setBoatControls(bodyName, { throttle: state.throttle, rudder: state.steer });
readHull(stepSeconds);
writeNode();
diff --git a/tests/player-rigs.test.js b/tests/player-rigs.test.js
index 4d68976b..c9b4663f 100644
--- a/tests/player-rigs.test.js
+++ b/tests/player-rigs.test.js
@@ -219,6 +219,33 @@ test('a zero-length variable update leaves vehicle and flyer state finite', () =
}
});
+test('one non-finite axis sample leaves vehicle, flyer and orbit camera state finite (#2612)', () => {
+ let sample = 1;
+ const input = { getAxis: () => sample, isHeld: () => false, wasPressed: () => false };
+ const makers = [
+ ['vehicle', () => createVehicleController({ input })],
+ ['flyer', () => createFlightController({ input })],
+ ['orbit', () => createOrbitCamera({ subject: { position: [0, 0, 0] }, input, zoom: 'zoom' })],
+ ];
+ for (const [kind, make] of makers) {
+ sample = 1;
+ const system = make();
+ const runtime = createRuntime(fakeRenderer, { mode: 'fixed', fixedStepSeconds: 1 / 60, systems: [system], scheduler });
+ runtime.step(STEP_MILLISECONDS);
+ for (const bad of [NaN, Infinity, 1]) {
+ sample = bad;
+ runtime.step(STEP_MILLISECONDS);
+ }
+ const state = system.state ?? system;
+ const numbers = Object.values(state).flatMap(value => (typeof value === 'number' ? [value] : Array.isArray(value) ? value : []));
+ assert.ok(numbers.length > 0 && numbers.every(Number.isFinite), `${kind} state is not finite after a bad sample: ${numbers}`);
+ if (kind === 'orbit') {
+ const { eye, target } = system.pose({ alpha: 1 });
+ assert.ok([...eye, ...target].every(Number.isFinite), `orbit pose is not finite: ${eye} ${target}`);
+ }
+ }
+});
+
test('flight and driving are deterministic for the same inputs', () => {
const finalStates = [0, 1].map(() => {
const game = createGame(input => createFlightController({ input }));