Skip to content

armgit / 67.js

Public Imported from GitHub
0

3D engine that AI agents drive

Test the ragdoll bake round trip through GLB for every bone and document it (#697)

Nguyen Xuan Anab13b7e9cabf
diff --git a/docs/physics.md b/docs/physics.md
index 6e98197d..0988b193 100644
--- a/docs/physics.md
+++ b/docs/physics.md
@@ -405,6 +405,7 @@ For a recipe armature, `ragdoll.armature` names it. The optional `density`, `fri
- Rigs need uniform scale above the bones (a scale of 100 on an armature node is fine). A bone with no length, more than 48 bones, a bone with a translation or rotation track in the same recipe, or a missing floor body in a scene recipe is refused or falls forever.
- On an asset, physics wins over motion tracks and `ik` on joints.
- `scene_inspect` returns `physics.ragdoll`: `bones`, `lowestY`, `highestY`, `fastestFinalSpeed` and `restsAtTime` (null while still moving). Read those instead of a picture.
+- A fall exports as a glTF clip: `bake-motion` over every bone and the fixed bodies, with a tolerance, replaces the recording with LINEAR keys, and `exportGlb` writes them as one animation. `tests/motion-bake.test.js` bakes and simplifies a ragdoll fall, re-imports the GLB with `GLTFLoader` and plays it with an `AnimationMixer`; every bone stays within 0.05 m and 0.05 in the rotation matrix of the live simulation, sampled every 0.1 s over the first 2.9 s.
- Rapier stays an optional peer. A recipe without `physics` loads none of this code.
## Recipe cloth
diff --git a/tests/motion-bake.test.js b/tests/motion-bake.test.js
index 0e1d1011..b39706de 100644
--- a/tests/motion-bake.test.js
+++ b/tests/motion-bake.test.js
@@ -152,15 +152,21 @@ test('a ragdoll fall bakes to keys, simplifies and exports as a clip a viewer pl
});
const mixer = new AnimationMixer(gltf.scene);
mixer.clipAction(gltf.animations[0]).play();
- const hips = skinned[0].skeleton.bones.find(bone => bone.name === 'hips');
- const hipsIndex = skinned[0].skeleton.bones.indexOf(hips);
- for (const time of [0.5, 1.5, 2.9]) {
+ const bones = skinned[0].skeleton.bones;
+ const POSITION_TOLERANCE = 0.05;
+ const ROTATION_TOLERANCE = 0.05;
+ for (const time of frameTimes(2.9, 0.1)) {
mixer.setTime(time);
gltf.scene.updateMatrixWorld(true);
- const liveMatrix = jointWorlds(live, time).slice(hipsIndex * 16, hipsIndex * 16 + 16);
- const viewerPosition = hips.getWorldPosition(new Vector3());
- const difference = Math.hypot(viewerPosition.x - liveMatrix[12], viewerPosition.y - liveMatrix[13], viewerPosition.z - liveMatrix[14]);
- assert.ok(difference < 0.05, `hips at ${time} are ${difference} off in the viewer`);
+ const liveJoints = jointWorlds(live, time);
+ bones.forEach((bone, index) => {
+ const liveMatrix = liveJoints.slice(index * 16, index * 16 + 16);
+ const viewerMatrix = bone.matrixWorld.elements;
+ const positionError = Math.hypot(viewerMatrix[12] - liveMatrix[12], viewerMatrix[13] - liveMatrix[13], viewerMatrix[14] - liveMatrix[14]);
+ const rotationError = largestDifference(Array.from(viewerMatrix).slice(0, 11), liveMatrix.slice(0, 11));
+ assert.ok(positionError < POSITION_TOLERANCE, `${bone.name} at ${time} is ${positionError} m off in the viewer`);
+ assert.ok(rotationError < ROTATION_TOLERANCE, `${bone.name} at ${time} is ${rotationError} off in rotation in the viewer`);
+ });
}
});