3D engine that AI agents drive
Jolt adapter: read flat convex hull points and free the world when setup throws
Nguyen Xuan Ana41b78727ffa
diff --git a/changes/771-jolt-review-fixes.md b/changes/771-jolt-review-fixes.mdnew file mode 100644index 00000000..7d8d999d--- /dev/null+++ b/changes/771-jolt-review-fixes.md@@ -0,0 +1 @@+- The jolt adapter builds convexHull colliders from the flat xyz point array and frees its world when setup throws.diff --git a/src/physics-jolt.js b/src/physics-jolt.jsindex 054d5657..13500a03 100644--- a/src/physics-jolt.js+++ b/src/physics-jolt.js@@ -137,7 +137,9 @@ function createJoltWorld(prepared) {shapeSettings = new Jolt.CylinderShapeSettings(collider.halfHeight, collider.radius, Math.min(MAX_CONVEX_RADIUS, collider.halfHeight, collider.radius) * 0.5);} else {shapeSettings = new Jolt.ConvexHullShapeSettings();- for (const point of collider.points) shapeSettings.mPoints.push_back(own(new Jolt.Vec3(...point)));+ for (let index = 0; index < collider.points.length; index += 3) {+ shapeSettings.mPoints.push_back(own(new Jolt.Vec3(collider.points[index], collider.points[index + 1], collider.points[index + 2])));+ }}if (collider.density !== undefined) shapeSettings.mDensity = collider.density;const hasOffset = collider.translation.some(component => component !== 0) || collider.rotation.some((component, axis) => component !== [0, 0, 0, 1][axis]);@@ -157,7 +159,16 @@ function createJoltWorld(prepared) {return shapeResult.Get();}- prepared.bodies.forEach((body, bodyIndex) => {+ function freeWorld() {+ for (const bodyId of bodyIds) {+ bodyInterface.RemoveBody(bodyId);+ bodyInterface.DestroyBody(bodyId);+ }+ Jolt.destroy(jolt);+ for (const object of owned.reverse()) Jolt.destroy(object);+ }++ function addBody(body, bodyIndex) {const colliderIndex = colliderOfBody[bodyIndex];const collider = colliderIndex >= 0 ? prepared.colliders[colliderIndex] : undefined;let shape;@@ -196,7 +207,15 @@ function createJoltWorld(prepared) {bodyIds.push(created.GetID());bodyOfIdIndex.set(created.GetID().GetIndex(), created);if (collider) colliderOfBodyIndex.set(created.GetID().GetIndex(), colliderIndex);- });+ }++ // A throw while building must not leave the Jolt interface, bodies or owned objects alive: no caller holds the world yet.+ try {+ prepared.bodies.forEach(addBody);+ } catch (error) {+ freeWorld();+ throw error;+ }// Jolt reports a contact as removed when a body falls asleep; Rapier does not. Track which pairs are touching so a// sleeping body neither ends nor restarts its contact, and every adapter reports the same started and stopped pairs.@@ -408,14 +427,7 @@ function createJoltWorld(prepared) {}return found;},- free() {- for (const bodyId of bodyIds) {- bodyInterface.RemoveBody(bodyId);- bodyInterface.DestroyBody(bodyId);- }- Jolt.destroy(jolt);- for (const object of owned.reverse()) Jolt.destroy(object);- },+ free: freeWorld,};}diff --git a/tests/physics-jolt.test.js b/tests/physics-jolt.test.jsindex b7883fff..53a6bb8e 100644--- a/tests/physics-jolt.test.js+++ b/tests/physics-jolt.test.js@@ -197,3 +197,58 @@ test('the same 500-box scene runs live on both engines and records the step timetest('loadJoltEngine resolves once', async () => {assert.equal(await loadJoltEngine(), await loadJoltEngine());});++const TETRAHEDRON_POINTS = [-0.5, 0, -0.5, 0.5, 0, -0.5, 0, 0, 0.5, 0, 0.8, 0];+const COLLINEAR_POINTS = [0, 0, 0, 1, 1, 1, 2, 2, 2, 3, 3, 3];++function hullDefinition(points, translation = [0, 0, 0], mass = 1) {+ return {+ bodies: [+ { name: 'floor', type: 'fixed', translation: [0, -0.5, 0] },+ { name: 'rock', translation: [0, 0.3, 0] },+ ],+ colliders: [+ { name: 'ground', body: 'floor', shape: 'cuboid', halfExtents: [10, 0.5, 10] },+ { name: 'hull', body: 'rock', shape: 'convexHull', points, mass, translation },+ ],+ };+}++test('a convex hull rests on the floor under jolt, with and without a collider offset', async () => {+ for (const offset of [[0, 0, 0], [0, 0.1, 0]]) {+ const physics = await createPhysicsSystem(hullDefinition(TETRAHEDRON_POINTS, offset), { adapter: joltAdapter });+ stepMany(physics, 180);+ const pose = physics.getPose('rock');+ const height = pose.translation[1];+ assert.ok(Number.isFinite(height) && height < 0.3 && height > -0.2, `the hull settled on the floor, got y ${height}`);+ physics.dispose();+ }+});++test('a collinear convex hull fails with a RangeError that names the collider', async () => {+ await assert.rejects(createPhysicsSystem(hullDefinition(COLLINEAR_POINTS, [0, 0, 0], undefined), { adapter: joltAdapter }), error => {+ assert.ok(error instanceof RangeError);+ assert.match(error.message, /Collider "hull" could not be built by jolt.*span a volume/);+ return true;+ });+});++test('a setup that throws destroys the jolt interface and every owned object', async () => {+ const Jolt = await loadJoltEngine();+ const originalDestroy = Jolt.destroy;+ const destroyed = [];+ Jolt.destroy = object => {+ destroyed.push(object);+ return originalDestroy.call(Jolt, object);+ };+ try {+ for (let attempt = 0; attempt < 5; attempt++) {+ destroyed.length = 0;+ await assert.rejects(createPhysicsSystem(hullDefinition(COLLINEAR_POINTS, [0, 0, 0], undefined), { adapter: joltAdapter }), /span a volume/);+ assert.ok(destroyed.some(object => object instanceof Jolt.JoltInterface), 'the interface is destroyed');+ assert.ok(destroyed.length > 10, `owned objects are destroyed, got ${destroyed.length}`);+ }+ } finally {+ Jolt.destroy = originalDestroy;+ }+});