Skip to content

3D engine that AI agents drive

Hold every corner of a sized torn-off part on the car side of the wall plane (Part of #983)

Nguyen Xuan An9576f4ca843c
diff --git a/docs/vehicle.md b/docs/vehicle.md
index d0acf8ed..2e11b5b8 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_cap
The 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, 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. 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 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.js
index fdddb5c0..ca23f308 100644
--- a/src/vehicle-damage.js
+++ b/src/vehicle-damage.js
@@ -279,8 +279,33 @@ function launchVelocityOf(part, hit, nextUnit) {
];
}
-function keepOnCarSide(state, wall, restitution) {
- const depth = (state.position[0] - wall.point[0]) * wall.normal[0] + (state.position[1] - wall.point[1]) * wall.normal[1] + (state.position[2] - wall.point[2]) * wall.normal[2];
+function rotateByEuler(rotation, vector) {
+ const [x, y, z] = vector;
+ const [cosZ, sinZ] = [Math.cos(rotation[2]), Math.sin(rotation[2])];
+ const aroundZ = [x * cosZ - y * sinZ, x * sinZ + y * cosZ, z];
+ const [cosY, sinY] = [Math.cos(rotation[1]), Math.sin(rotation[1])];
+ const aroundY = [aroundZ[0] * cosY + aroundZ[2] * sinY, aroundZ[1], -aroundZ[0] * sinY + aroundZ[2] * cosY];
+ const [cosX, sinX] = [Math.cos(rotation[0]), Math.sin(rotation[0])];
+ return [aroundY[0], aroundY[1] * cosX - aroundY[2] * sinX, aroundY[1] * sinX + aroundY[2] * cosX];
+}
+
+/** How far the part's box (`halfExtents`, turned by `rotation`) reaches along `normal` from its centre; 0 for a part with no size. */
+function reachAlong(normal, rotation, halfExtents) {
+ if (!halfExtents) return 0;
+ let reach = 0;
+ for (let axis = 0; axis < 3; axis++) {
+ const unitAxis = [0, 0, 0];
+ unitAxis[axis] = halfExtents[axis];
+ const turned = rotateByEuler(rotation, unitAxis);
+ reach += Math.abs(turned[0] * normal[0] + turned[1] * normal[1] + turned[2] * normal[2]);
+ }
+ return reach;
+}
+
+function keepOnCarSide(state, flight) {
+ const { wall, halfExtents, restitution } = flight;
+ const centreDepth = (state.position[0] - wall.point[0]) * wall.normal[0] + (state.position[1] - wall.point[1]) * wall.normal[1] + (state.position[2] - wall.point[2]) * wall.normal[2];
+ const depth = centreDepth - reachAlong(wall.normal, state.rotation, halfExtents);
if (depth >= 0) return;
for (let axis = 0; axis < 3; axis++) state.position[axis] -= depth * wall.normal[axis];
const approach = state.velocity[0] * wall.normal[0] + state.velocity[1] * wall.normal[1] + state.velocity[2] * wall.normal[2];
@@ -294,7 +319,7 @@ function advanceFlight(state, flight, seconds) {
state.velocity[1] -= flight.gravity * FLIGHT_STEP_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.wall, flight.restitution);
+ keepOnCarSide(state, flight);
if (state.position[1] > flight.groundY) continue;
state.position[1] = flight.groundY;
state.velocity[1] = -state.velocity[1] * flight.restitution;
@@ -310,10 +335,10 @@ 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, point }` from `findDriveImpact` (`point` is optional and defaults to the part's centre). Returns null when the impulse stays below the
+ * 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`. It never crosses the wall plane through `point` with normal `direction`.
+ * 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.
* `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.
*/
@@ -323,6 +348,9 @@ export function detachedPartFlight(part, hit, { seed = 0, gravity = 9.81, ground
requireNumber(part.threshold, 'part.threshold', 1e-6, 1e9, 9000);
requireNumber(part.mass, 'part.mass', 1e-3, 1e5, 12);
requireVector3(part.position, 'part.position', '[0, 0.5, 2]');
+ if (part.size !== undefined) {
+ requireVector3(part.size, 'part.size', '[1.7, 0.35, 0.2], the box of the part in metres').forEach((extent, axis) => requireNumber(extent, `part.size[${axis}]`, 1e-3, 100, 0.5));
+ }
requirePlainObject(hit, 'hit', '{ "impulse": 16800, "direction": [0, 0, -1] }');
requireNumber(hit.impulse, 'hit.impulse', 0, 1e9, 16800);
const rawDirection = requireVector3(hit.direction, 'hit.direction', '[0, 0, -1]');
@@ -337,7 +365,8 @@ export function detachedPartFlight(part, hit, { seed = 0, gravity = 9.81, ground
const launchVelocity = launchVelocityOf(part, hit, nextUnit);
const spin = [0, 1, 2].map(() => (nextUnit() * 2 - 1) * MAX_SPIN_RADIANS_PER_SECOND);
const wall = { point: hit.point ?? part.position, normal: normalized(rawDirection) };
- const flight = { gravity, groundY, restitution, wall };
+ const halfExtents = part.size ? part.size.map(extent => extent / 2) : null;
+ const flight = { gravity, groundY, restitution, wall, halfExtents };
return {
id: part.id,
launchVelocity,
diff --git a/tests/vehicle-detached-part-flight.test.js b/tests/vehicle-detached-part-flight.test.js
index 81c7ecd1..6f12d888 100644
--- a/tests/vehicle-detached-part-flight.test.js
+++ b/tests/vehicle-detached-part-flight.test.js
@@ -59,3 +59,39 @@ test('a part thrown at the wall is held on the car side of its plane', () => {
const towardWall = detachedPartFlight({ ...BUMPER, position: [0, 0.5, 2.09] }, { ...WALL_HIT, direction: [0, 0, -1], point: [0, 0.5, 2.1] }, { seed: 2 });
assert.ok(towardWall.sample(1).position[2] <= 2.1 + 1e-6);
});
+
+const SIZED_BUMPER = { ...BUMPER, position: [0, 0.5, 1.95], size: [1.7, 0.35, 0.2] };
+
+function boxCorners(position, rotation, size) {
+ const [x, y, z] = rotation;
+ const turn = ([vx, vy, vz]) => {
+ const aroundZ = [vx * Math.cos(z) - vy * Math.sin(z), vx * Math.sin(z) + vy * Math.cos(z), vz];
+ const aroundY = [aroundZ[0] * Math.cos(y) + aroundZ[2] * Math.sin(y), aroundZ[1], -aroundZ[0] * Math.sin(y) + aroundZ[2] * Math.cos(y)];
+ return [aroundY[0], aroundY[1] * Math.cos(x) - aroundY[2] * Math.sin(x), aroundY[1] * Math.sin(x) + aroundY[2] * Math.cos(x)];
+ };
+ const corners = [];
+ for (const signX of [-1, 1]) for (const signY of [-1, 1]) for (const signZ of [-1, 1]) {
+ const offset = turn([signX * size[0] / 2, signY * size[1] / 2, signZ * size[2] / 2]);
+ corners.push(offset.map((component, axis) => position[axis] + component));
+ }
+ return corners;
+}
+
+test('no corner of a sized part crosses the wall plane, for 4 seeds, every 1/60 s', () => {
+ for (const seed of [1, 2, 3, 4]) {
+ const flight = detachedPartFlight(SIZED_BUMPER, WALL_HIT, { seed });
+ for (let step = 0; step <= 10 * 60; step++) {
+ const sample = flight.sample(step / 60);
+ for (const corner of boxCorners(sample.position, sample.rotation, SIZED_BUMPER.size)) {
+ const behindWall = (corner[2] - WALL_HIT.point[2]) * WALL_HIT.direction[2];
+ assert.ok(behindWall >= -1e-6, `seed ${seed} step ${step} corner ${corner} is ${behindWall} past the wall`);
+ }
+ }
+ }
+});
+
+test('a part with a size is checked and one without keeps the centre-only hold', () => {
+ assert.throws(() => detachedPartFlight({ ...BUMPER, size: [1, 0, 1] }, WALL_HIT), /part\.size/);
+ assert.throws(() => detachedPartFlight({ ...BUMPER, size: [1, 1] }, WALL_HIT), /part\.size/);
+ assert.ok(detachedPartFlight(BUMPER, WALL_HIT, { seed: 1 }).sample(1));
+});