Skip to content

armgit / raea

Public Imported from GitHub
0

Render Anything Engine: tell your AI what to make. Videos, motion graphics, 3D and games as React and TypeScript, rendered on your own GPU. Rust + Bevy, macOS, Linux and Windows.

HG9b, HG6c: a thing passed from hand to hand, a thing moved where it is and left (a drawer), a throw's wind-up and follow-through; forearms, knees and the carried thing out of the scene's solids. Each hand has its own part in a take (when it starts, is on, lets go), so hands come and go separately. The body bends for a thing only when it is lower than 0.88 of an arm below the shoulders. The body crate's own tests (limits, the hand prior) are its own; 29 tests in all

shinyobjectzf3f720700175
diff --git a/docs/hands.feature b/docs/hands.feature
index f00da1f6..4585104c 100644
--- a/docs/hands.feature
+++ b/docs/hands.feature
@@ -249,13 +249,40 @@ Feature: Procedural arms and hands: a character reaches for, takes, holds, carri
# it, both palms still on. Seen on teal with 14 kg. Not done: a throw's wind-up and follow-through.
Then a heavy thing is held low and close, lifted slower, and leant against
+ @done
+ Scenario: HG9b. Passing a thing, moving a thing where it is, and a throw's wind-up
+ # 11-14, built. Each hand now has its own part in a take (when it leaves the body's motion, when it is on,
+ # when it lets go), so hands can come and go separately. `passing(at)` (a take's `pass`): a thing held in
+ # one hand is brought to the middle, the other hand takes it on its far side (`Hold::across`), and it is
+ # carried on that side; one palm or the other is within 15 mm of its hold at every frame, both at the
+ # pass, and the thing stays upright. `slide` and `Release::Leave` (a take's `slide`, `leave`): a thing
+ # that isn't carried goes through places where it is, the hands staying on, and is left there; a drawer's
+ # front pulled 18 cm keeps the palm within 3 cm of its handle and never leaves its runners. A throw draws
+ # the thing back 18 cm against the throw over its last 0.4 s and brings it through, and the hands follow
+ # it out 20 cm before coming back. Seen on teal: the cup passed right to left, a drawer pulled open, the
+ # box drawn back before it is thrown. The arm let go swings back in half a second, up to 26 degrees a
+ # frame at 30 a second, which may read as abrupt. Not done: a thing passed to another character.
+ Then a thing changes hands with somebody always holding it, a drawer is pulled and left, a throw winds up
+
+ @done
+ Scenario: HG6c. Forearms, knees and the carried thing out of the scene's solids
+ # 11-14, built. An elbow swing that puts the elbow or the middle of the forearm within 3 to 4 cm of a solid
+ # counts as far past its range, so another swing is chosen. A squat whose knee would be within 7 cm of a
+ # solid sits back from it, 4 cm at a time. A lifted thing rises by how deep its corners would be in a
+ # solid. With a table 38 cm in front, a box lifted from the floor onto it: no knee or elbow is in the
+ # table at any frame, and no corner of the box is over 5 mm into it on the way. The body bends for a
+ # thing only when it is lower than 0.88 of an arm below the shoulders (0.7 squatted for a drawer handle
+ # a hanging arm reaches). Not done: the body's own shape (an arm through the torso, a thing through
+ # the thighs), which sdk/body's shape.ts knows and the Rust does not.
+ Then the limbs and the thing clear a table as the hands, head and chest do
+
Rule: Next, in order
@needs:HG5
Scenario: HG6. The whole body out of itself and the scene
- # HG6a keeps the hands out of solids and HG6b the head and chest; the arms are inside their ranges.
- Then the forearms, the elbows, the knees and the held thing stay out of the body's own shape (sdk/body's
- shape.ts and shell.ts) and the scene's solids, by the least move
+ # HG6a, HG6b and HG6c keep the hands, head, chest, forearms, knees and the thing out of the scene's solids.
+ Then the arms and the held thing stay out of the body's own shape (sdk/body's shape.ts and shell.ts), by
+ the least move
And one copy of the limits serves both the TypeScript and the Rust
@needs:HG5
diff --git a/sdk/character/src/take.ts b/sdk/character/src/take.ts
index c5508997..c4adda47 100644
--- a/sdk/character/src/take.ts
+++ b/sdk/character/src/take.ts
@@ -50,6 +50,13 @@ export interface TakeSpec {
path?: { t: number; carry: Vec3; tilt?: Quat }[];
/** how heavy it is, kilograms: held lower and closer, lifted slower, the trunk leaning back against it */
weight?: number;
+ /** when a thing held in one hand is passed to the other, seconds */
+ pass?: number;
+ /** a thing that isn't carried but moved where it is (a drawer, a door, a lever): where it is at times after the
+ * grab, in the frame the body is posed in; the hands stay on it */
+ slide?: { t: number; at: Vec3; turn?: Quat }[];
+ /** when the hands leave the thing where it is (after a slide), seconds */
+ leave?: number;
/** the scene's solids the hands keep out of (a table's top, a shelf) */
avoid?: TakeThing[];
/** set down: moved to `place` from `at`, standing there from `down` */
diff --git a/sdk/raea/src/native-calls.ts b/sdk/raea/src/native-calls.ts
index 601f9c51..334a0664 100644
--- a/sdk/raea/src/native-calls.ts
+++ b/sdk/raea/src/native-calls.ts
@@ -41,7 +41,7 @@ export interface NativeCalls {
/** a hand shaped by a few numbers of a model of real hand poses (none: the average relaxed hand; the first opens it, positive, and closes it), on a pose; and that hand read back: its numbers and how odd it is (0: the average hand) */
"character.hand": { args: { joints: { name: string; parent: number; head: [number, number, number]; tail: [number, number, number] }[]; side: "L" | "R"; z?: number[]; pose?: { root: [number, number, number]; turns: [number, number, number, number][] } }; answer: { pose: { root: [number, number, number]; turns: [number, number, number, number][] }; z: number[]; odd: number } };
/** a frame of a character taking a thing with its hands (reach, grab, lift, carry, put down or throw): the pose to draw and where the thing is, with how far a wrist missed (metres) and how far the body was turned from its own motion (degrees) */
- "character.take": { args: { joints: { name: string; parent: number; head: [number, number, number]; tail: [number, number, number] }[]; t: number; base: { t: number; root: [number, number, number]; turns: [number, number, number, number][] }[]; thing: { ball?: number; box?: [number, number, number]; capsule?: [number, number]; cylinder?: [number, number]; at: [number, number, number]; turn?: [number, number, number, number] }; start: number; grab?: number; carried?: number; carry?: [number, number, number]; hand?: "L" | "R"; lower?: number; holds?: { side: "L" | "R"; at: [number, number, number]; normal: [number, number, number]; along?: [number, number, number]; shape?: number[] }[]; path?: { t: number; carry: [number, number, number]; tilt?: [number, number, number, number] }[]; weight?: number; avoid?: { ball?: number; box?: [number, number, number]; capsule?: [number, number]; cylinder?: [number, number]; at: [number, number, number]; turn?: [number, number, number, number] }[]; put?: { at: number; down: number; place: [number, number, number] }; throw?: { at: number; velocity: [number, number, number]; floor?: number } }; answer: { pose: { root: [number, number, number]; turns: [number, number, number, number][] }; thing: { at: [number, number, number]; turn: [number, number, number, number] }; phase: "Before" | "Reach" | "Lift" | "Carry" | "Release" | "After"; hands: 1 | 2; miss: number; moved: number; limited: boolean; stand: { at: [number, number, number]; yaw: number } } };
+ "character.take": { args: { joints: { name: string; parent: number; head: [number, number, number]; tail: [number, number, number] }[]; t: number; base: { t: number; root: [number, number, number]; turns: [number, number, number, number][] }[]; thing: { ball?: number; box?: [number, number, number]; capsule?: [number, number]; cylinder?: [number, number]; at: [number, number, number]; turn?: [number, number, number, number] }; start: number; grab?: number; carried?: number; carry?: [number, number, number]; hand?: "L" | "R"; lower?: number; holds?: { side: "L" | "R"; at: [number, number, number]; normal: [number, number, number]; along?: [number, number, number]; shape?: number[] }[]; path?: { t: number; carry: [number, number, number]; tilt?: [number, number, number, number] }[]; weight?: number; pass?: number; slide?: { t: number; at: [number, number, number]; turn?: [number, number, number, number] }[]; leave?: number; avoid?: { ball?: number; box?: [number, number, number]; capsule?: [number, number]; cylinder?: [number, number]; at: [number, number, number]; turn?: [number, number, number, number] }[]; put?: { at: number; down: number; place: [number, number, number] }; throw?: { at: number; velocity: [number, number, number]; floor?: number } }; answer: { pose: { root: [number, number, number]; turns: [number, number, number, number][] }; thing: { at: [number, number, number]; turn: [number, number, number, number] }; phase: "Before" | "Reach" | "Lift" | "Carry" | "Release" | "After"; hands: 1 | 2; miss: number; moved: number; limited: boolean; stand: { at: [number, number, number]; yaw: number } } };
/** raea's command line (render, play, guide, bundle, types; the media verbs: raea media, raea ffmpeg), run in process in cwd */
"cli.raea": { args: { args: string[]; cwd: string }; answer: { exit: number; stdout: string; stderr: string } };
/** a headless run ends with this exit code */
diff --git a/systems/body/tests/body.rs b/systems/body/tests/body.rs
new file mode 100644
index 00000000..9f711f81
--- /dev/null
+++ b/systems/body/tests/body.rs
@@ -0,0 +1,80 @@
+//! The body's own measures (docs/hands.feature): joint limits and the hand prior, on the stand-in body.
+
+use glam::{Quat, Vec3};
+use raea_sys_body::prior::{coefficients, oddness, shape};
+use raea_sys_body::*;
+
+#[test]
+fn limits_read_and_hold_a_joint() {
+ let s = mannequin();
+ // the rest pose is inside every range
+ assert!(violations(&s, &s.rest(), 0.01).is_empty(), "{:?}", violations(&s, &s.rest(), 0.01));
+ for side in Side::BOTH {
+ let [upper, elbow, hand] = s.arm(side).unwrap();
+ // an elbow's fold reads as flex alone, the same on both sides; bent back it is past its range, and is brought to it
+ let hinge = s.offset(elbow).cross(s.offset(hand)).normalize();
+ let a = angles_of(&s, elbow, Quat::from_axis_angle(hinge, 1.0)).unwrap();
+ assert!((a.flex - 57.3).abs() < 1.0 && a.side.abs() < 1.0 && a.twist.abs() < 1.0, "{a:?}");
+ let back = Quat::from_axis_angle(hinge, -1.2);
+ let mut p = s.rest();
+ p.turns[elbow] = back;
+ assert_eq!(violations(&s, &p, 0.5)[0].0, elbow);
+ let (held, by) = clamp_turn(&s, elbow, back);
+ assert!((angles_of(&s, elbow, held).unwrap().flex + 50.0).abs() < 0.5 && by > 15.0);
+ // a turn inside its range comes back as it is
+ let fine = Quat::from_axis_angle(Vec3::Z, 0.3 * side.sign());
+ assert_eq!(clamp_turn(&s, upper, fine), (fine, 0.0));
+ // a wrist's twist past 25 degrees is held there
+ let twisted = Quat::from_axis_angle((s.joints[hand].tail - s.joints[hand].head).normalize(), 0.9 * side.sign());
+ let (held, _) = clamp_turn(&s, hand, twisted);
+ assert!((angles_of(&s, hand, held).unwrap().twist - 25.0).abs() < 0.5);
+ }
+}
+
+#[test]
+fn the_hand_prior_shapes_and_reads_hands() {
+ let s = mannequin();
+ for side in Side::BOTH {
+ let rest = hand_rest(&s, side).unwrap();
+ let hand = s.arm(side).unwrap()[2];
+ // how far each fingertip is from its knuckle, on average: longest with the hand open, shortest in a fist
+ let span = |pose: &Pose| {
+ let w = s.fk(pose);
+ ["index", "middle", "ring", "pinky"].iter().map(|f| {
+ let [knuckle, _, tip] = s.finger(f, side).unwrap();
+ (w.pos[tip] + w.rot[tip] * (s.joints[tip].tail - s.joints[tip].head) - w.pos[knuckle]).length()
+ }).sum::<f32>() / 4.0
+ };
+ let shaped = |z: &[f32]| {
+ let mut p = s.rest();
+ shape(&s, &mut p.turns, side, z).unwrap();
+ p
+ };
+ let (mean, open, shut) = (shaped(&[]), shaped(&[2.0]), shaped(&[-2.0]));
+ // the first component opens and closes the hand, the average hand between: open at +2, a fist at -2
+ assert!(span(&open) > span(&mean) && span(&mean) > span(&shut) + 0.004, "{} {} {}", span(&open), span(&mean), span(&shut));
+ // and the fingers close toward the palm, not away from it
+ let w = s.fk(&shut);
+ let [knuckle, _, tip] = s.finger("middle", side).unwrap();
+ assert!((w.pos[tip] - w.pos[knuckle]).dot(w.rot[hand] * rest.palm) > 0.01);
+ // a shape reads back as the numbers that made it, with nothing left over
+ let z = [0.8, -0.5, 0.3, 1.1, -0.7];
+ let (back, left) = coefficients(&s, &shaped(&z), side).unwrap();
+ for (a, b) in z.iter().zip(&back) {
+ assert!((a - b).abs() < 0.05, "{z:?} read back as {:?}", &back[..5]);
+ }
+ assert!(left < 0.5, "{left} degrees left over");
+ // the average hand is the least odd; the rig's rest hand reads about 2 (it isn't SOMA's hand); fingers bent
+ // back read over twice that
+ assert!(oddness(&s, &mean, side).unwrap() < 0.05);
+ assert!(oddness(&s, &s.rest(), side).unwrap() < 3.0);
+ let mut broken = s.rest();
+ for f in ["index", "middle", "ring", "pinky"] {
+ let j = s.finger(f, side).unwrap()[1];
+ let axis = (s.joints[j].tail - s.joints[j].head).normalize().cross(rest.palm).normalize();
+ broken.turns[j] = Quat::from_axis_angle(axis, -1.2);
+ }
+ let (odd, usual) = (oddness(&s, &broken, side).unwrap(), oddness(&s, &s.rest(), side).unwrap());
+ assert!(odd > 2.0 * usual, "fingers bent back read {odd}, the rest hand {usual}");
+ }
+}
diff --git a/systems/grab/src/hand.rs b/systems/grab/src/hand.rs
index 7ffa8768..0bf1147e 100644
--- a/systems/grab/src/hand.rs
+++ b/systems/grab/src/hand.rs
@@ -29,6 +29,14 @@ pub enum Grip {
Two(Hold, Hold),
}
+impl Hold {
+ /// the same hold for the other hand: across the thing, on its far side
+ pub fn across(&self) -> Hold {
+ let flip = |v: Vec3| v - self.normal * (2.0 * v.dot(self.normal));
+ Hold { side: if self.side == Side::L { Side::R } else { Side::L }, at: flip(self.at), normal: -self.normal, along: self.along.map(flip), shape: self.shape }
+ }
+}
+
impl Grip {
pub fn holds(&self) -> impl Iterator<Item = Hold> {
let (a, b) = match *self {
diff --git a/systems/grab/src/json.rs b/systems/grab/src/json.rs
index 5d7c4dc9..1f6c6972 100644
--- a/systems/grab/src/json.rs
+++ b/systems/grab/src/json.rs
@@ -13,7 +13,7 @@ use raea_sys_body::rig::{Pose, Side, Skeleton};
use crate::shape::{Solid, Thing};
use crate::take::{Key, Release, Take, perform};
-pub const TAKE_ARGS: &str = "{ joints: { name: string; parent: number; head: [number, number, number]; tail: [number, number, number] }[]; t: number; base: { t: number; root: [number, number, number]; turns: [number, number, number, number][] }[]; thing: { ball?: number; box?: [number, number, number]; capsule?: [number, number]; cylinder?: [number, number]; at: [number, number, number]; turn?: [number, number, number, number] }; start: number; grab?: number; carried?: number; carry?: [number, number, number]; hand?: \"L\" | \"R\"; lower?: number; holds?: { side: \"L\" | \"R\"; at: [number, number, number]; normal: [number, number, number]; along?: [number, number, number]; shape?: number[] }[]; path?: { t: number; carry: [number, number, number]; tilt?: [number, number, number, number] }[]; weight?: number; avoid?: { ball?: number; box?: [number, number, number]; capsule?: [number, number]; cylinder?: [number, number]; at: [number, number, number]; turn?: [number, number, number, number] }[]; put?: { at: number; down: number; place: [number, number, number] }; throw?: { at: number; velocity: [number, number, number]; floor?: number } }";
+pub const TAKE_ARGS: &str = "{ joints: { name: string; parent: number; head: [number, number, number]; tail: [number, number, number] }[]; t: number; base: { t: number; root: [number, number, number]; turns: [number, number, number, number][] }[]; thing: { ball?: number; box?: [number, number, number]; capsule?: [number, number]; cylinder?: [number, number]; at: [number, number, number]; turn?: [number, number, number, number] }; start: number; grab?: number; carried?: number; carry?: [number, number, number]; hand?: \"L\" | \"R\"; lower?: number; holds?: { side: \"L\" | \"R\"; at: [number, number, number]; normal: [number, number, number]; along?: [number, number, number]; shape?: number[] }[]; path?: { t: number; carry: [number, number, number]; tilt?: [number, number, number, number] }[]; weight?: number; pass?: number; slide?: { t: number; at: [number, number, number]; turn?: [number, number, number, number] }[]; leave?: number; avoid?: { ball?: number; box?: [number, number, number]; capsule?: [number, number]; cylinder?: [number, number]; at: [number, number, number]; turn?: [number, number, number, number] }[]; put?: { at: number; down: number; place: [number, number, number] }; throw?: { at: number; velocity: [number, number, number]; floor?: number } }";
pub const TAKE_ANSWER: &str = "{ pose: { root: [number, number, number]; turns: [number, number, number, number][] }; thing: { at: [number, number, number]; turn: [number, number, number, number] }; phase: \"Before\" | \"Reach\" | \"Lift\" | \"Carry\" | \"Release\" | \"After\"; hands: 1 | 2; miss: number; moved: number; limited: boolean; stand: { at: [number, number, number]; yaw: number } }";
fn num(v: &Value, what: &str) -> Result<f32, String> {
@@ -101,6 +101,16 @@ pub fn take_json(v: &Value) -> Result<Value, String> {
if !v["weight"].is_null() {
take = take.weighing(num(&v["weight"], "weight")?);
}
+ if !v["pass"].is_null() {
+ take = take.passing(num(&v["pass"], "pass")?);
+ }
+ for k in v["slide"].as_array().into_iter().flatten() {
+ let turn = if k["turn"].is_null() { thing.turn } else { quat(&k["turn"], "a slide key's turn")? };
+ take.slide.push((num(&k["t"], "a slide key's t")?, vec3(&k["at"], "a slide key's at")?, turn));
+ }
+ if !v["leave"].is_null() {
+ take.release = Some(Release::Leave { at: num(&v["leave"], "leave")? });
+ }
for k in v["path"].as_array().into_iter().flatten() {
let tilt = if k["tilt"].is_null() { Quat::IDENTITY } else { quat(&k["tilt"], "a path key's tilt")? };
take.path.push(Key { t: num(&k["t"], "a path key's t")?, carry: vec3(&k["carry"], "a path key's carry")?, tilt });
diff --git a/systems/grab/src/shape.rs b/systems/grab/src/shape.rs
index 6a7d7282..6f9dc1cb 100644
--- a/systems/grab/src/shape.rs
+++ b/systems/grab/src/shape.rs
@@ -102,6 +102,17 @@ impl Thing {
None
}
+ /// the eight corners of the box round it, where it is
+ pub fn corners(&self) -> [Vec3; 8] {
+ let half = match self.solid {
+ Solid::Ball { r } => Vec3::splat(r),
+ Solid::Box { half } => half,
+ Solid::Capsule { half, r } => Vec3::new(r, half + r, r),
+ Solid::Cylinder { half, r } => Vec3::new(r, half, r),
+ };
+ [0, 1, 2, 3, 4, 5, 6, 7].map(|i: usize| self.world(Vec3::new(if i & 1 == 0 { -half.x } else { half.x }, if i & 2 == 0 { -half.y } else { half.y }, if i & 4 == 0 { -half.z } else { half.z })))
+ }
+
/// the same thing, moved and turned with a frame it rides in
pub fn moved(&self, at: Vec3, turn: Quat) -> Thing {
Thing { solid: self.solid, at, turn }
diff --git a/systems/grab/src/take.rs b/systems/grab/src/take.rs
index 231aeb1a..7ed09910 100644
--- a/systems/grab/src/take.rs
+++ b/systems/grab/src/take.rs
@@ -12,7 +12,7 @@ use raea_sys_body::arm::{ArmOptions, forearm_twist, reach};
use crate::trunk::{clear, look, roomy, stoop};
use raea_sys_body::limits::clamp_turn;
use raea_sys_body::prior::shape;
-use crate::hand::{Grip, Touch, curl, facing, wrist_for};
+use crate::hand::{Grip, Hold, Touch, curl, facing, wrist_for};
use raea_sys_body::rig::hand_rest;
use raea_sys_body::rig::{Pose, Posed, Skeleton};
use crate::shape::Thing;
@@ -21,8 +21,11 @@ use crate::shape::Thing;
pub enum Release {
/// set down: from `at` the thing is moved to `place`, standing there from `down` on, and the hands leave it
Put { at: f32, down: f32, place: Vec3 },
- /// let go at `at` with a velocity, metres a second (none: a drop); it falls to `floor`
+ /// let go at `at` with a velocity, metres a second (none: a drop); it falls to `floor`. A throw is wound up:
+ /// the thing drawn back against the throw and brought through, and the hands follow it out
Throw { at: f32, velocity: Vec3, floor: f32 },
+ /// the hands leave it where it is at `at` (a drawer pushed shut, a door opened)
+ Leave { at: f32 },
}
#[derive(Clone, Debug)]
@@ -43,6 +46,20 @@ pub struct Take {
pub path: Vec<Key>,
/// how heavy it is, kilograms (0: no matter)
pub weight: f32,
+ /// a thing that isn't carried but moved where it is (a drawer, a door, a lever): where it is at times after the
+ /// grab, in the frame the body is posed in; the hands stay on it
+ pub slide: Vec<(f32, Vec3, Quat)>,
+ /// when a thing held in one hand is passed to the other
+ pub pass: Option<f32>,
+}
+
+/// One hand's part in a take: its hold, when it leaves the body's own motion, when it is on, and when it lets go.
+#[derive(Clone, Copy)]
+struct Part {
+ hold: Hold,
+ start: f32,
+ grab: f32,
+ gone: Option<f32>,
}
/// Where a carried thing is at a time: off the chest as `carry` is, and tilted from how it is carried (the tilt in
@@ -80,9 +97,11 @@ pub struct Frame {
const GRAVITY: f32 = 9.81;
/// how long the hands take to go back to the body's own motion after letting go, seconds
-const AWAY: f32 = 0.4;
+const AWAY: f32 = 0.5;
/// how long the fingers take to shut, ending at the grab, seconds
const SHUT: f32 = 0.25;
+/// how long a throw's wind-up is, ending at the release, seconds
+const WIND: f32 = 0.4;
/// how open the hand comes in, in the hand prior's first component (its standard deviations)
const OPEN: f32 = 1.5;
@@ -110,7 +129,7 @@ impl Take {
// against the belly, the elbows bent: nearer and higher the smaller it is
Grip::Two(..) => Vec3::new(0.0, -0.1, 0.2 + thing.solid.bound() * 0.5),
};
- Take { thing, grip, start, grab: start + 0.6, carried: start + 1.1, carry, release: None, avoid: Vec::new(), path: Vec::new(), weight: 0.0 }
+ Take { thing, grip, start, grab: start + 0.6, carried: start + 1.1, carry, release: None, avoid: Vec::new(), path: Vec::new(), weight: 0.0, slide: Vec::new(), pass: None }
}
/// The take of a thing that weighs `kg`: held lower and closer, lifted slower, the trunk leaning back against it.
@@ -121,6 +140,31 @@ impl Take {
self
}
+ /// The take of a thing in one hand that is passed to the other at `at`: brought to the middle, taken by the
+ /// other hand on its far side, and carried on that side.
+ pub fn passing(mut self, at: f32) -> Self {
+ if matches!(self.grip, Grip::One(_)) {
+ self.pass = Some(at);
+ let (middle, other) = (Vec3::new(0.0, self.carry.y, self.carry.z), Vec3::new(-self.carry.x, self.carry.y, self.carry.z));
+ self.path.push(Key { t: at - 0.15, carry: middle, tilt: Quat::IDENTITY });
+ self.path.push(Key { t: at + 0.3, carry: middle, tilt: Quat::IDENTITY });
+ self.path.push(Key { t: at + 0.9, carry: other, tilt: Quat::IDENTITY });
+ }
+ self
+ }
+
+ /// each hand's part: both hands of a two-handed take together; a pass's second hand from half a second before
+ fn parts(&self) -> Vec<Part> {
+ let end = self.let_go();
+ match (self.grip, self.pass) {
+ (Grip::One(h), Some(pass)) => vec![
+ Part { hold: h, start: self.start, grab: self.grab, gone: Some(pass + 0.1) },
+ Part { hold: h.across(), start: pass - 0.5, grab: pass, gone: end },
+ ],
+ _ => self.grip.holds().map(|hold| Part { hold, start: self.start, grab: self.grab, gone: end }).collect(),
+ }
+ }
+
/// where the carried thing is off the chest at `t`, and its tilt: the carry place, then the path's keys, eased
/// from each to the next
fn held_at(&self, t: f32) -> (Vec3, Quat) {
@@ -154,10 +198,31 @@ impl Take {
return (self.thing, Phase::Reach);
}
let carried = |t: f32| {
+ // a thing moved where it is: from where it stood through its places, eased
+ if !self.slide.is_empty() {
+ let mut from = (self.grab, self.thing.at, self.thing.turn);
+ for key in &self.slide {
+ if t < key.0 {
+ let u = smooth(span(t, from.0, key.0));
+ return self.thing.moved(from.1.lerp(key.1, u), from.2.slerp(key.2, u));
+ }
+ from = *key;
+ }
+ return self.thing.moved(from.1, from.2);
+ }
let (ride, u) = (self.riding(skel, base, t), smooth(span(t, self.grab, self.carried)));
- // lifted in a low arc, so it leaves what it stood on before it travels
- let arc = Vec3::Y * 0.04 * (std::f32::consts::PI * span(t, self.grab, self.carried)).sin();
- self.thing.moved(self.thing.at.lerp(ride.at, u) + arc, self.thing.turn.slerp(ride.turn, u))
+ // lifted in a low arc, so it leaves what it stood on before it travels; and over whatever is in its
+ // way, by how deep its corners would be in it
+ let rise = (std::f32::consts::PI * span(t, self.grab, self.carried)).sin();
+ let mut thing = self.thing.moved(self.thing.at.lerp(ride.at, u) + Vec3::Y * 0.04 * rise, self.thing.turn.slerp(ride.turn, u));
+ let deep = thing.corners().iter().flat_map(|c| self.avoid.iter().map(|s| 0.01 - s.distance(*c))).fold(0.0f32, f32::max);
+ thing.at.y += deep * (rise * 4.0).min(1.0);
+ // a throw is wound up: drawn back against the throw over its last moments, and brought through
+ if let Some(Release::Throw { at, velocity, .. }) = self.release {
+ let wind = (std::f32::consts::PI * span(t, at - WIND, at)).sin() * (velocity.length() / 3.0).min(1.0);
+ thing.at -= velocity.normalize_or_zero() * 0.18 * wind;
+ }
+ thing
};
match self.release {
Some(Release::Put { at, down, place }) if t >= at => {
@@ -176,6 +241,7 @@ impl Take {
let land = (velocity.y + (velocity.y * velocity.y + 2.0 * GRAVITY * (from.at.y - rest).max(0.0)).sqrt()) / GRAVITY;
(from.moved(fall((t - at).min(land)), from.turn), Phase::After)
}
+ Some(Release::Leave { at }) if t >= at => (carried(at), Phase::After),
_ => (carried(t), if t < self.carried { Phase::Lift } else { Phase::Carry }),
}
}
@@ -184,7 +250,7 @@ impl Take {
fn let_go(&self) -> Option<f32> {
match self.release? {
Release::Put { down, .. } => Some(down),
- Release::Throw { at, .. } => Some(at),
+ Release::Throw { at, .. } | Release::Leave { at } => Some(at),
}
}
}
@@ -194,48 +260,58 @@ pub fn perform(skel: &Skeleton, base: &dyn Fn(f32) -> Pose, take: &Take, t: f32)
let own = base(t);
let (thing, phase) = take.thing_at(skel, base, t);
let mut frame = Frame { pose: own.clone(), thing, phase, miss: 0.0, moved: 0.0, touch: [None, None], limited: false };
- // how much the hands are the take's and not the body's own: none before, all from the start, none again once away
- let gone = take.let_go();
- let away = gone.map_or(0.0, |g| smooth(span(t, g, g + AWAY)));
- if t < take.start || away >= 1.0 {
+ // each hand's part: how far along its reach it is (`into`), and how far gone once it has let go (`away`)
+ let parts: Vec<(Part, f32, f32)> = take
+ .parts()
+ .into_iter()
+ .map(|p| (p, smooth(span(t, p.start, p.grab)), p.gone.map_or(0.0, |g| smooth(span(t, g, g + AWAY)))))
+ .filter(|(p, _, away)| t >= p.start && *away < 1.0)
+ .collect();
+ if parts.is_empty() {
return frame;
}
- // while letting go the hands ease off the place the thing was let go at, not after a thing in flight
- let held = match gone {
- Some(g) if t >= g => take.thing_at(skel, base, g).0,
- _ => thing,
- };
+ // how much of the body is the take's: the most any hand is on
+ let busy = parts.iter().map(|(_, into, away)| into * (1.0 - away)).fold(0.0, f32::max);
+ let on = parts.iter().map(|(_, _, away)| 1.0 - away).fold(0.0, f32::max);
let w0 = skel.fk(&own);
let face = yaw(&w0) * Vec3::Z;
- // in: how far along the reach; the hands are fully on the thing from the grab until they let go
- let into = smooth(span(t, take.start, take.grab));
- // the fingers point the level way the arm comes: from the shoulder to the hold, with some of the body's facing, so the
- // wrist is asked for as little bend as the hold allows
- let holds: Vec<_> = take
- .grip
- .holds()
- .filter_map(|h| {
+ // a hand that has let go eases off the place the thing was let go at, not after a thing in flight; and a throw's
+ // hands follow the thing out a little
+ let follow = match take.release {
+ Some(Release::Throw { velocity, .. }) => velocity.normalize_or_zero() * 0.2 * (velocity.length() / 3.0).min(1.0),
+ _ => Vec3::ZERO,
+ };
+ // the fingers point the level way the arm comes: from the shoulder to the hold, with some of the body's facing
+ let holds: Vec<_> = parts
+ .iter()
+ .filter_map(|&(part, into, away)| {
+ let h = part.hold;
+ let held = match part.gone {
+ Some(g) if t >= g => take.thing_at(skel, base, g).0,
+ _ => thing,
+ };
let shoulder = w0.pos[skel.arm(h.side)?[0]];
let from = held.world(h.at) - shoulder;
let found = Vec3::new(from.x, 0.0, from.z).normalize_or(face).lerp(facing(&held, w0.pos[0]), 0.4).normalize_or(face);
// a hold that is a way of holding says where the fingers point
let along = h.along.map_or(found, |a| held.turn * a);
- Some((h, wrist_for(skel, &held, &h, along)?))
+ Some((part, into, away, held, wrist_for(skel, &held, &h, along)?))
})
.collect();
// a thing lower than the arms reach: the body bends and squats for it, as far as the hands have got
- let low = holds.iter().map(|h| h.1.0.y).fold(f32::MAX, f32::min);
+ let low = holds.iter().map(|h| h.4.0.y).fold(f32::MAX, f32::min);
// solved with the bend the body has room for: where bending or leaning would put the head or chest into a
// solid, less of both and more squat, a step at a time
let solve = |give: f32, frame: &mut Frame| {
- let mut pose = stoop(skel, &own, &w0, face, low, into * (1.0 - away), give);
+ let mut pose = stoop(skel, &own, &w0, face, low, busy, give, &take.avoid);
frame.miss = 0.0;
// a heavy thing: the trunk leans back against it once it is off what it stood on
if let (true, Some(waist)) = (take.weight > 0.0, skel.joint("spine_01")) {
- let back = (0.012 * take.weight).min(0.2) * smooth(span(t, take.grab, take.carried)) * (1.0 - away);
+ let back = (0.012 * take.weight).min(0.2) * smooth(span(t, take.grab, take.carried)) * on;
pose.turns[waist] = (pose.turns[waist] * Quat::from_rotation_x(-back)).normalize();
}
- for &(hold, level) in &holds {
+ for &(part, into, away, held, level) in &holds {
+ let hold = part.hold;
let (Some(arm), Some(rest)) = (skel.arm(hold.side), hand_rest(skel, hold.side)) else { continue };
let normal = held.turn * hold.normal;
// how the hand lies on its hold: the fingers along the forearm as it arrives (found by solving the arm to
@@ -265,7 +341,7 @@ pub fn perform(skel: &Skeleton, base: &dyn Fn(f32) -> Pose, take: &Take, t: f32)
};
// the wrist's path: from where the body has it, in onto the hold from off the surface and over what is in the
// way; and when letting go, back off the surface and out to where the body has it
- let (u, off) = if away > 0.0 { (1.0 - away, normal * 0.08 * (away * 4.0).min(1.0)) } else { (into, normal * 0.1 * (1.0 - into)) };
+ let (u, off) = if away > 0.0 { (1.0 - away, (normal * 0.08 + follow) * (away * 4.0).min(1.0)) } else { (into, normal * 0.1 * (1.0 - into)) };
let arc = Vec3::Y * 0.06 * (std::f32::consts::PI * u).sin();
let end = w0.rot[arm[2]].slerp(turn, smooth(span(u, 0.0, 0.8)));
let target = clear(w0.pos[arm[2]].lerp(at + off, u) + arc, end, &rest, &take.avoid);
@@ -279,7 +355,13 @@ pub fn perform(skel: &Skeleton, base: &dyn Fn(f32) -> Pose, take: &Take, t: f32)
let swung = Quat::from_axis_angle(line, (step * 10.0).to_radians()) * pole;
let Some(solved) = reach(skel, &pose, hold.side, target, ArmOptions { pole: Some(swung), end: Some(end), soft: 0.02, stiff: give }) else { break };
let tried = forearm_twist(skel, &solved.pose, hold.side, 1.0);
- let past: f32 = arm[..2].iter().map(|&joint| clamp_turn(skel, joint, tried.turns[joint]).1).sum();
+ let mut past: f32 = arm[..2].iter().map(|&joint| clamp_turn(skel, joint, tried.turns[joint]).1).sum();
+ // a swing that puts the elbow or the forearm into a solid counts as far past
+ let drawn = skel.fk(&tried);
+ let (elbow, wrist) = (drawn.pos[arm[1]], drawn.pos[arm[2]]);
+ if take.avoid.iter().any(|s| s.distance(elbow) < 0.04 || s.distance(elbow.lerp(wrist, 0.5)) < 0.03) {
+ past += 30.0;
+ }
if best.as_ref().is_none_or(|b| past < b.0 - 0.01) {
best = Some((past, tried));
}
@@ -292,19 +374,19 @@ pub fn perform(skel: &Skeleton, base: &dyn Fn(f32) -> Pose, take: &Take, t: f32)
let (wrist, by) = clamp_turn(skel, arm[2], pose.turns[arm[2]]);
pose.turns[arm[2]] = wrist;
frame.limited |= past >= 0.5 || by > 0.5;
- if t >= take.grab && away <= 0.0 {
+ if t >= part.grab && away <= 0.0 {
frame.miss = frame.miss.max((skel.fk(&pose).pos[arm[2]] - at).length());
}
// the hand opens as it comes in, as a hand does before it takes something (the prior's open hand), then
// the fingers shut over the last moments of the reach, and open again as the hand leaves
let mut open = pose.turns.clone();
if shape(skel, &mut open, hold.side, &[OPEN]).is_some() {
- let wide = smooth(span(t, take.start, take.grab - SHUT));
+ let wide = smooth(span(t, part.start, part.grab - SHUT));
for (mine, open) in pose.turns.iter_mut().zip(&open) {
*mine = mine.slerp(*open, wide);
}
}
- let close = smooth(span(t, take.grab - SHUT, take.grab)) * (1.0 - smooth(span(away, 0.0, 0.5)));
+ let close = smooth(span(t, part.grab - SHUT, part.grab)) * (1.0 - smooth(span(away, 0.0, 0.5)));
match hold.shape {
// a way of holding brings its hand's shape; any other hold curls the fingers onto what is there
Some(z) => {
@@ -320,8 +402,8 @@ pub fn perform(skel: &Skeleton, base: &dyn Fn(f32) -> Pose, take: &Take, t: f32)
}
}
// the eyes go to the thing as the hands do, and mostly leave it once it is carried
- let watching = into * (1.0 - away) * (1.0 - 0.7 * smooth(span(t, take.grab, take.carried)));
- look(skel, &pose, held.at, watching * (1.0 - 0.5 * give))
+ let watching = busy * (1.0 - 0.7 * smooth(span(t, take.grab, take.carried)));
+ look(skel, &pose, thing.at, watching * (1.0 - 0.5 * give))
};
let mut pose = solve(0.0, &mut frame);
for step in 1..=6 {
@@ -330,12 +412,25 @@ pub fn perform(skel: &Skeleton, base: &dyn Fn(f32) -> Pose, take: &Take, t: f32)
}
pose = solve(step as f32 / 6.0, &mut frame);
}
- if away > 0.0 {
- // the last of the way back is a blend of the joints, so the arm ends exactly as the body has it
- let w = 1.0 - smooth(span(away, 0.5, 1.0));
- pose.root = own.root.lerp(pose.root, w);
- for (mine, own) in pose.turns.iter_mut().zip(&own.turns) {
- *mine = own.slerp(*mine, w);
+ // the first and the last of a hand's part are a blend of the joints, so its arm leaves and ends exactly as the
+ // body has it: each hand's own arm by its own part, the rest of the body by the hand most on
+ let mut most = 0.0f32;
+ // a joint of a side's arm or hand (its legs aren't)
+ let of_arm = |name: &str, side: &str| name.ends_with(side) && !["thigh", "calf", "foot", "ball"].iter().any(|leg| name.starts_with(leg));
+ let sides: Vec<String> = holds.iter().map(|h| format!("_{}", h.0.hold.side.sx())).collect();
+ for (&(part, _, away, ..), side) in holds.iter().zip(&sides) {
+ let w = smooth(span(t, part.start, part.start + 0.15)) * (1.0 - smooth(span(away, 0.25, 1.0)));
+ most = most.max(w);
+ for (i, joint) in skel.joints.iter().enumerate() {
+ if of_arm(&joint.name, side) {
+ pose.turns[i] = own.turns[i].slerp(pose.turns[i], w);
+ }
+ }
+ }
+ pose.root = own.root.lerp(pose.root, most);
+ for (i, joint) in skel.joints.iter().enumerate() {
+ if !sides.iter().any(|side| of_arm(&joint.name, side)) {
+ pose.turns[i] = own.turns[i].slerp(pose.turns[i], most);
}
}
frame.moved = pose.turns.iter().zip(&own.turns).map(|(a, b)| a.angle_between(*b).to_degrees()).fold(0.0, f32::max);
diff --git a/systems/grab/src/trunk.rs b/systems/grab/src/trunk.rs
index 448bd566..af749bdc 100644
--- a/systems/grab/src/trunk.rs
+++ b/systems/grab/src/trunk.rs
@@ -13,13 +13,13 @@ use raea_sys_body::rig::{Pose, Posed, Side, Skeleton};
use crate::shape::Thing;
/// how far below the shoulders, in arm lengths, the hands go before the body bends for them
-const EASY: f32 = 0.7;
+const EASY: f32 = 0.88;
/// The body bent and lowered for hands that must get down to `low` (a height, metres), by `amount` (0 to 1) of what
/// that needs. `own` drawn is `w`; `face` is the level way the body faces. `give` (0 to 1) trades the bend for more
/// squat, down to an upright, deep squat at 1: for a body that can't bend forward because something stands there
/// (`roomy` says when it can).
-pub fn stoop(skel: &Skeleton, own: &Pose, w: &Posed, face: Vec3, low: f32, amount: f32, give: f32) -> Pose {
+pub fn stoop(skel: &Skeleton, own: &Pose, w: &Posed, face: Vec3, low: f32, amount: f32, give: f32, solids: &[Thing]) -> Pose {
let (Some(l), Some(r)) = (skel.arm(Side::L), skel.arm(Side::R)) else { return own.clone() };
let shoulders = (w.pos[l[0]].y + w.pos[r[0]].y) / 2.0;
let arm = skel.offset(l[1]).length() + skel.offset(l[2]).length();
@@ -33,22 +33,31 @@ pub fn stoop(skel: &Skeleton, own: &Pose, w: &Posed, face: Vec3, low: f32, amoun
let (drop, pitch) = ((short * 0.75).min(0.55), (short * 0.8).min(0.85));
let (drop, pitch) = ((drop + 0.45 * pitch * give).min(0.82) * amount, pitch * (1.0 - give) * amount);
let left = Vec3::Y.cross(face);
+ let knees: Vec<usize> = ["calf_l", "calf_r"].iter().filter_map(|n| skel.joint(n)).collect();
let mut pose = own.clone();
- // the hips go back a little as the trunk goes forward, as a body keeps its balance
- pose.root = own.root - Vec3::Y * drop - face * (0.12 * pitch);
- pose.turns[0] = (Quat::from_axis_angle(left, pitch * 0.4) * own.turns[0]).normalize();
- for name in ["spine_01", "spine_02", "spine_03"] {
- if let Some(i) = skel.joint(name) {
- pose.turns[i] = (own.turns[i] * Quat::from_rotation_x(pitch * 0.2)).normalize();
+ // the hips go back a little as the trunk goes forward, as a body keeps its balance; and further back, 4 cm at a
+ // time, while a knee would be in a solid (a squat in front of a table sits back from it)
+ for back in 0..6 {
+ pose = own.clone();
+ pose.root = own.root - Vec3::Y * drop - face * (0.12 * pitch + 0.04 * back as f32);
+ pose.turns[0] = (Quat::from_axis_angle(left, pitch * 0.4) * own.turns[0]).normalize();
+ for name in ["spine_01", "spine_02", "spine_03"] {
+ if let Some(i) = skel.joint(name) {
+ pose.turns[i] = (own.turns[i] * Quat::from_rotation_x(pitch * 0.2)).normalize();
+ }
+ }
+ // the feet stay where they stand, turned as they were, the knees going forward
+ for s in Side::BOTH {
+ let x = s.sx();
+ let leg = [format!("thigh_{x}"), format!("calf_{x}"), format!("foot_{x}")].map(|n| skel.joint(&n));
+ let [Some(a), Some(b), Some(c)] = leg else { continue };
+ let pole = face + left * (0.25 * s.sign());
+ pose = two_bone(skel, &pose, [a, b, c], w.pos[c], ArmOptions { pole: Some(pole), end: Some(w.rot[c]), ..Default::default() }).pose;
+ }
+ let at = skel.fk(&pose);
+ if knees.iter().all(|&k| solids.iter().all(|s| s.distance(at.pos[k]) > 0.07)) {
+ break;
}
- }
- // the feet stay where they stand, turned as they were, the knees going forward
- for s in Side::BOTH {
- let x = s.sx();
- let leg = [format!("thigh_{x}"), format!("calf_{x}"), format!("foot_{x}")].map(|n| skel.joint(&n));
- let [Some(a), Some(b), Some(c)] = leg else { continue };
- let pole = face + left * (0.25 * s.sign());
- pose = two_bone(skel, &pose, [a, b, c], w.pos[c], ArmOptions { pole: Some(pole), end: Some(w.rot[c]), ..Default::default() }).pose;
}
pose
}
diff --git a/systems/grab/tests/body.rs b/systems/grab/tests/body.rs
index 0ab90d6a..0aa484df 100644
--- a/systems/grab/tests/body.rs
+++ b/systems/grab/tests/body.rs
@@ -1,36 +1,10 @@
-//! The measures docs/hands.feature names for the body round the hands: joint limits, and keeping out of solids.
+//! The measures docs/hands.feature names for the body round the hands: its arms inside their ranges, keeping out of
+//! solids, a thing's own holds, weight.
use glam::{Quat, Vec3};
use raea_sys_body::*;
use raea_sys_grab::*;
-#[test]
-fn limits_read_and_hold_a_joint() {
- let s = mannequin();
- // the rest pose is inside every range
- assert!(violations(&s, &s.rest(), 0.01).is_empty(), "{:?}", violations(&s, &s.rest(), 0.01));
- for side in Side::BOTH {
- let [upper, elbow, hand] = s.arm(side).unwrap();
- // an elbow's fold reads as flex alone, the same on both sides; bent back it is past its range, and is brought to it
- let hinge = s.offset(elbow).cross(s.offset(hand)).normalize();
- let a = angles_of(&s, elbow, Quat::from_axis_angle(hinge, 1.0)).unwrap();
- assert!((a.flex - 57.3).abs() < 1.0 && a.side.abs() < 1.0 && a.twist.abs() < 1.0, "{a:?}");
- let back = Quat::from_axis_angle(hinge, -1.2);
- let mut p = s.rest();
- p.turns[elbow] = back;
- assert_eq!(violations(&s, &p, 0.5)[0].0, elbow);
- let (held, by) = clamp_turn(&s, elbow, back);
- assert!((angles_of(&s, elbow, held).unwrap().flex + 50.0).abs() < 0.5 && by > 15.0);
- // a turn inside its range comes back as it is
- let fine = Quat::from_axis_angle(Vec3::Z, 0.3 * side.sign());
- assert_eq!(clamp_turn(&s, upper, fine), (fine, 0.0));
- // a wrist's twist past 25 degrees is held there
- let twisted = Quat::from_axis_angle((s.joints[hand].tail - s.joints[hand].head).normalize(), 0.9 * side.sign());
- let (held, _) = clamp_turn(&s, hand, twisted);
- assert!((angles_of(&s, hand, held).unwrap().twist - 25.0).abs() < 0.5);
- }
-}
-
#[test]
fn a_take_keeps_both_arms_inside_their_ranges() {
let s = mannequin();
@@ -81,55 +55,6 @@ fn the_head_and_chest_stay_out_of_a_table_when_the_body_bends() {
assert!(table.distance(top(&perform(&s, &|_| s.rest(), &take, take.grab).pose, head).1) < 0.09);
}
-#[test]
-fn the_hand_prior_shapes_and_reads_hands() {
- use raea_sys_body::prior::{coefficients, oddness, shape};
- let s = mannequin();
- for side in Side::BOTH {
- let rest = hand_rest(&s, side).unwrap();
- let hand = s.arm(side).unwrap()[2];
- // how far each fingertip is from its knuckle, on average: longest with the hand open, shortest in a fist
- let span = |pose: &Pose| {
- let w = s.fk(pose);
- ["index", "middle", "ring", "pinky"].iter().map(|f| {
- let [knuckle, _, tip] = s.finger(f, side).unwrap();
- (w.pos[tip] + w.rot[tip] * (s.joints[tip].tail - s.joints[tip].head) - w.pos[knuckle]).length()
- }).sum::<f32>() / 4.0
- };
- let shaped = |z: &[f32]| {
- let mut p = s.rest();
- shape(&s, &mut p.turns, side, z).unwrap();
- p
- };
- let (mean, open, shut) = (shaped(&[]), shaped(&[2.0]), shaped(&[-2.0]));
- // the first component opens and closes the hand, the average hand between: open at +2, a fist at -2
- assert!(span(&open) > span(&mean) && span(&mean) > span(&shut) + 0.004, "{} {} {}", span(&open), span(&mean), span(&shut));
- // and the fingers close toward the palm, not away from it
- let w = s.fk(&shut);
- let [knuckle, _, tip] = s.finger("middle", side).unwrap();
- assert!((w.pos[tip] - w.pos[knuckle]).dot(w.rot[hand] * rest.palm) > 0.01);
- // a shape reads back as the numbers that made it, with nothing left over
- let z = [0.8, -0.5, 0.3, 1.1, -0.7];
- let (back, left) = coefficients(&s, &shaped(&z), side).unwrap();
- for (a, b) in z.iter().zip(&back) {
- assert!((a - b).abs() < 0.05, "{z:?} read back as {:?}", &back[..5]);
- }
- assert!(left < 0.5, "{left} degrees left over");
- // the average hand is the least odd; the rig's rest hand reads about 2 (it isn't SOMA's hand); fingers bent
- // back read over twice that
- assert!(oddness(&s, &mean, side).unwrap() < 0.05);
- assert!(oddness(&s, &s.rest(), side).unwrap() < 3.0);
- let mut broken = s.rest();
- for f in ["index", "middle", "ring", "pinky"] {
- let j = s.finger(f, side).unwrap()[1];
- let axis = (s.joints[j].tail - s.joints[j].head).normalize().cross(rest.palm).normalize();
- broken.turns[j] = Quat::from_axis_angle(axis, -1.2);
- }
- let (odd, usual) = (oddness(&s, &broken, side).unwrap(), oddness(&s, &s.rest(), side).unwrap());
- assert!(odd > 2.0 * usual, "fingers bent back read {odd}, the rest hand {usual}");
- }
-}
-
#[test]
fn a_mug_is_taken_by_its_own_hold_and_drunk_from() {
use raea_sys_body::prior::shape;
diff --git a/systems/grab/tests/use.rs b/systems/grab/tests/use.rs
new file mode 100644
index 00000000..5dd7c5c6
--- /dev/null
+++ b/systems/grab/tests/use.rs
@@ -0,0 +1,117 @@
+//! The measures docs/hands.feature names for what is done with a thing: passing it, moving it where it is, a
+//! throw's wind-up, and keeping the arms, the knees and the thing out of the scene's solids.
+
+use glam::{Quat, Vec3};
+use raea_sys_body::*;
+use raea_sys_grab::*;
+
+fn palm_off(s: &Skeleton, f: &Frame, hold: &Hold) -> f32 {
+ let (w, rest) = (s.fk(&f.pose), hand_rest(s, hold.side).unwrap());
+ let hand = s.arm(hold.side).unwrap()[2];
+ (w.pos[hand] + w.rot[hand] * rest.mid - f.thing.world(hold.at)).length()
+}
+
+#[test]
+fn a_cup_passes_from_one_hand_to_the_other() {
+ let s = mannequin();
+ let cup = Thing::new(Solid::Cylinder { half: 0.05, r: 0.035 }, Vec3::new(-0.2, 1.0, 0.38));
+ let Grip::One(right) = find_holds(&cup, s.rest().root, HoldOptions::default()) else { panic!() };
+ let take = Take::new(cup, Grip::One(right), 0.0).passing(2.0);
+ let left = right.across();
+ assert_eq!(left.side, Side::L);
+ let base = |_: f32| s.rest();
+ let (mut t, mut last) = (0.0, perform(&s, &base, &take, 0.0).pose);
+ while t < 4.0 {
+ t += 1.0 / 30.0;
+ let f = perform(&s, &base, &take, t);
+ // somebody always has it: the right hand until the pass, the left from it, both at it
+ if t >= take.grab && t <= 2.0 {
+ assert!(palm_off(&s, &f, &right) < 0.015, "t {t}: the right palm is {} m off", palm_off(&s, &f, &right));
+ }
+ if t >= 2.0 {
+ assert!(palm_off(&s, &f, &left) < 0.015, "t {t}: the left palm is {} m off", palm_off(&s, &f, &left));
+ }
+ assert!((f.thing.turn * Vec3::Y).dot(Vec3::Y) > 0.9999);
+ // and no joint of the body jumps as the hands change (the arm let go swings back down in half a second, at up to 26 degrees a frame)
+ let turned = f.pose.turns[..22].iter().zip(&last.turns).map(|(a, b)| a.angle_between(*b).to_degrees()).fold(0.0, f32::max);
+ assert!(turned < 30.0, "t {t}: a joint turned {turned} degrees in a 30th of a second");
+ last = f.pose;
+ }
+ // it ends on the left, and the right arm is back as the body has it
+ let f = perform(&s, &base, &take, 3.9);
+ assert!(f.thing.at.x > 0.1);
+ let arm = s.arm(Side::R).unwrap();
+ assert!(arm.iter().all(|&j| f.pose.turns[j].dot(s.rest().turns[j]).abs() > 0.9999));
+}
+
+#[test]
+fn a_drawer_is_pulled_open_and_left_there() {
+ let s = mannequin();
+ // a drawer front at belly height, held by a handle on its near face, pulled 18 cm toward the body
+ let front = Thing::new(Solid::Box { half: Vec3::new(0.2, 0.08, 0.01) }, Vec3::new(-0.1, 0.95, 0.55));
+ let handle = Hold { side: Side::R, at: Vec3::new(0.0, 0.0, -0.01), normal: Vec3::NEG_Z, along: None, shape: Some([-0.8, 0.0, 0.0]) };
+ let mut take = Take::new(front, Grip::One(handle), 0.0);
+ let open = front.at - Vec3::Z * 0.18;
+ take.slide = vec![(1.6, open, Quat::IDENTITY)];
+ take.release = Some(Release::Leave { at: 1.9 });
+ let base = |_: f32| s.rest();
+ let mut t = take.grab;
+ while t < 1.9 {
+ let f = perform(&s, &base, &take, t);
+ assert!(palm_off(&s, &f, &handle) < 0.03, "t {t}: the palm is {} m off the handle", palm_off(&s, &f, &handle));
+ // it only slides: level, along its runners
+ assert!((f.thing.at.x - front.at.x).abs() < 1e-5 && (f.thing.at.y - front.at.y).abs() < 1e-5);
+ t += 1.0 / 30.0;
+ }
+ // left open where it was pulled to, the body back on its own
+ let f = perform(&s, &base, &take, 3.0);
+ assert!((f.thing.at - open).length() < 1e-5);
+ assert_eq!(f.pose, s.rest());
+}
+
+#[test]
+fn a_throw_is_wound_up_and_followed_through() {
+ let s = mannequin();
+ let ball = Thing::new(Solid::Ball { r: 0.05 }, Vec3::new(-0.2, 1.0, 0.38));
+ let mut take = Take::new(ball, find_holds(&ball, s.rest().root, HoldOptions::default()), 0.0);
+ let velocity = Vec3::new(0.0, 2.0, 4.0);
+ take.release = Some(Release::Throw { at: 2.0, velocity, floor: 0.0 });
+ let base = |_: f32| s.rest();
+ let along = |t: f32| perform(&s, &base, &take, t).thing.at.dot(velocity.normalize());
+ // drawn back against the throw before the release, then through where it was carried, moving forward
+ let (carried, back, at) = (along(1.5), along(1.8), along(2.0));
+ assert!(back < carried - 0.1, "drawn back {} m", carried - back);
+ assert!((at - carried).abs() < 1e-3 && along(1.97) < at);
+ // the hand goes out after it before coming back
+ let hand = s.arm(Side::R).unwrap()[2];
+ let reach = |t: f32| s.fk(&perform(&s, &base, &take, t).pose).pos[hand].dot(velocity.normalize());
+ assert!(reach(2.12) > reach(2.0) + 0.03, "the hand follows through {} m", reach(2.12) - reach(2.0));
+ assert_eq!(perform(&s, &base, &take, 2.6).pose, s.rest());
+}
+
+#[test]
+fn the_forearms_the_knees_and_the_thing_keep_out_of_a_table() {
+ let s = mannequin();
+ let table = Thing::new(Solid::Box { half: Vec3::new(0.6, 0.475, 0.34) }, Vec3::new(0.0, 0.475, 0.72));
+ // a box on the floor in front of the table, lifted onto it
+ let thing = Thing::new(Solid::Box { half: Vec3::new(0.16, 0.1, 0.12) }, Vec3::new(0.0, 0.1, 0.2));
+ let mut take = Take::new(thing, find_holds(&thing, s.rest().root, HoldOptions::default()), 0.0);
+ take.avoid = vec![table];
+ take.release = Some(Release::Put { at: 1.6, down: 2.3, place: Vec3::new(0.0, 1.05, 0.52) });
+ let base = |_: f32| s.rest();
+ let joints: Vec<usize> = ["calf_l", "calf_r", "lowerarm_l", "lowerarm_r"].iter().map(|n| s.joint(n).unwrap()).collect();
+ let mut t = 0.0;
+ while t < 2.3 {
+ let f = perform(&s, &base, &take, t);
+ let w = s.fk(&f.pose);
+ for &j in &joints {
+ assert!(table.distance(w.pos[j]) > 0.0, "t {t}: {} is in the table", s.joints[j].name);
+ }
+ // the box's corners clear the table on the way (it ends standing on it)
+ if t > take.grab + 0.1 && t < 2.2 {
+ let deep = f.thing.corners().iter().map(|c| -table.distance(*c)).fold(f32::MIN, f32::max);
+ assert!(deep < 0.005, "t {t}: a corner of the box is {deep} m into the table");
+ }
+ t += 1.0 / 30.0;
+ }
+}