3D engine that AI agents drive
JavaScript 3d3d-engineagentic-aiai-agentsblenderclaude-codegame-enginegaussian-splattinggltfjavascriptmcpmcp-servermodel-context-protocolpath-tracerpath-tracingpbrrenderingthreejs-alternativewebgl2webgpu
Fold a two-bone limb onto a root target and warn target-at-root (#2076)
Nguyen Xuan Anedd313339789
diff --git a/changes/2076.md b/changes/2076.mdnew file mode 100644index 00000000..0ab2757d--- /dev/null+++ b/changes/2076.md@@ -0,0 +1 @@+- A two-bone IK target on or next to the root joint now folds the limb and reaches it when the bones are equal, and `motionIk[n].warnings` carries `target-at-root` with the distance to move the target off the root.diff --git a/docs/agent-motion.md b/docs/agent-motion.mdindex 2269fcc9..b1c8f331 100644--- a/docs/agent-motion.md+++ b/docs/agent-motion.md@@ -277,6 +277,7 @@ the foot short of its target and `reached` turns false. `motionIk[n].limited` li| --- | --- || `hyperextended` | The middle joint is within 5 degrees of straight: the target is at or past the limb's reach || `wrong-bend` | The middle joint bends opposite to its natural side, which a pole on the wrong side produces while the foot still arrives |+| `target-at-root` | A two-bone target is within 0.1 percent of the limb's length from the root joint. The limb folds along the way it already points; move the target at least that far off the root. Unequal bones cannot fold fully, so `reached` stays false |The natural side is `bendToward` (a world direction, such as `[0, 0, 1]` for a knee on a character that faces +z) or, whenthat is absent, the way the limb is bent in its rest pose. A limb that rests perfectly straight and has no `bendToward` isdiff --git a/src/skeleton-pose.js b/src/skeleton-pose.jsindex e6310608..d6775535 100644--- a/src/skeleton-pose.js+++ b/src/skeleton-pose.js@@ -11,7 +11,9 @@ import {// forward. Quaternions are XYZW; IK targets and poles are in the rig's world space.const NEAR_ZERO = 1e-9;-const TO_RADIANS = Math.PI / 180;+// A two-bone target closer to the root than this fraction of the limb's length warns that it sits on the root.+const ROOT_TARGET_FRACTION = 1e-3;+const TO_RADIANS =Math.PI / 180;const IDENTITY_ROTATION = Object.freeze([0, 0, 0, 1]);function jointResolver(rig, prepared) {@@ -454,10 +456,12 @@ function solveTwoBone(solver, time, context) {const lowerLength = vec3.distance(middlePosition, startEnd);const toTarget = vec3.subtract(new Float64Array(3), target, rootPosition);const targetDistance = vec3.length(toTarget);- if (targetDistance < NEAR_ZERO || upperLength < NEAR_ZERO || lowerLength < NEAR_ZERO) {+ if (upperLength < NEAR_ZERO || lowerLength < NEAR_ZERO) {return { reached: false, error: vec3.distance(startEnd, target), warnings: [], limited: [] };}- const direction = vec3.scale(new Float64Array(3), toTarget, 1 / targetDistance);+ const direction = targetDistance < NEAR_ZERO+ ? foldDirection(rootPosition, middlePosition, startEnd)+ : vec3.scale(new Float64Array(3), toTarget, 1 / targetDistance);// Stop just short of the limits: a fully straight limb has no defined bend plane for the next frame.const margin = (upperLength + lowerLength) * 1e-4;const reachLimit = upperLength + lowerLength - margin;@@ -502,7 +506,20 @@ function solveTwoBone(solver, time, context) {setWorldRotation(solver.end, quat.multiply(new Float64Array(4), turn, state.rotations[solver.end]), weight);}const error = vec3.distance(state.positions[solver.end], target);- return { reached: error <= margin * 2 + NEAR_ZERO, error, warnings: bendWarnings(solver, state, prepared), limited };+ const warnings = bendWarnings(solver, state, prepared);+ const rootTargetDistance = (upperLength + lowerLength) * ROOT_TARGET_FRACTION;+ if (targetDistance < rootTargetDistance) {+ warnings.unshift({ code: 'target-at-root', message: `The target is ${Number(targetDistance.toFixed(6))} m from ${prepared.joints[solver.root].name}, so the limb folds with no direction from the target. Move the target at least ${Number(rootTargetDistance.toFixed(4))} m off ${prepared.joints[solver.root].name}.` });+ }+ return { reached: error <= margin * 2 + NEAR_ZERO, error, warnings, limited };+}++/** Where a limb folds when the target sits on its root: along the way the end already points, else toward the middle joint. */+function foldDirection(rootPosition, middlePosition, endPosition) {+ const toEnd = vec3.subtract(new Float64Array(3), endPosition, rootPosition);+ if (vec3.length(toEnd) >= NEAR_ZERO) return vec3.normalize(toEnd, toEnd);+ const toMiddle = vec3.subtract(new Float64Array(3), middlePosition, rootPosition);+ return vec3.normalize(toMiddle, toMiddle);}/** Turn a joint, or a chain sharing the turn evenly, so its forward axis points at the target. */diff --git a/tests/skeleton-pose-limits.test.js b/tests/skeleton-pose-limits.test.jsindex 124ba433..aae39d0c 100644--- a/tests/skeleton-pose-limits.test.js+++ b/tests/skeleton-pose-limits.test.js@@ -100,6 +100,41 @@ test('a limb stretched to its reach warns about hyperextension', () => {assert.ok(result.warnings.some(warning => warning.code === 'hyperextended'));});+function jointPositionsAreFinite(pose) {+ return ['hip', 'knee', 'ankle'].every(name => Array.from(pose.jointWorld(name).position).every(Number.isFinite));+}++test('a target exactly on the root folds an equal-bone limb onto it and says so', () => {+ const pose = solved({ target: [0, 1, 0], pole: [0, 0, 2] });+ const [result] = pose.ikResults;+ assert.equal(result.reached, true);+ assert.ok(jointPositionsAreFinite(pose));+ const warning = result.warnings.find(entry => entry.code === 'target-at-root');+ assert.ok(warning);+ assert.match(warning.message, /Move the target at least 0\.001 m off hip/);+});++test('a target a hair from the root warns the same way and stays deterministic', () => {+ const near = solved({ target: [0, 1 - 1e-5, 0], pole: [0, 0, 2] });+ assert.equal(near.ikResults[0].reached, true);+ assert.ok(near.ikResults[0].warnings.some(entry => entry.code === 'target-at-root'));+ const again = solved({ target: [0, 1 - 1e-5, 0], pole: [0, 0, 2] });+ assert.deepEqual(Array.from(again.jointWorld('ankle').position), Array.from(near.jointWorld('ankle').position));+ const clear = solved({ target: [0, 0.99, 0], pole: [0, 0, 2] });+ assert.ok(!clear.ikResults[0].warnings.some(entry => entry.code === 'target-at-root'));+});++test('a target on the root of an unequal limb cannot be reached but names the cause', () => {+ const rig = createLegRig();+ rig.joints[2].translation = [0, -0.25, 0];+ const pose = createSkeletonPose({ rig, ik: [{ ...LEG, target: [0, 1, 0], pole: [0, 0, 2] }] });+ pose.apply(0);+ const [result] = pose.ikResults;+ assert.equal(result.reached, false);+ assert.ok(jointPositionsAreFinite(pose));+ assert.ok(result.warnings.some(entry => entry.code === 'target-at-root'));+});+test('seeking gives the same limited pose as playing to that time', () => {const definition = {target: { },