3D engine that AI agents drive
JavaScript 3d3d-engineagentic-aiai-agentsblenderclaude-codegame-enginegaussian-splattinggltfjavascriptmcpmcp-servermodel-context-protocolpath-tracerpath-tracingpbrrenderingthreejs-alternativewebgl2webgpu
Keep a cloth drag field stable at any accepted strength (#2424)
Nguyen Xuan An6489784d0adc
diff --git a/changes/2424.md b/changes/2424.mdnew file mode 100644index 00000000..f5d66d23--- /dev/null+++ b/changes/2424.md@@ -0,0 +1 @@+- A `drag` force field on cloth no longer blows the sheet up at strengths of 500 and above: a drag slows a particle to rest and never carries its velocity past rest (#2424)diff --git a/src/cloth-fields.js b/src/cloth-fields.jsindex 921b191a..a8430476 100644--- a/src/cloth-fields.js+++ b/src/cloth-fields.js@@ -1,23 +1,32 @@import { bakeCloth } from './cloth.js';import { addFieldAcceleration } from './force-fields.js';+// A drag field slows a particle by `strength * dt` of its speed each substep. Past 1 that overshoots and the speed grows+// with every substep, so a drag can bring a component to rest but never carry it past rest.+function limitedDragChange(velocity, dragChange) {+ return velocity * (velocity + dragChange) < 0 ? -velocity : dragChange;+}+// Adds every field's acceleration to the velocity of every particle for one substep.function createFieldApplier(fields) {const position = [0, 0, 0];const velocity = [0, 0, 0];const acceleration = [0, 0, 0];+ const dragAcceleration = [0, 0, 0];return (positions, velocities, time, substepSeconds) => {for (let particle = 0; particle < positions.length / 3; particle++) {for (let axis = 0; axis < 3; axis++) {position[axis] = positions[particle * 3 + axis];velocity[axis] = velocities[particle * 3 + axis];acceleration[axis] = 0;+ dragAcceleration[axis] = 0;}for (const field of fields) {- addFieldAcceleration(field, position, velocity, time, acceleration);+ addFieldAcceleration(field, position, velocity, time, field.type === 'drag' ? dragAcceleration : acceleration);}for (let axis = 0; axis < 3; axis++) {- velocities[particle * 3 + axis] += acceleration[axis] * substepSeconds;+ const dragChange = limitedDragChange(velocity[axis], dragAcceleration[axis] * substepSeconds);+ velocities[particle * 3 + axis] += acceleration[axis] * substepSeconds + dragChange;}}};diff --git a/tests/cloth-fields-drag.test.js b/tests/cloth-fields-drag.test.jsnew file mode 100644index 00000000..56194ec3--- /dev/null+++ b/tests/cloth-fields-drag.test.js@@ -0,0 +1,33 @@+import test from 'node:test';+import assert from 'node:assert/strict';+import { bakeClothWithFields } from '../src/cloth-fields.js';+import { normalizeForceFields } from '../src/force-fields.js';++const POSITIONS = Float64Array.from([0, 0, 0, 0.1, 0, 0, 0, 0, 0.1, 0.1, 0, 0.1]);+const TRIANGLES = Uint32Array.from([0, 2, 1, 1, 2, 3]);++function fallHeights(strength) {+ const fields = normalizeForceFields([{ type: 'drag', strength }], 'Cloth force field');+ const frames = bakeClothWithFields(POSITIONS, TRIANGLES, { fields, frameCount: 30, framesPerSecond: 60, gravity: [0, -9.81, 0] });+ const flat = Array.from(frames.frames ?? frames);+ return Array.from({ length: 30 }, (unused, frame) => flat[frame * POSITIONS.length + 1]);+}++test('a drag field at the strongest accepted strength keeps cloth finite and never lifts it', () => {+ const heights = fallHeights(1000);+ assert.ok(heights.every(Number.isFinite), 'heights stay finite');+ assert.ok(heights.every(height => height <= 1e-6), `a drag field must not push the sheet up; highest ${Math.max(...heights)}`);+});++test('a drag field never lets cloth rise against gravity at any accepted strength', () => {+ for (const strength of [300, 500, 1000]) {+ const heights = fallHeights(strength);+ assert.ok(heights.every(height => height <= 1e-6), `strength ${strength}: highest ${Math.max(...heights)}`);+ }+});++test('a gentle drag still slows the fall in proportion', () => {+ const free = fallHeights(0)[29];+ const dragged = fallHeights(10)[29];+ assert.ok(dragged > free && dragged < 0, `free ${free}, dragged ${dragged}`);+});