3D engine that AI agents drive
Sample buoyancy waves on the same axes as the drawn ocean (#982)
Nguyen Xuan Anea53b3615434
diff --git a/changes/982-buoyancy-axes.md b/changes/982-buoyancy-axes.mdnew file mode 100644index 00000000..e7193427--- /dev/null+++ b/changes/982-buoyancy-axes.md@@ -0,0 +1 @@+- Bodies that float now sample the ocean waves on the same horizontal axes as the drawn `ocean` mesh edit, so a hull rides the wave it sits on and not its mirror (#982)diff --git a/src/physics-buoyancy.js b/src/physics-buoyancy.jsindex 0d0bf56c..40c0dee8 100644--- a/src/physics-buoyancy.js+++ b/src/physics-buoyancy.js@@ -151,16 +151,18 @@ export function checkBuoyancyWork(ocean, floatingBodies, frameCount) {/** Height of the sea surface above the calm level at a world position and physics time, following the horizontal pull of choppy waves. */export function seaHeightAt(ocean, worldX, worldZ, time) {const seaTime = ocean.time + time;- let restX = worldX;- let restZ = worldZ;+ // The ocean mesh edit of a y-up sea reads the waves with its first horizontal axis (z) as the wave x and its second (x) as the+ // wave z, and pulls them by moveX and moveZ in that order. The same order here puts a body on the wave the mesh draws.+ let restFirst = worldZ;+ let restSecond = worldX;if (ocean.choppiness > 0) {for (let pass = 0; pass < CHOPPY_REST_PASSES; pass++) {- const surface = oceanSurfaceAt(ocean.waves, restX, restZ, seaTime, ocean.choppiness);- restX = worldX - surface.moveX;- restZ = worldZ - surface.moveZ;+ const surface = oceanSurfaceAt(ocean.waves, restFirst, restSecond, seaTime, ocean.choppiness);+ restFirst = worldZ - surface.moveX;+ restSecond = worldX - surface.moveZ;}}- return oceanSurfaceAt(ocean.waves, restX, restZ, seaTime, ocean.choppiness).height;+ return oceanSurfaceAt(ocean.waves, restFirst, restSecond, seaTime, ocean.choppiness).height;}function rotateByQuaternion(point, rotation) {diff --git a/tests/physics-buoyancy-mesh-match.test.js b/tests/physics-buoyancy-mesh-match.test.jsnew file mode 100644index 00000000..ee0a683f--- /dev/null+++ b/tests/physics-buoyancy-mesh-match.test.js@@ -0,0 +1,29 @@+import test from 'node:test';+import assert from 'node:assert/strict';+import { oceanMesh } from '../src/mesh-ocean-modifier.js';+import { createMeshData } from '../src/mesh-data.js';+import { normalizeOcean, seaHeightAt } from '../src/physics-buoyancy.js';++const SEA = { windSpeed: 6, direction: 20, size: 30, seed: 5, time: 1.5, resolution: 5 };+const SAMPLE_POINTS = [[5, 2], [2, 5], [-7, 3], [3, -7], [11, -4], [-9, 8]];++function drawnHeights(choppiness) {+ const positions = new Float32Array(SAMPLE_POINTS.flatMap(([x, z]) => [x, 0, z]));+ const faces = { faceOffsets: new Uint32Array([0, 3]), cornerVertices: new Uint32Array([0, 1, 2]) };+ const mesh = oceanMesh(createMeshData({ positions, ...faces }), { axis: 'y', ...SEA, choppiness });+ return SAMPLE_POINTS.map((_, index) => ({ x: mesh.positions[index * 3], height: mesh.positions[index * 3 + 1], z: mesh.positions[index * 3 + 2] }));+}++test('a body floats on the wave the ocean mesh draws at the same place, not on its mirror', () => {+ const ocean = normalizeOcean({ ...SEA, choppiness: 0 });+ for (const { x, z, height } of drawnHeights(0)) {+ assert.ok(Math.abs(seaHeightAt(ocean, x, z, 0) - height) < 1e-4, `at ${x}, ${z}: physics ${seaHeightAt(ocean, x, z, 0)}, drawn ${height}`);+ }+});++test('with choppy waves the sea under a body is the drawn height at the drawn, pulled position', () => {+ const ocean = normalizeOcean({ ...SEA, choppiness: 0.6 });+ for (const { x, z, height } of drawnHeights(0.6)) {+ assert.ok(Math.abs(seaHeightAt(ocean, x, z, 0) - height) < 0.02, `at ${x}, ${z}: physics ${seaHeightAt(ocean, x, z, 0)}, drawn ${height}`);+ }+});