3D engine that AI agents drive
JavaScript 3d3d-engineagentic-aiai-agentsblenderclaude-codegame-enginegaussian-splattinggltfjavascriptmcpmcp-servermodel-context-protocolpath-tracerpath-tracingpbrrenderingthreejs-alternativewebgl2webgpu
Validate both teleport arguments before moving the controller (#2614)
abu-tahir-02c1ee2df35ff
diff --git a/changes/2614.md b/changes/2614.mdnew file mode 100644index 00000000..008b998c--- /dev/null+++ b/changes/2614.md@@ -0,0 +1 @@+- A refused `teleport` on a character, vehicle or flyer checks both arguments before it moves the body, so a bad yaw leaves the controller where it was (#2614)diff --git a/src/player.js b/src/player.jsindex 684e982a..70e4f459 100644--- a/src/player.js+++ b/src/player.js@@ -288,10 +288,13 @@ export function createCharacterController(options = {}) {// Position drawn between the last two fixed steps; alpha is context.alpha.positionAt: alpha => blend(state.previousPosition, state.position, alpha),teleport(position, yaw = state.yaw) {- state.position = vectorOption(owner, 'teleport position', position);- state.previousPosition = [...state.position];+ // Both arguments are checked before anything moves, so a refused teleport leaves the player where it was.+ const nextPosition = vectorOption(owner, 'teleport position', position);+ const nextYaw = numberOption(owner, 'teleport yaw', yaw, 0, -1000, 1000);+ state.position = nextPosition;+ state.previousPosition = [...nextPosition];state.velocity = [0, 0, 0];- state.yaw = numberOption(owner, 'teleport yaw', yaw, 0, -1000, 1000);+ state.yaw = nextYaw;state.grounded = false;state.airborne = true;writeNode();@@ -660,11 +663,14 @@ export function createVehicleController(options = {}) {writeNode,advance,teleport(position, yaw) {- state.position = vectorOption(owner, 'teleport position', position);- state.previousPosition = [...state.position];+ // Checked before anything moves, so a refused teleport leaves the car where it was.+ const nextPosition = vectorOption(owner, 'teleport position', position);+ const nextYaw = numberOption(owner, 'teleport yaw', yaw, state.yaw, -1000, 1000);+ state.position = nextPosition;+ state.previousPosition = [...nextPosition];state.velocity = [0, 0, 0];state.forwardSpeed = 0;- state.yaw = numberOption(owner, 'teleport yaw', yaw, state.yaw, -1000, 1000);+ state.yaw = nextYaw;state.grounded = false;state.airborne = true;},@@ -774,10 +780,13 @@ export function createFlightController(options = {}) {writeNode,advance,teleport(position, yaw) {- state.position = vectorOption(owner, 'teleport position', position);- state.previousPosition = [...state.position];+ // Checked before anything moves, so a refused teleport leaves the flyer where it was.+ const nextPosition = vectorOption(owner, 'teleport position', position);+ const nextYaw = numberOption(owner, 'teleport yaw', yaw, state.yaw, -1000, 1000);+ state.position = nextPosition;+ state.previousPosition = [...nextPosition];state.velocity = [0, 0, 0];- state.yaw = numberOption(owner, 'teleport yaw', yaw, state.yaw, -1000, 1000);+ state.yaw = nextYaw;state.pitch = 0;state.grounded = false;state.airborne = true;diff --git a/tests/player-rigs.test.js b/tests/player-rigs.test.jsindex 4d68976b..f71a1e09 100644--- a/tests/player-rigs.test.js+++ b/tests/player-rigs.test.js@@ -311,6 +311,20 @@ test('errors name the option and what to pass', () => {assert.throws(() => createOrbitCamera({ subject: { position: [0, 0, 0] }, zoom: 'zoom' }), /needs options.input/);});+test('a refused teleport leaves the controller where it was (#2614)', () => {+ const input = createScriptedInput();+ const controllers = [+ createCharacterController({ input, position: [1, 2, 3], yaw: 0.5 }),+ createVehicleController({ input, position: [1, 2, 3], yaw: 0.5 }),+ createFlightController({ input, position: [1, 2, 3], yaw: 0.5 }),+ ];+ for (const controller of controllers) {+ const before = structuredClone(controller.state);+ assert.throws(() => controller.teleport([9, 9, 9], NaN), /teleport yaw must be a number/);+ assert.deepEqual(controller.state, before, controller.kind);+ }+});+function approximately(actual, expected, tolerance = 1e-6) {assert.ok(Math.abs(actual - expected) <= tolerance, `expected ${actual} to be within ${tolerance} of ${expected}`);}