Skip to content

armgit / 67.js

Public Imported from GitHub
0

3D engine that AI agents drive

Set the clip sea level to its drawn surface so crates stay in view (#982)

Nguyen Xuan An128f4e1874ac
diff --git a/scripts/render-buoyancy-film.mjs b/scripts/render-buoyancy-film.mjs
index 375d00b4..4ab1dd29 100644
--- a/scripts/render-buoyancy-film.mjs
+++ b/scripts/render-buoyancy-film.mjs
@@ -55,8 +55,8 @@ function crate(id, translation, density) {
function buildRecipe(boatAssetId) {
const crates = [
- crate('crate-a', [-3.2, 1.6, 2.2], 350), crate('crate-b', [3.4, 1.1, -1.4], 500), crate('crate-c', [-2.6, 2.0, -3.2], 600),
- crate('crate-d', [2.8, 1.4, 3.1], 250), crate('crate-e', [0.4, 1.8, -4.4], 450),
+ crate('crate-a', [-3.2, 1.1, 2.2], 350), crate('crate-b', [3.4, 1.0, -1.4], 500), crate('crate-c', [-2.6, 1.2, -3.2], 600),
+ crate('crate-d', [2.8, 1.0, 3.1], 250), crate('crate-e', [0.4, 1.1, -4.4], 450),
];
const seaEdits = [
{ op: 'delete', select: { not: { normal: [0, 1, 0], maxAngle: 10 } } },
@@ -80,14 +80,15 @@ function buildRecipe(boatAssetId) {
nodes: [
{ id: 'sea', geometry: 'sea', material: 'water' },
...crates.map(entry => entry.node),
- { id: 'hull', geometry: 'hull', material: 'ghost', castShadow: false, translation: [0, 0.9, 0], scale: [BOAT_SIZE.width, BOAT_SIZE.hullHeight, BOAT_SIZE.length] },
+ { id: 'hull', geometry: 'hull', material: 'ghost', castShadow: false, translation: [0, 1.2, 0], scale: [BOAT_SIZE.width, BOAT_SIZE.hullHeight, BOAT_SIZE.length] },
{ id: 'boat', parent: 'hull', asset: { id: boatAssetId }, translation: [0, -boatLift / BOAT_SIZE.hullHeight, 0], scale: [1 / BOAT_SIZE.width, 1 / BOAT_SIZE.hullHeight, 1 / BOAT_SIZE.length] },
],
- camera: { eye: [6.5, 2.4, 8], target: [0, 0.4, 0], fov: 42 },
+ camera: { eye: [6.5, 2.9, 8], target: [0, 0.8, 0], fov: 42 },
motion: { duration, tracks: [{ kind: 'geometry', id: 'sea', property: 'oceanTime', interpolation: 'LINEAR', keyframes: [{ time: 0, value: 0 }, { time: duration, value: duration }] }] },
physics: {
duration,
- ocean: { geometry: 'sea' },
+ // The sheet is the top of a unit box, so it draws at y = 0.5.
+ ocean: { geometry: 'sea', level: 0.5 },
bodies: [
...crates.map(entry => entry.body),
{ node: 'hull', density: 300, float: { drag: 1.5, angularDrag: 1.5, points: [[-0.6, 0, -1.4], [0.6, 0, -1.4], [-0.6, 0, 1.4], [0.6, 0, 1.4]] } },
diff --git a/tests/physics-buoyancy-clip.test.js b/tests/physics-buoyancy-clip.test.js
new file mode 100644
index 00000000..aaa655c8
--- /dev/null
+++ b/tests/physics-buoyancy-clip.test.js
@@ -0,0 +1,34 @@
+import test from 'node:test';
+import assert from 'node:assert/strict';
+import { bakePhysicsScene, loadPhysicsEngine } from '../src/physics.js';
+import { normalizeOcean, seaHeightAt } from '../src/physics-buoyancy.js';
+
+// The clip's sea draws its surface at y = 0.5 (the top of a unit box base), so its recipe sets level 0.5.
+const CLIP_SEA = { windSpeed: 4.5, choppiness: 0.4, size: 30, seed: 5, resolution: 5, level: 0.5 };
+const FRAMES_PER_SECOND = 60;
+const CRATE_SIDE = 0.9;
+
+test.before(() => loadPhysicsEngine({ buoyancy: true }));
+
+test('crates with the clip settings float at their density and never stay sunk', () => {
+ const sea = normalizeOcean(CLIP_SEA);
+ for (const density of [250, 450, 600]) {
+ const definition = {
+ ocean: CLIP_SEA,
+ bodies: [{ name: 'crate', translation: [-3.2, 1.2, 2.2], float: {} }],
+ colliders: [{ name: 'crate', body: 'crate', shape: 'cuboid', halfExtents: [CRATE_SIDE / 2, CRATE_SIDE / 2, CRATE_SIDE / 2], density }],
+ };
+ const scene = bakePhysicsScene(definition, { frameCount: 10 * FRAMES_PER_SECOND + 1 });
+ let submergedTotal = 0;
+ let samples = 0;
+ for (let time = 5; time <= 10; time += 0.1) {
+ const [x, y, z] = scene.sample(time);
+ const surface = CLIP_SEA.level + seaHeightAt(sea, x, z, time);
+ assert.ok(y - surface > -0.6, `density ${density} crate sank to ${y - surface} m below the waterline at ${time} s`);
+ submergedTotal += Math.min(1, Math.max(0, (surface - y) / CRATE_SIDE + 0.5));
+ samples++;
+ }
+ const meanSubmerged = submergedTotal / samples;
+ assert.ok(Math.abs(meanSubmerged - density / 1000) < 0.1, `density ${density} sat ${meanSubmerged} submerged`);
+ }
+});