3D engine that AI agents drive
JavaScript 3d3d-engineagentic-aiai-agentsblenderclaude-codegame-enginegaussian-splattinggltfjavascriptmcpmcp-servermodel-context-protocolpath-tracerpath-tracingpbrrenderingthreejs-alternativewebgl2webgpu
Launch a torn-off part away from the car with a seeded sideways and up kick (Part of #983)
Nguyen Xuan Anf3d58264e517
diff --git a/docs/vehicle.md b/docs/vehicle.mdindex 7a97a633..e53c7674 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 }` and `hit` is `{ impulse, direction }` from `findDriveImpact`. 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 along `direction` at `(impulse - threshold) / (mass * 100)` m/s (at most 30) with a seeded sideways spread and tumble, 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 }` and `hit` is `{ impulse, direction }` from `findDriveImpact`. 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 away from the car (against `direction`) at `(impulse - threshold) / (mass * 100)` m/s (at most 30) with a seeded sideways kick, an up kick and tumble, 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.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 dd7a7d4c..f0108527 100644--- a/src/vehicle-damage.js+++ b/src/vehicle-damage.js@@ -262,11 +262,14 @@ function seededUnitSequence(seed) {function launchVelocityOf(part, hit, nextUnit) {const direction = normalized(hit.direction);const speed = Math.min(MAX_LAUNCH_SPEED, (hit.impulse - part.threshold) / (part.mass * LAUNCH_IMPULSE_PER_KILOGRAM));- const spread = () => (nextUnit() * 2 - 1) * SPREAD_SHARE;+ const horizontalLength = Math.hypot(direction[0], direction[2]);+ const sideways = horizontalLength < 1e-6 ? [1, 0, 0] : [-direction[2] / horizontalLength, 0, direction[0] / horizontalLength];+ const sidewaysKick = (nextUnit() < 0.5 ? -1 : 1) * (0.5 + nextUnit() * 0.5) * SPREAD_SHARE;+ const upwardKick = LAUNCH_UPWARD_SHARE + nextUnit() * SPREAD_SHARE;return [- (direction[0] + spread()) * speed,- (direction[1] + LAUNCH_UPWARD_SHARE + Math.abs(spread())) * speed,- (direction[2] + spread()) * speed,+ (-direction[0] + sideways[0] * sidewaysKick) * speed,+ (-direction[1] + upwardKick) * speed,+ (-direction[2] + sideways[2] * sidewaysKick) * speed,];}@@ -293,8 +296,8 @@ function advanceFlight(state, flight, seconds) {/*** The flight of a loose part a hit tears off. `part` is `{ id, threshold, mass, position }` (N s, kg, the part's centre in* metres); `hit` is `{ impulse, direction }` from `findDriveImpact`. Returns null when the impulse stays below the- * threshold, so the part stays on. Otherwise it returns `{ id, launchVelocity, sample(time) }`: the part leaves along- * `direction` with a seeded sideways spread and tumble, falls under gravity and bounces on the ground plane `groundY`.+ * threshold, so the part stays on. Otherwise it returns `{ id, launchVelocity, sample(time) }`: the part leaves away from+ * the car, against `direction`, with a seeded sideways kick, an up kick and tumble, falls under gravity and bounces on the ground plane `groundY`.* `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 f41355ea..29df76c4 100644--- a/tests/vehicle-detached-part-flight.test.js+++ b/tests/vehicle-detached-part-flight.test.js@@ -10,7 +10,7 @@ test('a part below its threshold stays on, and one above it flies off', () => {const flight = detachedPartFlight(BUMPER, WALL_HIT, { seed: 7 });assert.equal(flight.id, 'front-bumper');const later = flight.sample(0.5);- assert.ok(later.position[2] < BUMPER.position[2] - 1, `moved ${later.position}`);+ assert.ok(later.position[2] > BUMPER.position[2] + 1, `moved ${later.position}`);});test('the same seed gives the same transform, a different seed another', () => {@@ -36,3 +36,13 @@ test('bad input says what to pass instead', () => {assert.throws(() => detachedPartFlight(BUMPER, WALL_HIT, { restitution: 2 }), /restitution/);assert.throws(() => detachedPartFlight(BUMPER, WALL_HIT).sample(31), /time/);});++test('the part leaves away from the car, with a sideways kick and an up kick', () => {+ for (const seed of [1, 2, 3, 4]) {+ const [velocityX, velocityY, velocityZ] = detachedPartFlight(BUMPER, WALL_HIT, { seed }).launchVelocity;+ const awayFromCar = velocityX * WALL_HIT.direction[0] + velocityZ * WALL_HIT.direction[2];+ assert.ok(awayFromCar < 0, `seed ${seed} horizontal velocity dot direction ${awayFromCar}`);+ assert.ok(velocityY > 0);+ assert.notEqual(velocityX, 0);+ }+});