Skip to content

3D engine that AI agents drive

Swing damaged doors open on their hinges (#983)

Nguyen Xuan Anb28ae83b086b
diff --git a/src/vehicle-damage.js b/src/vehicle-damage.js
index ca23f308..f7e10964 100644
--- a/src/vehicle-damage.js
+++ b/src/vehicle-damage.js
@@ -379,6 +379,154 @@ export function detachedPartFlight(part, hit, { seed = 0, gravity = 9.81, ground
};
}
+export const MAX_SWING_SECONDS = 30;
+export const MAX_SWING_SPEED = 15;
+export const MAX_DOOR_ANGLE_DEGREES = 170;
+export const MAX_DOOR_DAMPING = 20;
+const DOOR_STEP_SECONDS = 1 / 240;
+const DOOR_IMPULSE_SHARE = 0.01;
+const DOOR_SPEED_JITTER = 0.1;
+const DOOR_STOP_BOUNCE = 0.1;
+const DOOR_DRY_FRICTION = 2;
+const DOOR_REST_SPEED = 0.05;
+const DEFAULT_DOOR_MAX_ANGLE_DEGREES = 70;
+const DEFAULT_DOOR_DAMPING = 3;
+
+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],
+ ];
+}
+
+function dotProduct(first, second) {
+ return first[0] * second[0] + first[1] * second[1] + first[2] * second[2];
+}
+
+function subtract(first, second) {
+ return [first[0] - second[0], first[1] - second[1], first[2] - second[2]];
+}
+
+/** Rotate `vector` by `angle` radians about the unit `axis` (Rodrigues). */
+function rotateAboutAxis(vector, axis, angle) {
+ const cosine = Math.cos(angle);
+ const sine = Math.sin(angle);
+ const crossed = crossProduct(axis, vector);
+ const along = dotProduct(axis, vector) * (1 - cosine);
+ return [0, 1, 2].map(index => vector[index] * cosine + crossed[index] * sine + axis[index] * along);
+}
+
+/** Euler angles (radians, XYZ order, like `detachedPartFlight`) of the turn by `angle` about the unit `axis`. */
+function eulerOfAxisAngle(axis, angle) {
+ const [columnX, columnY, columnZ] = [[1, 0, 0], [0, 1, 0], [0, 0, 1]].map(unit => rotateAboutAxis(unit, axis, angle));
+ const sineY = Math.max(-1, Math.min(1, columnZ[0]));
+ if (Math.abs(sineY) > 0.9999999) return [Math.atan2(columnY[2], columnY[1]), Math.asin(sineY), 0];
+ return [Math.atan2(-columnZ[1], columnZ[2]), Math.asin(sineY), Math.atan2(-columnY[0], columnX[0])];
+}
+
+function swingInertia(part, hingePoint, axis) {
+ const toCentre = subtract(part.position, hingePoint);
+ const alongAxis = dotProduct(toCentre, axis);
+ const centreDistance = Math.hypot(toCentre[0] - axis[0] * alongAxis, toCentre[1] - axis[1] * alongAxis, toCentre[2] - axis[2] * alongAxis);
+ if (centreDistance < 1e-3) throw invalidField('hinge.point', hingePoint, 'a point on the hinge line, which must pass at least 1 mm from the door centre, such as [0.8, 0.6, -0.1]');
+ const longestSide = part.size ? Math.max(...part.size) : 0;
+ return part.mass * (centreDistance * centreDistance + (longestSide * longestSide) / 12);
+}
+
+function initialSwingSpeed(part, hit, hinge, inertia, nextUnit) {
+ const lever = subtract(hit.point ?? part.position, hinge.point);
+ const excessImpulse = hit.impulse - part.threshold;
+ const push = normalized(hit.direction).map(component => component * excessImpulse * DOOR_IMPULSE_SHARE);
+ const torque = dotProduct(hinge.axis, crossProduct(lever, push));
+ const jitter = 1 + (nextUnit() * 2 - 1) * DOOR_SPEED_JITTER;
+ return Math.max(-MAX_SWING_SPEED, Math.min(MAX_SWING_SPEED, (torque / inertia) * jitter));
+}
+
+/** Advance the opening of the door (0 shut to `swing.maxAngle` open, speed positive while opening) by `seconds`. */
+function advanceSwing(state, swing, seconds) {
+ let remaining = seconds;
+ while (remaining > 0 && !state.settled) {
+ const step = Math.min(DOOR_STEP_SECONDS, remaining);
+ const heading = Math.sign(state.speed);
+ let speed = state.speed - (swing.damping * state.speed + DOOR_DRY_FRICTION * heading) * step;
+ if (Math.sign(speed) !== heading) speed = 0;
+ state.opening += speed * step;
+ if (state.opening >= swing.maxAngle) {
+ state.opening = swing.maxAngle;
+ speed = -speed * DOOR_STOP_BOUNCE;
+ } else if (state.opening <= 0) {
+ state.opening = 0;
+ speed = -speed * DOOR_STOP_BOUNCE;
+ }
+ state.speed = speed;
+ state.settled = Math.abs(speed) < DOOR_REST_SPEED;
+ if (state.settled) state.speed = 0;
+ remaining -= step;
+ }
+}
+
+/**
+ * A door swings open on its hinge instead of tearing off. `part` is `{ id, threshold, mass, position, size }` (the door's
+ * centre; `size` is optional and sets its inertia), `hit` is `{ impulse, direction, point }` as for `detachedPartFlight`,
+ * and `hinge` is `{ point, axis }`: a point on the hinge line and its direction (any length but zero). Returns null while
+ * the impulse stays below `threshold`. Otherwise `swing.sample(time)` gives `{ angle, angularVelocity, position, rotation,
+ * quaternion, settled }` for 0 to 30 s: the door turns about the hinge line at the speed the hit's torque gives it
+ * (a share of `impulse - threshold` about the lever from `point` to the hinge, over the door's inertia, at most 15 rad/s),
+ * slows by `damping` and dry friction, stops at `maxAngle` degrees (0 to 170, default 70) with a small bounce and rests. It never swings back past shut, since the frame stops it.
+ * `angle` is in radians and its sign follows the hit; `position` is the door centre and `rotation` (Euler XYZ) and
+ * `quaternion` ([x, y, z, w]) its turn, so the hinge line stays fixed. A hit along the hinge line gives no torque, so
+ * the door does not move. The same inputs and `seed` give the same numbers.
+ */
+export function hingedDoorSwing(part, hit, { hinge, maxAngle = DEFAULT_DOOR_MAX_ANGLE_DEGREES, damping = DEFAULT_DOOR_DAMPING, seed = 0 } = {}) {
+ requirePlainObject(part, 'part', '{ "id": "door-left", "threshold": 9000, "mass": 20, "position": [0.8, 0.6, 0.4] }');
+ if (typeof part.id !== 'string' || part.id === '') throw invalidField('part.id', part.id, 'the id of a part, such as "door-left"', TypeError);
+ requireNumber(part.threshold, 'part.threshold', 1e-6, 1e9, 9000);
+ requireNumber(part.mass, 'part.mass', 1e-3, 1e5, 20);
+ requireVector3(part.position, 'part.position', '[0.8, 0.6, 0.4]');
+ if (part.size !== undefined) {
+ requireVector3(part.size, 'part.size', '[0.1, 0.9, 1.0], the box of the door in metres').forEach((extent, axis) => requireNumber(extent, `part.size[${axis}]`, 1e-3, 100, 0.5));
+ }
+ requirePlainObject(hit, 'hit', '{ "impulse": 16800, "direction": [1, 0, 0], "point": [0.8, 0.6, 0.9] }');
+ requireNumber(hit.impulse, 'hit.impulse', 0, 1e9, 16800);
+ const rawDirection = requireVector3(hit.direction, 'hit.direction', '[1, 0, 0]');
+ if (Math.hypot(...rawDirection) < 1e-6) throw invalidField('hit.direction', rawDirection, 'a vector that is not zero, such as [1, 0, 0]');
+ if (hit.point !== undefined) requireVector3(hit.point, 'hit.point', '[0.8, 0.6, 0.9]');
+ requirePlainObject(hinge, 'hinge', '{ "point": [0.8, 0.6, -0.1], "axis": [0, 1, 0] }');
+ const hingePoint = requireVector3(hinge.point, 'hinge.point', '[0.8, 0.6, -0.1]');
+ const rawAxis = requireVector3(hinge.axis, 'hinge.axis', '[0, 1, 0]');
+ if (Math.hypot(...rawAxis) < 1e-6) throw invalidField('hinge.axis', rawAxis, 'a direction that is not zero, such as [0, 1, 0]');
+ requireNumber(maxAngle, 'maxAngle', 0, MAX_DOOR_ANGLE_DEGREES, DEFAULT_DOOR_MAX_ANGLE_DEGREES);
+ requireNumber(damping, 'damping', 0, MAX_DOOR_DAMPING, DEFAULT_DOOR_DAMPING);
+ requireNumber(seed, 'seed', 0, 4294967295, 7);
+ const axis = normalized(rawAxis);
+ const inertia = swingInertia(part, hingePoint, axis);
+ if (hit.impulse < part.threshold) return null;
+ const initialSpeed = initialSwingSpeed(part, hit, { point: hingePoint, axis }, inertia, seededUnitSequence(seed));
+ const swing = { damping, maxAngle: (maxAngle * Math.PI) / 180 };
+ const toCentre = subtract(part.position, hingePoint);
+ return {
+ id: part.id,
+ initialAngularVelocity: initialSpeed,
+ sample(time) {
+ requireNumber(time, 'time', 0, MAX_SWING_SECONDS, 1);
+ const state = { opening: 0, speed: Math.abs(initialSpeed), settled: initialSpeed === 0 };
+ advanceSwing(state, swing, time);
+ const angle = Math.sign(initialSpeed) * state.opening + 0;
+ const turned = rotateAboutAxis(toCentre, axis, angle);
+ const half = Math.sin(angle / 2);
+ return {
+ angle,
+ angularVelocity: Math.sign(initialSpeed) * state.speed + 0,
+ position: [hingePoint[0] + turned[0], hingePoint[1] + turned[1], hingePoint[2] + turned[2]],
+ rotation: eulerOfAxisAngle(axis, angle),
+ quaternion: [axis[0] * half, axis[1] * half, axis[2] * half, Math.cos(angle / 2)],
+ settled: state.settled,
+ };
+ },
+ };
+}
+
/**
* The vertex estimate of a recipe `dented` geometry, which is its source's: checks the dent fields and that the source
* is a geometry of the recipe that is not itself a fragment or a dent. `estimateSource` estimates a source record.
diff --git a/src/vehicle.js b/src/vehicle.js
index 9d66d215..4daef1f0 100644
--- a/src/vehicle.js
+++ b/src/vehicle.js
@@ -1,7 +1,7 @@
import { mat4, vec3 } from 'gl-matrix';
import { invalidField } from './field-error.js';
-export { crashImpulse, dentDepth, dentGeometry, detachedPartFlight, detachedPartIds, findDriveImpact } from './vehicle-damage.js';
+export { crashImpulse, dentDepth, dentGeometry, detachedPartFlight, detachedPartIds, findDriveImpact, hingedDoorSwing } from './vehicle-damage.js';
// Local vehicle frame: x is left, y is up, z is forward, origin on the ground under the wheel centroid.
const FORWARD_AXES = Object.freeze({
diff --git a/tests/vehicle-hinged-door.test.js b/tests/vehicle-hinged-door.test.js
new file mode 100644
index 00000000..fc78760e
--- /dev/null
+++ b/tests/vehicle-hinged-door.test.js
@@ -0,0 +1,111 @@
+import test from 'node:test';
+import assert from 'node:assert/strict';
+import { hingedDoorSwing } from '../src/vehicle.js';
+
+const DOOR = { id: 'door-right', threshold: 9000, mass: 20, position: [0.9, 0.6, 0.4], size: [0.05, 0.8, 1] };
+const HINGE = { point: [0.9, 0.6, 0.9], axis: [0, 1, 0] };
+const HIT = { impulse: 16800, direction: [1, 0, 0], point: [0.9, 0.6, -0.05] };
+const FRAME_SECONDS = 1 / 60;
+
+function rotateByQuaternion(vector, [x, y, z, w]) {
+ const crossed = [y * vector[2] - z * vector[1], z * vector[0] - x * vector[2], x * vector[1] - y * vector[0]];
+ const crossedAgain = [y * crossed[2] - z * crossed[1], z * crossed[0] - x * crossed[2], x * crossed[1] - y * crossed[0]];
+ return [0, 1, 2].map(index => vector[index] + 2 * (w * crossed[index] + crossedAgain[index]));
+}
+
+function rotateByEuler([angleX, angleY, angleZ], vector) {
+ const [x, y, z] = vector;
+ const aroundZ = [x * Math.cos(angleZ) - y * Math.sin(angleZ), x * Math.sin(angleZ) + y * Math.cos(angleZ), z];
+ const aroundY = [aroundZ[0] * Math.cos(angleY) + aroundZ[2] * Math.sin(angleY), aroundZ[1], -aroundZ[0] * Math.sin(angleY) + aroundZ[2] * Math.cos(angleY)];
+ return [aroundY[0], aroundY[1] * Math.cos(angleX) - aroundY[2] * Math.sin(angleX), aroundY[1] * Math.sin(angleX) + aroundY[2] * Math.cos(angleX)];
+}
+
+function everyFrame(swing, seconds = 4) {
+ const samples = [];
+ for (let frame = 0; frame * FRAME_SECONDS <= seconds; frame++) samples.push(swing.sample(frame * FRAME_SECONDS));
+ return samples;
+}
+
+test('a door below its threshold stays shut, and one above it swings', () => {
+ assert.equal(hingedDoorSwing(DOOR, { ...HIT, impulse: 8999 }, { hinge: HINGE }), null);
+ const swing = hingedDoorSwing(DOOR, HIT, { hinge: HINGE, seed: 7 });
+ assert.equal(swing.id, 'door-right');
+ assert.equal(swing.sample(0).angle, 0);
+ assert.ok(Math.abs(swing.sample(0.2).angle) > 0.3, `angle ${swing.sample(0.2).angle}`);
+});
+
+test('the same seed gives the same samples, a different seed another speed', () => {
+ const first = everyFrame(hingedDoorSwing(DOOR, HIT, { hinge: HINGE, seed: 7 }));
+ const second = everyFrame(hingedDoorSwing(DOOR, HIT, { hinge: HINGE, seed: 7 }));
+ assert.deepEqual(first, second);
+ const other = hingedDoorSwing(DOOR, HIT, { hinge: HINGE, seed: 8 });
+ assert.notEqual(other.initialAngularVelocity, hingedDoorSwing(DOOR, HIT, { hinge: HINGE, seed: 7 }).initialAngularVelocity);
+});
+
+test('a harder hit swings the door faster', () => {
+ const soft = hingedDoorSwing(DOOR, { ...HIT, impulse: 12000 }, { hinge: HINGE, seed: 1 });
+ const hard = hingedDoorSwing(DOOR, { ...HIT, impulse: 16800 }, { hinge: HINGE, seed: 1 });
+ assert.ok(Math.abs(hard.initialAngularVelocity) > Math.abs(soft.initialAngularVelocity));
+ assert.ok(Math.abs(hard.sample(0.1).angle) > Math.abs(soft.sample(0.1).angle));
+});
+
+test('the angle never passes maxAngle at any step, and a strong hit reaches the stop', () => {
+ const swing = hingedDoorSwing(DOOR, HIT, { hinge: HINGE, maxAngle: 40, damping: 1 });
+ const limit = (40 * Math.PI) / 180;
+ const angles = everyFrame(swing, 8).map(sample => Math.abs(sample.angle));
+ assert.ok(Math.max(...angles) <= limit + 1e-12, `max ${Math.max(...angles)}`);
+ let deepest = 0;
+ for (let time = 0; time < 1; time += 0.001) deepest = Math.max(deepest, Math.abs(swing.sample(time).angle));
+ assert.ok(deepest > limit - 1e-9, `deepest ${deepest}`);
+ assert.ok(angles.at(-1) > limit * 0.5, 'it rests open, not shut');
+});
+
+test('the door never swings back past shut, whatever the damping', () => {
+ const swing = hingedDoorSwing(DOOR, HIT, { hinge: HINGE, maxAngle: 40, damping: 0 });
+ const direction = Math.sign(swing.initialAngularVelocity);
+ for (const sample of everyFrame(swing, 10)) assert.ok(sample.angle * direction >= 0, `angle ${sample.angle}`);
+});
+
+test('a closed stop (maxAngle 0) keeps the door shut', () => {
+ const samples = everyFrame(hingedDoorSwing(DOOR, HIT, { hinge: HINGE, maxAngle: 0 }), 1);
+ assert.ok(samples.every(sample => sample.angle === 0));
+});
+
+test('the hinge edge stays on the hinge axis at every step', () => {
+ const swing = hingedDoorSwing(DOOR, { ...HIT, impulse: 16800 }, { hinge: { point: [0.9, 0.6, 0.9], axis: [0, 2, 0] } });
+ const hingeOffsetFromCentre = [0, 0, 0.5];
+ for (const sample of everyFrame(swing)) {
+ const edge = rotateByQuaternion(hingeOffsetFromCentre, sample.quaternion).map((component, index) => component + sample.position[index]);
+ const distanceFromAxis = Math.hypot(edge[0] - 0.9, edge[2] - 0.9);
+ assert.ok(distanceFromAxis < 1e-6, `edge ${edge}`);
+ }
+});
+
+test('the Euler rotation turns the door like its quaternion', () => {
+ const swing = hingedDoorSwing(DOOR, HIT, { hinge: { point: [0.9, 0.6, 0.9], axis: [1, 2, 3] }, seed: 3 });
+ const sample = swing.sample(0.15);
+ assert.ok(Math.abs(sample.angle) > 0.1);
+ const byQuaternion = rotateByQuaternion([0.3, -0.2, 0.7], sample.quaternion);
+ const byEuler = rotateByEuler(sample.rotation, [0.3, -0.2, 0.7]);
+ byEuler.forEach((component, index) => assert.ok(Math.abs(component - byQuaternion[index]) < 1e-9));
+});
+
+test('the door slows, comes to rest and stays there', () => {
+ const swing = hingedDoorSwing(DOOR, HIT, { hinge: HINGE, maxAngle: 100, damping: 5 });
+ const rest = swing.sample(30);
+ assert.equal(rest.settled, true);
+ assert.equal(rest.angularVelocity, 0);
+ assert.deepEqual(swing.sample(20), swing.sample(30));
+ assert.equal(swing.sample(0.01).settled, false);
+});
+
+test('bad input says what to pass instead', () => {
+ assert.throws(() => hingedDoorSwing(DOOR, HIT, {}), /hinge/);
+ assert.throws(() => hingedDoorSwing(DOOR, HIT, { hinge: { point: [0, 0, 0], axis: [0, 0, 0] } }), /hinge\.axis/);
+ assert.throws(() => hingedDoorSwing(DOOR, HIT, { hinge: { point: [0.9, 0.6, 0.4], axis: [0, 1, 0] } }), /hinge\.point/);
+ assert.throws(() => hingedDoorSwing(DOOR, HIT, { hinge: HINGE, maxAngle: 171 }), /maxAngle/);
+ assert.throws(() => hingedDoorSwing(DOOR, HIT, { hinge: HINGE, damping: -1 }), /damping/);
+ assert.throws(() => hingedDoorSwing({ ...DOOR, mass: 0 }, HIT, { hinge: HINGE }), /part\.mass/);
+ assert.throws(() => hingedDoorSwing(DOOR, { ...HIT, direction: [0, 0, 0] }, { hinge: HINGE }), /hit\.direction/);
+ assert.throws(() => hingedDoorSwing(DOOR, HIT, { hinge: HINGE }).sample(31), /time/);
+});