3D engine that AI agents drive
JavaScript 3d3d-engineagentic-aiai-agentsblenderclaude-codegame-enginegaussian-splattinggltfjavascriptmcpmcp-servermodel-context-protocolpath-tracerpath-tracingpbrrenderingthreejs-alternativewebgl2webgpu
Stop particles tunnelling through shared collider edges and name field rotation operands (#2390)
Nguyen Xuan An8c62a5231642
diff --git a/changes/2390.md b/changes/2390.mdnew file mode 100644index 00000000..06f8b726--- /dev/null+++ b/changes/2390.md@@ -0,0 +1 @@+- Particle colliders no longer let a path through a shared triangle edge or vertex fall through the mesh (#2390).diff --git a/changes/2391.md b/changes/2391.mdnew file mode 100644index 00000000..800061d9--- /dev/null+++ b/changes/2391.md@@ -0,0 +1 @@+- Field `rotate`, `align` and `transform` errors name the operand that is not a vector (#2391).diff --git a/src/field-rotation.js b/src/field-rotation.jsindex 960af553..5ae644c0 100644--- a/src/field-rotation.js+++ b/src/field-rotation.js@@ -58,8 +58,9 @@ export function alignEuler(direction) {return [Math.atan2(entry(2, 1), entry(2, 2)), Math.asin(-entry(2, 0)), Math.atan2(entry(1, 0), entry(0, 0))];}-function vectorOperand(value, label) {- if (value.kind !== 'vector') throw new TypeError(`${label} needs a vector.`);+// `operand` names the argument that is wrong, so a recipe with three vectors tells which one to fix.+function vectorOperand(value, operator, operand, example) {+ if (value.kind !== 'vector') throw new TypeError(`Field expression: ${operator} ${operand} needs a vector, such as ${example}.`);return value.parts;}@@ -81,23 +82,23 @@ function readVector(parts, index) {}function buildRotate([vector, axis, angle]) {- const vectorParts = vectorOperand(vector, 'rotate');- const axisParts = vectorOperand(axis, 'rotate');- const angleAt = requireScalar(angle, 'rotate');+ const vectorParts = vectorOperand(vector, 'rotate', 'vector', '[1, 0, 0]');+ const axisParts = vectorOperand(axis, 'rotate', 'axis', '[0, 1, 0]');+ const angleAt = requireScalar(angle, 'rotate angle');const part = sharedVector(index => rotateAboutAxis(readVector(vectorParts, index), readVector(axisParts, index), angleAt(index)));return vectorValue(part(0), part(1), part(2));}function buildAlign([direction]) {- const directionParts = vectorOperand(direction, 'align');+ const directionParts = vectorOperand(direction, 'align', 'direction', '[0, 1, 0]');const part = sharedVector(index => alignEuler(readVector(directionParts, index)));return vectorValue(part(0), part(1), part(2));}function buildTransform([point, move, euler, size]) {- const pointParts = vectorOperand(point, 'transform');+ const pointParts = vectorOperand(point, 'transform', 'point', '[1, 0, 0]');const moveParts = move === undefined ? null : partsOf(move);- const eulerParts = euler === undefined ? null : vectorOperand(euler, 'transform');+ const eulerParts = euler === undefined ? null : vectorOperand(euler, 'transform', 'euler', '[0, 0, 1.57]');const sizeParts = size === undefined ? null : partsOf(size);const part = sharedVector(index => {const scaled = readVector(pointParts, index).map((component, axis) => (sizeParts ? component * sizeParts[axis](index) : component));diff --git a/src/particle-collision.js b/src/particle-collision.jsindex f8e26b9d..2ec6de3c 100644--- a/src/particle-collision.js+++ b/src/particle-collision.js@@ -2,6 +2,9 @@// A path (not a point) is tested, so a fast particle cannot tunnel through a thin mesh.const PARALLEL_EPSILON = 1e-12;+// A path through the edge two triangles share lands on the boundary of both, and rounding can put it just outside each, so+// the barycentric bounds are widened by this much (relative to the triangle) and the earliest hit still wins.+const EDGE_TOLERANCE = 1e-9;/*** Packs the triangles of several meshes. `meshes` are { positions (world space), indices }. Each mesh keeps its own@@ -70,12 +73,12 @@ export function findFirstCollision(surface, from, to, contact) {const originY = from[1] - surface.triangles[base + 1];const originZ = from[2] - surface.triangles[base + 2];const along = (originX * pvecX + originY * pvecY + originZ * pvecZ) / determinant;- if (along < 0 || along > 1) continue;+ if (along < -EDGE_TOLERANCE || along > 1 + EDGE_TOLERANCE) continue;const qvecX = originY * edgeOneZ - originZ * edgeOneY;const qvecY = originZ * edgeOneX - originX * edgeOneZ;const qvecZ = originX * edgeOneY - originY * edgeOneX;const across = (directionX * qvecX + directionY * qvecY + directionZ * qvecZ) / determinant;- if (across < 0 || along + across > 1) continue;+ if (across < -EDGE_TOLERANCE || along + across > 1 + EDGE_TOLERANCE) continue;const fraction = (edgeTwoX * qvecX + edgeTwoY * qvecY + edgeTwoZ * qvecZ) / determinant;if (fraction < 0 || fraction > 1 || fraction >= contact.fraction) continue;const normalX = edgeOneY * edgeTwoZ - edgeOneZ * edgeTwoY;diff --git a/tests/field-math.test.js b/tests/field-math.test.jsindex e1181695..ef0615b8 100644--- a/tests/field-math.test.js+++ b/tests/field-math.test.js@@ -110,3 +110,12 @@ test('bounding_box and convex_hull run as recipe edits', () => {const { mesh } = applyMeshEdits(pointCloudMesh(cubePoints()), [{ op: 'convex_hull' }, { op: 'bounding_box' }]);assert.equal(mesh.faceCount, 6);});++test('rotate, align and transform name the operand that is not a vector', () => {+ assert.throws(() => evaluateVector({ rotate: [[1, 0, 0], 1, 1] }), /Field expression: rotate axis needs a vector, such as \[0, 1, 0\]/);+ assert.throws(() => evaluateVector({ rotate: [1, [0, 1, 0], 1] }), /Field expression: rotate vector needs a vector/);+ assert.throws(() => evaluateVector({ rotate: [[1, 0, 0], [0, 1, 0], [1, 1, 1]] }), /rotate angle needs a number/);+ assert.throws(() => evaluateVector({ align: [1] }), /Field expression: align direction needs a vector/);+ assert.throws(() => evaluateVector({ transform: [[1, 0, 0], [0, 0, 0], 1] }), /Field expression: transform euler needs a vector/);+ assert.throws(() => evaluateVector({ transform: [1] }), /Field expression: transform point needs a vector/);+});diff --git a/tests/particle-collision.test.js b/tests/particle-collision.test.jsindex 43e9d175..84a74d17 100644--- a/tests/particle-collision.test.js+++ b/tests/particle-collision.test.js@@ -5,6 +5,7 @@ import { createAgentScene } from '../src/agent-scene.js';import { AGENT_SCENE_CAPABILITIES } from '../src/agent-scene-capabilities.js';import { createCollisionSurface, findFirstCollision } from '../src/particle-collision.js';import { createParticleLaunches, particleStateAt } from '../src/particles.js';+import { icoSphere } from '../src/geometry.js';// A unit box is 1 m wide; the slab is scaled to 4 m x 0.2 m x 4 m, so its top face sits at y = 0.1.const SLAB_TOP = 0.1;@@ -233,3 +234,20 @@ test('the default bounce with colliders reads as debris', () => {const rise = Math.max(...heights.slice(65, 100)) - SLAB_TOP;assert.ok(rise > 0 && rise < 0.3, `default bounce rebounds ${rise} m from a 1.9 m drop`);});++test('a path through a vertex or an edge midpoint of a scaled mesh always hits it', () => {+ const geometry = icoSphere(2);+ const positions = Float64Array.from(geometry.positions, value => value * 0.37);+ const surface = createCollisionSurface([{ positions, indices: geometry.indices }]);+ const drops = [];+ for (let vertex = 0; vertex < positions.length; vertex += 3) drops.push([positions[vertex], positions[vertex + 2]]);+ for (let corner = 0; corner < geometry.indices.length; corner += 3) {+ for (const [first, second] of [[0, 1], [1, 2], [2, 0]]) {+ const start = geometry.indices[corner + first] * 3;+ const end = geometry.indices[corner + second] * 3;+ drops.push([(positions[start] + positions[end]) / 2, (positions[start + 2] + positions[end + 2]) / 2]);+ }+ }+ const leaks = drops.filter(([x, z]) => !findFirstCollision(surface, [x, 6, z], [x, -6, z], { fraction: Infinity, normal: [0, 1, 0], onFloor: false }));+ assert.equal(leaks.length, 0, `${leaks.length} of ${drops.length} drops fell through the mesh`);+});