3D engine that AI agents drive
Test the jelly ball's fall and impact, and say what pressure below 1 does (part of #696)
Nguyen Xuan An148c9f8f420f
diff --git a/docs/physics.md b/docs/physics.mdindex 2d226261..02e7a42c 100644--- a/docs/physics.md+++ b/docs/physics.md@@ -431,7 +431,7 @@ A recipe `cloth` section records deformable sheets with a position-based solver- `{ "armature": "rig", "radiusFraction": 0.2, "exclude": ["head"] }` adds one capsule per bone, head to tail, the radius a fraction (0.02 to 0.5, default 0.2) of the bone length as the ragdoll fits it. A bone with no length is skipped.- A sheet's `stiffnessGroup: "sleeve"` names a vertex group whose weights scale stretch and bend stiffness per vertex; a constraint is as stiff as the softer of its two ends, and a weight under 0.05 counts as 0.05 so the vertex does not drift off.- `selfCollision: true` makes the sheet's own particles push each other apart, so cloth folded over itself stays in layers instead of passing through. The contact radius is 0.9 of half the shortest edge, a spatial hash keeps it near linear, two particles joined by an edge are skipped, and a sheet over 2000 particles fails with the count and the fix. It is per particle, so a layer can still slip between particles on a mesh much coarser than the fold. The fold test (`tests/soft-body.test.js`) keeps every unjoined pair at least 0.8 of the contact distance apart; without it the same fold interpenetrates.- - `softBody: { "shape": 0.2, "pressure": 1 }` turns the sheet node into a jelly: `shape` (0 to 1) pulls every particle toward the best rigid fit of the rest shape (shape matching, rotation extracted with a warm-started iteration), and `pressure` (0 to 3) is a volume constraint that holds that multiple of the rest volume. Pressure needs a closed mesh (every edge in two triangles) and fails naming the open edge otherwise; set `pressure: 0` to keep only shape matching. A box or sphere geometry is closed. It rests on the colliders like cloth and bakes to the same frames every run.+ - `softBody: { "shape": 0.2, "pressure": 1 }` turns the sheet node into a jelly: `shape` (0 to 1) pulls every particle toward the best rigid fit of the rest shape (shape matching, rotation extracted with a warm-started iteration), and `pressure` (0 to 3) is a volume constraint that holds that multiple of the rest volume. Use 1 for a jelly that keeps its volume and squashes on impact; below 1 the body deflates to that volume before it lands (0.85 is a body 15 percent smaller from the first frame), above 1 it inflates. Pressure needs a closed mesh (every edge in two triangles) and fails naming the open edge otherwise; set `pressure: 0` to keep only shape matching. A box or sphere geometry is closed. It rests on the colliders like cloth and bakes to the same frames every run.- A sheet on a mesh geometry can hold chosen vertices with `pinGroup: "hem"`, the name of a vertex group made by a `vertex_group` mesh edit; vertices of weight 0.5 or more are pinned, on their own or together with `pin`. An unknown name fails and lists the groups the mesh has.- A sheet's `pinTo: { "node": "chest", "weight": 1 }` makes the pinned edges (`pin`) ride that node, keeping their rest offset in its space. Weight 1 holds them on the node; 0.01 to under 1 pulls them toward it by that fraction on every pass, so they sag and lag.- Up to 64 moving balls and capsules, counting every bone. The node poses come from the recipe's motion tracks and IK; a physics ragdoll or body is not followed. Points are pushed out of the capsules, not whole triangles, so a coarse sheet can still cut a corner of a thin limb: use a finer grid than the limb is wide. A cape on the reference walker (24 by 24 grid, 5 s, about 2.5 s to bake on the dev machine) stays outside every limb capsule in `tests/cloth-moving.test.js`.diff --git a/tests/soft-body.test.js b/tests/soft-body.test.jsindex e19fb569..1d00045a 100644--- a/tests/soft-body.test.js+++ b/tests/soft-body.test.js@@ -118,6 +118,35 @@ test('a jelly cube lands on the ground, keeps its volume and comes to rest', ()assert.ok(fallen > 0);});+test('a jelly ball falls as fast as free fall, then flattens and bulges on impact with its volume kept', async () => {+ const { icoSphere } = await import('../src/geometry.js');+ const { weldVertices } = await import('../src/cloth.js');+ const geometry = icoSphere(2);+ const lifted = Float32Array.from(geometry.positions, (value, index) => (index % 3 === 1 ? value * 0.5 + 1.4 : value * 0.5));+ const welded = weldVertices(lifted, geometry.indices);+ const particles = new Float32Array(welded.representatives.length * 3);+ welded.representatives.forEach((vertex, particle) => particles.set(lifted.subarray(vertex * 3, vertex * 3 + 3), particle * 3));+ const restVolume = signedVolume(particles, welded.triangles);+ const recording = bakeCloth(particles, welded.triangles, { colliders: [GROUND], softBody: { shape: 0.03, pressure: 1 }, bend: 0, damping: 0.3, frameCount: 61 });+ const measure = frame => {+ const state = frameAt(recording, frame);+ const low = [Infinity, Infinity, Infinity];+ const high = [-Infinity, -Infinity, -Infinity];+ state.forEach((value, index) => {+ low[index % 3] = Math.min(low[index % 3], value);+ high[index % 3] = Math.max(high[index % 3], value);+ });+ return { bottom: low[1], height: high[1] - low[1], width: high[0] - low[0], volume: signedVolume(state, welded.triangles) / restVolume };+ };+ // Free fall from a ball bottom at 0.9 m reaches the floor (thickness 0.01) at about 0.43 s, frame 26.+ assert.ok(measure(24).bottom > 0.05, 'still falling two frames before contact');+ assert.ok(measure(26).bottom < 0.011, 'touches the floor at frame 26');+ const impact = measure(29);+ assert.ok(impact.height < 0.9, `height ${impact.height} m flattens below 0.9 m`);+ assert.ok(impact.width > 1.005, `width ${impact.width} m bulges past 1 m`);+ assert.ok(Math.abs(impact.volume - 1) < 0.02, `volume ratio ${impact.volume} stays at 1`);+});+test('pressure keeps a jelly full while a soft body without it collapses further', () => {const body = cube(4, 0.5, 1);const settle = softBody => {