3D engine that AI agents drive
JavaScript 3d3d-engineagentic-aiai-agentsblenderclaude-codegame-enginegaussian-splattinggltfjavascriptmcpmcp-servermodel-context-protocolpath-tracerpath-tracingpbrrenderingthreejs-alternativewebgl2webgpu
Rest a sized torn-off part on a face with its lowest corner on the ground (Part of #983)
Nguyen Xuan Ane45ec5595308
diff --git a/docs/vehicle.md b/docs/vehicle.mdindex 2e11b5b8..fdd3251d 100644--- a/docs/vehicle.md+++ b/docs/vehicle.md@@ -222,7 +222,7 @@ In a recipe, a `dented` geometry does the same for a node (details in `scene_capThe source can be any geometry except a fragment or another dent. The vertex count stays the same.A part that comes off in a physics crash is a body joined to the car body by a `fixed` joint with `breakImpulse` ([breakable joints](physics.md#fracture-and-breakable-glue)); it breaks when a body takes a contact impulse over that number in one 1/60 s step.-For a drive, where nothing simulates the loose part, `detachedPartFlight(part, hit, { seed, gravity, groundY, restitution })` gives it a flight. `part` is `{ id, threshold, mass, position, size }`, with `size` the box of the part as three extents in metres (optional) and `hit` is `{ impulse, direction, point }` from `findDriveImpact` (`point` is optional and defaults to the part's centre); the part never crosses the plane through `point` with normal `direction`, which is the wall: with a `size`, no corner of its box (Euler angles in XYZ order) crosses it; without one, only the centre is held, so a long part can swing over the wall. It returns null when the impulse stays below `threshold`, so the part stays on. Otherwise `flight.sample(seconds)` (0 to 30 s) returns `{ position, rotation, velocity, settled }`: the part leaves mostly sideways at `(impulse - threshold) / (mass * 100)` m/s (at most 30) (a seeded sign, across `direction`, with a small rebound along `direction` and an up kick) and tumbles, falls, bounces on the plane `groundY` and comes to rest. The same inputs and `seed` give the same numbers. Set the node's transform from the sample each frame.+For a drive, where nothing simulates the loose part, `detachedPartFlight(part, hit, { seed, gravity, groundY, restitution })` gives it a flight. `part` is `{ id, threshold, mass, position, size }`, with `size` the box of the part as three extents in metres (optional) and `hit` is `{ impulse, direction, point }` from `findDriveImpact` (`point` is optional and defaults to the part's centre); the part never crosses the plane through `point` with normal `direction`, which is the wall: with a `size`, no corner of its box (Euler angles in XYZ order) crosses it; without one, only the centre is held, so a long part can swing over the wall. It returns null when the impulse stays below `threshold`, so the part stays on. Otherwise `flight.sample(seconds)` (0 to 30 s) returns `{ position, rotation, velocity, settled }`: the part leaves mostly sideways at `(impulse - threshold) / (mass * 100)` m/s (at most 30) (a seeded sign, across `direction`, with a small rebound along `direction` and an up kick) and tumbles, falls, bounces on the ground plane `groundY` (the top of the ground, not the centre height) and comes to rest. With a `size` the lowest corner touches the ground, and at rest the box is snapped to lie on a face (the axis nearest vertical becomes vertical, the yaw is kept) with its lowest corner exactly on `groundY`; without a `size` the centre is held at `groundY` and the rotation is not snapped. The same inputs and `seed` give the same numbers. Set the node's transform from the sample each frame.Not covered yet: hinged doors, hood and trunk that pop open, swapping broken glass for shards, and dents driven by the live vehicle controller. A drive is kinematic, so `findDriveImpact` only reports the moment of the hit; the car does not stop or bounce.diff --git a/src/vehicle-damage.js b/src/vehicle-damage.jsindex ca23f308..7e28c6f3 100644--- a/src/vehicle-damage.js+++ b/src/vehicle-damage.js@@ -313,6 +313,56 @@ function keepOnCarSide(state, flight) {for (let axis = 0; axis < 3; axis++) state.velocity[axis] -= (1 + restitution) * approach * wall.normal[axis];}+const WORLD_UP = [0, 1, 0];++function crossProduct(first, second) {+ return [+ first[1] * second[2] - first[2] * second[1],+ first[2] * second[0] - first[0] * second[2],+ first[0] * second[1] - first[1] * second[0],+ ];+}++/** The Euler angles (XYZ order) of the rotation whose columns are the three world directions of the box axes. */+function eulerFromAxes(columns) {+ const element = (row, column) => columns[column][row];+ const sineY = Math.max(-1, Math.min(1, element(0, 2)));+ const angleY = Math.asin(sineY);+ if (Math.abs(sineY) > 0.9999999) return [Math.atan2(element(2, 1), element(1, 1)), angleY, 0];+ return [Math.atan2(-element(1, 2), element(2, 2)), angleY, Math.atan2(-element(0, 1), element(0, 0))];+}++/** The nearest rotation that lies the box on a face: the axis closest to vertical becomes vertical, the yaw is kept. */+function restingRotation(rotation) {+ const columns = [0, 1, 2].map(axis => {+ const unitAxis = [0, 0, 0];+ unitAxis[axis] = 1;+ return rotateByEuler(rotation, unitAxis);+ });+ const verticalAxis = [0, 1, 2].reduce((best, axis) => (Math.abs(columns[axis][1]) > Math.abs(columns[best][1]) ? axis : best), 0);+ const first = (verticalAxis + 1) % 3;+ const second = (verticalAxis + 2) % 3;+ const up = columns[verticalAxis][1] < 0 ? [0, -1, 0] : WORLD_UP;+ const flat = [columns[first][0], 0, columns[first][2]];+ const flatLength = Math.hypot(...flat);+ const firstAxis = flatLength < 1e-9 ? [1, 0, 0] : flat.map(component => component / flatLength);+ const rebuilt = [null, null, null];+ rebuilt[verticalAxis] = up;+ rebuilt[first] = firstAxis;+ rebuilt[second] = crossProduct(up, firstAxis);+ return eulerFromAxes(rebuilt);+}++function settle(state, flight) {+ state.velocity.fill(0);+ state.spin.fill(0);+ state.settled = true;+ if (!flight.halfExtents) return;+ state.rotation = restingRotation(state.rotation);+ keepOnCarSide(state, flight);+ state.position[1] = flight.groundY + reachAlong(WORLD_UP, state.rotation, flight.halfExtents);+}+function advanceFlight(state, flight, seconds) {const steps = Math.round(seconds / FLIGHT_STEP_SECONDS);for (let step = 0; step < steps && !state.settled; step++) {@@ -320,17 +370,14 @@ function advanceFlight(state, flight, seconds) {for (let axis = 0; axis < 3; axis++) state.position[axis] += state.velocity[axis] * FLIGHT_STEP_SECONDS;for (let axis = 0; axis < 3; axis++) state.rotation[axis] += state.spin[axis] * FLIGHT_STEP_SECONDS;keepOnCarSide(state, flight);- if (state.position[1] > flight.groundY) continue;- state.position[1] = flight.groundY;+ const lowestCentreHeight = flight.groundY + reachAlong(WORLD_UP, state.rotation, flight.halfExtents);+ if (state.position[1] > lowestCentreHeight) continue;+ state.position[1] = lowestCentreHeight;state.velocity[1] = -state.velocity[1] * flight.restitution;state.velocity[0] *= GROUND_FRICTION;state.velocity[2] *= GROUND_FRICTION;for (let axis = 0; axis < 3; axis++) state.spin[axis] *= GROUND_FRICTION;- if (Math.hypot(...state.velocity) < SETTLE_SPEED) {- state.velocity.fill(0);- state.spin.fill(0);- state.settled = true;- }+ if (Math.hypot(...state.velocity) < SETTLE_SPEED) settle(state, flight);}}@@ -338,7 +385,7 @@ function advanceFlight(state, flight, seconds) {* The flight of a loose part a hit tears off. `part` is `{ id, threshold, mass, position, size }` (N s, kg, the part's centre in* metres, `size` its box as three extents in metres, optional); `hit` is `{ impulse, direction, point }` from `findDriveImpact` (`point` is optional and defaults to the part's centre). Returns null when the impulse stays below the* threshold, so the part stays on. Otherwise it returns `{ id, launchVelocity, sample(time) }`: the part leaves mostly- * sideways (a seeded sign, across `direction`) with a small rebound along `direction` and an up kick, tumbles, falls under gravity and bounces on the ground plane `groundY`. No corner of its box (turned by the Euler angles in XYZ order) crosses the wall plane through `point` with normal `direction`; a part with no `size` is held by its centre only.+ * sideways (a seeded sign, across `direction`) with a small rebound along `direction` and an up kick, tumbles, falls under gravity and bounces on the ground plane `groundY` (the top of the ground, not the centre height: with a `size` the lowest corner touches it, and at rest the box lies on a face with its lowest corner exactly on `groundY`; without a `size` the centre is held at `groundY` and the rotation is not snapped). No corner of its box (turned by the Euler angles in XYZ order) crosses the wall plane through `point` with normal `direction`; a part with no `size` is held by its centre only.* `sample(time)` gives `{ position, rotation (Euler radians), velocity, settled }` for 0 to 30 s, and the same inputs* with the same `seed` give the same numbers on every run.*/diff --git a/tests/vehicle-detached-part-flight.test.js b/tests/vehicle-detached-part-flight.test.jsindex 6f12d888..6b623b1d 100644--- a/tests/vehicle-detached-part-flight.test.js+++ b/tests/vehicle-detached-part-flight.test.js@@ -95,3 +95,25 @@ test('a part with a size is checked and one without keeps the centre-only hold',assert.throws(() => detachedPartFlight({ ...BUMPER, size: [1, 1] }, WALL_HIT), /part\.size/);assert.ok(detachedPartFlight(BUMPER, WALL_HIT, { seed: 1 }).sample(1));});++test('a sized part never sinks into the ground and lies on a face at rest, for 4 seeds, every 1/60 s', () => {+ const groundY = 0.1;+ for (const seed of [1, 2, 3, 4]) {+ const flight = detachedPartFlight(SIZED_BUMPER, WALL_HIT, { seed, groundY });+ for (let step = 0; step <= 10 * 60; step++) {+ const sample = flight.sample(step / 60);+ const lowest = Math.min(...boxCorners(sample.position, sample.rotation, SIZED_BUMPER.size).map(corner => corner[1]));+ assert.ok(lowest >= groundY - 1e-6, `seed ${seed} step ${step} lowest corner ${lowest} is below the ground`);+ }+ const rest = flight.sample(30);+ assert.equal(rest.settled, true);+ const heights = boxCorners(rest.position, rest.rotation, SIZED_BUMPER.size).map(corner => corner[1]);+ const lowest = Math.min(...heights);+ const standing = Math.max(...heights) - lowest;+ assert.ok(Math.abs(lowest - groundY) < 1e-6, `seed ${seed} rests with its lowest corner at ${lowest}`);+ assert.ok(SIZED_BUMPER.size.some(extent => Math.abs(standing - extent) < 1e-6), `seed ${seed} rests ${standing} tall, not on a face`);+ for (const corner of boxCorners(rest.position, rest.rotation, SIZED_BUMPER.size)) {+ assert.ok((corner[2] - WALL_HIT.point[2]) * WALL_HIT.direction[2] >= -1e-6, `seed ${seed} rests across the wall`);+ }+ }+});