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.

HG8a, HG9a, HG10a: a thing held one way brings its holds and hand shape; a carried thing follows a path (a drink is a key at the mouth); weight. The hand lies along the forearm as it arrives, brought level where that would tip fingers into a solid, and the elbow's swing is chosen inside the shoulder's and elbow's ranges instead of clamping the arm after the solve (clamping threw the wrist up to 18 cm off). All of a wrist's twist goes to the forearm. 25 tests

shinyobjectzee8edf43b17f
diff --git a/docs/hands.feature b/docs/hands.feature
index 044fc647..f00da1f6 100644
--- a/docs/hands.feature
+++ b/docs/hands.feature
@@ -218,6 +218,37 @@ Feature: Procedural arms and hands: a character reaches for, takes, holds, carri
# and a 78-joint rig on PyTorch and Warp), and the vertex-fitting refinements of its pose inversion.
Then a hand can be given a natural shape by number, and any hand can be scored against real ones
+ @done
+ Scenario: HG8a. A thing held one way brings its holds
+ # 11-14, built. A hold can come with the thing (`holds`, in its own frame: where the palm goes, the way the
+ # surface faces, optionally the way the fingers point and the hand's shape as the hand prior's numbers); any
+ # other thing has its holds found (HG2). A hold with a shape takes that shape instead of the curl. Where a
+ # hold doesn't say, the fingers lie along the forearm as it arrives (the arm solved to the hold alone
+ # first), brought level by halves where that would tip them into a solid; and the elbow's swing is chosen,
+ # 10 degrees at a time, as the nearest that keeps the shoulder and the elbow inside their ranges. A mug
+ # held by its side in a loose fist keeps the palm within 15 mm of its hold from the table to the mouth and
+ # back with no joint at a limit; with the fingers forced to point forward the same take reports the arm at
+ # its limits and the palm up to 18 cm off, which is the report doing its job.
+ Then a mug, a grip or a handle is held the way it says, and a hold no arm can make is said, not faked
+
+ @done
+ Scenario: HG9a. Using what is held: a path the hands follow
+ # 11-14, built. A take's `path`: where the carried thing is at times, off the chest, and how it is tipped;
+ # eased from the carry place through each key. A drink is one key at the mouth, tipped 29 degrees. The
+ # thing is exactly at each key at its time, and upright again after. A lift and a put-down now arc 4 cm so
+ # the thing leaves what it stood on before it travels. Seen on teal: a mug to the mouth and back to its
+ # place. Not done: passing a thing from hand to hand or to another character, and pushes, pulls and turns
+ # of things that stay where they are (a door, a drawer).
+ Then a held thing can be raised, tipped, aimed or swung by giving where it goes
+
+ @done
+ Scenario: HG10a. Weight
+ # 11-14, built. `weighing(kg)` (a take's `weight`): the carry place 1.5 cm lower a kilogram (to 18 cm) and
+ # a little closer, the lift an eighth longer a kilogram, the waist leaning back 0.7 degrees a kilogram (to
+ # 11). 12 kg: carried over 10 cm lower, the lift 0.75 s longer, the chest behind where a light take has
+ # 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
+
Rule: Next, in order
@needs:HG5
diff --git a/sdk/character/src/take.ts b/sdk/character/src/take.ts
index 735afbc1..c5508997 100644
--- a/sdk/character/src/take.ts
+++ b/sdk/character/src/take.ts
@@ -41,6 +41,15 @@ export interface TakeSpec {
hand?: "L" | "R";
/** how far below the thing's middle the hands go, metres */
lower?: number;
+ /** a thing held one way brings its holds, in its own frame: where a palm goes (`at`, on its surface), the way the
+ * surface faces there (`normal`), optionally the way the fingers point (`along`) and the hand's shape as
+ * handShape's first numbers (`shape`: about -1.2 is a loose fist round a handle). None: holds are found on its shape */
+ holds?: { side: "L" | "R"; at: Vec3; normal: Vec3; along?: Vec3; shape?: number[] }[];
+ /** what is done with the thing once carried: where it is at times (`carry`, from the chest as above) and how it
+ * is tipped from how it is carried (`tilt`, in the body's axes); the hands follow. A drink is a key at the mouth */
+ 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;
/** 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 b583e484..601f9c51 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; 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; 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/grab/src/hand.rs b/systems/grab/src/hand.rs
index d0df2106..7ffa8768 100644
--- a/systems/grab/src/hand.rs
+++ b/systems/grab/src/hand.rs
@@ -16,6 +16,11 @@ pub struct Hold {
pub at: Vec3,
/// the way the surface faces there, outward
pub normal: Vec3,
+ /// the way the fingers point, for a thing held one way (a handle, a grip); found from the arm when none
+ pub along: Option<Vec3>,
+ /// the hand's shape, as the hand prior's first numbers, for a hold that is a way of holding (a pinch, a fist
+ /// round a handle); the fingers curl onto the surface when none
+ pub shape: Option<[f32; 3]>,
}
#[derive(Clone, Copy, Debug)]
@@ -66,7 +71,7 @@ pub fn find_holds(thing: &Thing, holder: Vec3, o: HoldOptions) -> Grip {
let from = thing.at + out * far - Vec3::Y * o.lower;
// no surface on that line (the lowered ray passes under a ball): the thing's edge that way, as the original does
let (at, normal) = thing.cast(from, -out, far).map_or((thing.at + out * thing.solid.bound(), out), |h| (h.at, h.normal));
- Hold { side, at: thing.local(at), normal: thing.turn.inverse() * normal }
+ Hold { side, at: thing.local(at), normal: thing.turn.inverse() * normal, along: None, shape: None }
};
let (l, r) = (hold(Side::L), hold(Side::R));
if (l.at - r.at).length() <= o.span { Grip::One(if o.hand == Side::L { l } else { r }) } else { Grip::Two(l, r) }
diff --git a/systems/grab/src/json.rs b/systems/grab/src/json.rs
index 498e2240..5d7c4dc9 100644
--- a/systems/grab/src/json.rs
+++ b/systems/grab/src/json.rs
@@ -8,12 +8,12 @@ use serde_json::{Value, json};
use crate::trunk::stand_for;
use raea_sys_body::fit::{Mark, fit};
use raea_sys_body::prior::{coefficients, oddness, shape};
-use crate::hand::{Grip, HoldOptions, find_holds};
+use crate::hand::{Grip, Hold, HoldOptions, find_holds};
use raea_sys_body::rig::{Pose, Side, Skeleton};
use crate::shape::{Solid, Thing};
-use crate::take::{Release, Take, perform};
+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; 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; 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> {
@@ -75,9 +75,36 @@ pub fn take_json(v: &Value) -> Result<Value, String> {
if !v["lower"].is_null() {
options.lower = num(&v["lower"], "lower")?;
}
- // the holds are found from where the body stands when it starts to reach
- let grip = find_holds(&thing, own(start).root, options);
+ // a thing held one way brings its holds (in its own frame); any other has them found from where the body
+ // stands when it starts to reach
+ let mut own_holds = Vec::new();
+ for h in v["holds"].as_array().into_iter().flatten() {
+ let along = if h["along"].is_null() { None } else { Some(vec3(&h["along"], "a hold's along")?.normalize_or_zero()) };
+ // a shape's first three numbers (the ones left out are 0)
+ let mut shape = None;
+ if let Some(z) = h["shape"].as_array() {
+ let mut first = [0.0f32; 3];
+ for (slot, n) in first.iter_mut().zip(z) {
+ *slot = num(n, "a number of a hold's shape")?;
+ }
+ shape = Some(first);
+ }
+ let side = if h["side"] == "L" { Side::L } else { Side::R };
+ own_holds.push(Hold { side, at: vec3(&h["at"], "a hold's at")?, normal: vec3(&h["normal"], "a hold's normal")?.normalize_or_zero(), along, shape });
+ }
+ let grip = match own_holds[..] {
+ [one] => Grip::One(one),
+ [a, b, ..] => if a.side == Side::L { Grip::Two(a, b) } else { Grip::Two(b, a) },
+ [] => find_holds(&thing, own(start).root, options),
+ };
let mut take = Take::new(thing, grip, start);
+ if !v["weight"].is_null() {
+ take = take.weighing(num(&v["weight"], "weight")?);
+ }
+ 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 });
+ }
if !v["grab"].is_null() {
take.grab = num(&v["grab"], "grab")?;
take.carried = take.grab + 0.5;
diff --git a/systems/grab/src/lib.rs b/systems/grab/src/lib.rs
index 9196fe17..6565a95a 100644
--- a/systems/grab/src/lib.rs
+++ b/systems/grab/src/lib.rs
@@ -23,5 +23,5 @@ pub use hand::{Grip, Hold, HoldOptions, Touch, curl, find_holds, wrist_for};
pub use interact::pick;
pub use json::{FIT_ANSWER, FIT_ARGS, HAND_ANSWER, HAND_ARGS, TAKE_ANSWER, TAKE_ARGS, fit_json, hand_json, take_json};
pub use shape::{Solid, Thing};
-pub use take::{Frame, Phase, Release, Take, perform};
+pub use take::{Frame, Key, Phase, Release, Take, perform};
pub use trunk::{clear, look, roomy, stand_for, stoop};
diff --git a/systems/grab/src/take.rs b/systems/grab/src/take.rs
index bf95d9a1..231aeb1a 100644
--- a/systems/grab/src/take.rs
+++ b/systems/grab/src/take.rs
@@ -39,6 +39,19 @@ pub struct Take {
pub release: Option<Release>,
/// the scene's solids the hands keep out of (a table, a shelf)
pub avoid: Vec<Thing>,
+ /// what is done with the thing once it is carried: places for it to be at times, the hands following
+ pub path: Vec<Key>,
+ /// how heavy it is, kilograms (0: no matter)
+ pub weight: 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
+/// the body's own axes: about x it tips toward the body or away).
+#[derive(Clone, Copy, Debug)]
+pub struct Key {
+ pub t: f32,
+ pub carry: Vec3,
+ pub tilt: Quat,
}
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
@@ -97,16 +110,39 @@ 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() }
+ Take { thing, grip, start, grab: start + 0.6, carried: start + 1.1, carry, release: None, avoid: Vec::new(), path: Vec::new(), weight: 0.0 }
}
- /// where the thing rides at a moment of the body's motion: off the chest, turned only with the body's facing, so
- /// what stood upright is carried upright
+ /// The take of a thing that weighs `kg`: held lower and closer, lifted slower, the trunk leaning back against it.
+ pub fn weighing(mut self, kg: f32) -> Self {
+ self.weight = kg.max(0.0);
+ self.carry -= Vec3::new(0.0, (0.015 * self.weight).min(0.18), (0.004 * self.weight).min(0.05));
+ self.carried = self.grab + (self.carried - self.grab) * (1.0 + self.weight / 8.0);
+ self
+ }
+
+ /// 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) {
+ let mut from = Key { t: self.carried, carry: self.carry, tilt: Quat::IDENTITY };
+ for key in &self.path {
+ if t < key.t {
+ let u = smooth(span(t, from.t, key.t));
+ return (from.carry.lerp(key.carry, u), from.tilt.slerp(key.tilt, u));
+ }
+ from = *key;
+ }
+ (from.carry, from.tilt)
+ }
+
+ /// where the thing rides at a moment of the body's motion: off the chest, turned only with the body's facing (and
+ /// the path's tilt), so what stood upright is carried upright
fn riding(&self, skel: &Skeleton, base: &dyn Fn(f32) -> Pose, t: f32) -> Thing {
let (now, then) = (skel.fk(&base(t)), skel.fk(&base(self.grab)));
let chest = skel.joint("spine_03").unwrap_or(0);
let (y, y0) = (yaw(&now), yaw(&then));
- self.thing.moved(now.pos[chest] + y * self.carry, (y * y0.inverse() * self.thing.turn).normalize())
+ let (carry, tilt) = self.held_at(t);
+ self.thing.moved(now.pos[chest] + y * carry, (y * tilt * y0.inverse() * self.thing.turn).normalize())
}
/// where the thing is at `t`, and whether the hands are on it
@@ -119,14 +155,17 @@ impl Take {
}
let carried = |t: f32| {
let (ride, u) = (self.riding(skel, base, t), smooth(span(t, self.grab, self.carried)));
- self.thing.moved(self.thing.at.lerp(ride.at, u), self.thing.turn.slerp(ride.turn, u))
+ // 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))
};
match self.release {
Some(Release::Put { at, down, place }) if t >= at => {
let from = carried(at);
let u = smooth(span(t, at, down));
// set down level with where it stood: its own turn about the vertical kept
- let thing = from.moved(from.at.lerp(place, u), from.turn);
+ let arc = Vec3::Y * 0.04 * (std::f32::consts::PI * span(t, at, down)).sin();
+ let thing = from.moved(from.at.lerp(place, u) + arc, from.turn);
(thing, if t < down { Phase::Release } else { Phase::After })
}
Some(Release::Throw { at, velocity, floor }) if t >= at => {
@@ -178,7 +217,9 @@ pub fn perform(skel: &Skeleton, base: &dyn Fn(f32) -> Pose, take: &Take, t: f32)
.filter_map(|h| {
let shoulder = w0.pos[skel.arm(h.side)?[0]];
let from = held.world(h.at) - shoulder;
- let along = Vec3::new(from.x, 0.0, from.z).normalize_or(face).lerp(facing(&held, w0.pos[0]), 0.4).normalize_or(face);
+ 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)?))
})
.collect();
@@ -189,28 +230,68 @@ pub fn perform(skel: &Skeleton, base: &dyn Fn(f32) -> Pose, take: &Take, t: f32)
let solve = |give: f32, frame: &mut Frame| {
let mut pose = stoop(skel, &own, &w0, face, low, into * (1.0 - away), give);
frame.miss = 0.0;
- for &(hold, (at, turn)) in &holds {
+ // 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);
+ pose.turns[waist] = (pose.turns[waist] * Quat::from_rotation_x(-back)).normalize();
+ }
+ for &(hold, level) in &holds {
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
+ // the hold alone), so the wrist is asked for the least bend; where that would tip the fingers into a solid
+ // (a cup on a table, the forearm coming down to it) they are brought level, by halves
+ let (at, turn) = if hold.along.is_some() {
+ level
+ } else {
+ let probe = reach(skel, &pose, hold.side, held.world(hold.at) + normal * 0.04, ArmOptions { stiff: give, ..Default::default() });
+ let forearm = probe.map_or(face, |r| {
+ let w = skel.fk(&r.pose);
+ (w.pos[arm[2]] - w.pos[arm[1]]).normalize_or(face)
+ });
+ let mut best = (f32::MAX, level);
+ for keep in [1.0, 0.5, 0.0] {
+ let along = Vec3::new(forearm.x, forearm.y * keep, forearm.z).normalize_or(face);
+ let Some(lie) = wrist_for(skel, &held, &hold, along) else { continue };
+ let pushed = (clear(lie.0, lie.1, &rest, &take.avoid) - lie.0).length();
+ if pushed < best.0 - 1e-4 {
+ best = (pushed, lie);
+ }
+ if pushed < 0.004 {
+ break;
+ }
+ }
+ best.1
+ };
// 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 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);
- // elbows out and down, the way arms hang
+ // elbows out and down, the way arms hang; where that puts the shoulder or the elbow past its range, the
+ // elbow is swung about the line from shoulder to hand, 10 degrees at a time, to the nearest swing that
+ // doesn't (the wrist is then held to its own range, and a frame that had to says so)
let pole = yaw(&w0) * Vec3::new(0.7 * hold.side.sign(), -1.0, -0.4);
- let Some(solved) = reach(skel, &pose, hold.side, target, ArmOptions { pole: Some(pole), end: Some(end), soft: 0.02, stiff: give }) else { continue };
- pose = forearm_twist(skel, &solved.pose, hold.side, 0.7);
- // each joint of the arm kept inside its range (limits.rs): a wrist bends so far and no further, and a
- // frame that needed more says so
- for joint in arm {
- let (turn, by) = clamp_turn(skel, joint, pose.turns[joint]);
- if by > 0.5 {
- pose.turns[joint] = turn;
- frame.limited = true;
+ let line = (target - skel.fk(&pose).pos[arm[0]]).normalize_or(face);
+ let mut best: Option<(f32, Pose)> = None;
+ for step in [0.0f32, 1.0, -1.0, 2.0, -2.0, 3.0, -3.0, 4.0, -4.0, 5.0, -5.0, 6.0, -6.0] {
+ 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();
+ if best.as_ref().is_none_or(|b| past < b.0 - 0.01) {
+ best = Some((past, tried));
+ }
+ if past < 0.5 {
+ break;
}
}
+ let Some((past, solved)) = best else { continue };
+ pose = solved;
+ 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 {
frame.miss = frame.miss.max((skel.fk(&pose).pos[arm[2]] - at).length());
}
@@ -224,8 +305,18 @@ pub fn perform(skel: &Skeleton, base: &dyn Fn(f32) -> Pose, take: &Take, t: f32)
}
}
let close = smooth(span(t, take.grab - SHUT, take.grab)) * (1.0 - smooth(span(away, 0.0, 0.5)));
- if close > 0.0 {
- frame.touch[hold.side as usize] = curl(skel, &mut pose.turns, (at, turn), hold.side, &held, close);
+ match hold.shape {
+ // a way of holding brings its hand's shape; any other hold curls the fingers onto what is there
+ Some(z) => {
+ let mut shaped = pose.turns.clone();
+ if shape(skel, &mut shaped, hold.side, &z).is_some() {
+ for (mine, shaped) in pose.turns.iter_mut().zip(&shaped) {
+ *mine = mine.slerp(*shaped, close);
+ }
+ }
+ }
+ None if close > 0.0 => frame.touch[hold.side as usize] = curl(skel, &mut pose.turns, (at, turn), hold.side, &held, close),
+ None => {}
}
}
// the eyes go to the thing as the hands do, and mostly leave it once it is carried
diff --git a/systems/grab/tests/body.rs b/systems/grab/tests/body.rs
index a88e7c22..0ab90d6a 100644
--- a/systems/grab/tests/body.rs
+++ b/systems/grab/tests/body.rs
@@ -129,3 +129,56 @@ fn the_hand_prior_shapes_and_reads_hands() {
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;
+ let s = mannequin();
+ let mug = Thing::new(Solid::Cylinder { half: 0.05, r: 0.04 }, Vec3::new(-0.2, 1.0, 0.38));
+ // held one way: the right hand on its right side, the hand a loose fist; the fingers point as the arm comes
+ let hold = Hold { side: Side::R, at: Vec3::new(-0.04, 0.0, 0.0), normal: Vec3::NEG_X, along: None, shape: Some([-1.2, 0.0, 0.0]) };
+ let mut take = Take::new(mug, Grip::One(hold), 0.0);
+ // to the mouth and tipped toward the face at 2 s, back down by 3 s
+ let up = Vec3::new(-0.02, 0.36, 0.24);
+ take.path = vec![Key { t: 2.0, carry: up, tilt: Quat::from_rotation_x(-0.5) }, Key { t: 3.0, carry: take.carry, tilt: Quat::IDENTITY }];
+ let base = |_: f32| s.rest();
+ let chest = s.joint("spine_03").unwrap();
+ let hand = s.arm(Side::R).unwrap()[2];
+ let rest = hand_rest(&s, Side::R).unwrap();
+ let mut t = take.grab;
+ while t < 3.2 {
+ let f = perform(&s, &base, &take, t);
+ let w = s.fk(&f.pose);
+ // the palm stays on the mug's own hold the whole way
+ let off = (w.pos[hand] + w.rot[hand] * rest.mid - f.thing.world(hold.at)).length();
+ assert!(off < 0.015 && !f.limited, "t {t}: the palm is {off} m off the mug's hold, limited {}", f.limited);
+ t += 1.0 / 30.0;
+ }
+ let f = perform(&s, &base, &take, 2.0);
+ assert!((f.thing.at - (s.fk(&s.rest()).pos[chest] + up)).length() < 1e-4);
+ assert!(((f.thing.turn * Vec3::Y).angle_between(Vec3::Y) - 0.5).abs() < 1e-3, "tipped 29 degrees at the mouth");
+ assert!((perform(&s, &base, &take, 3.1).thing.turn * Vec3::Y).dot(Vec3::Y) > 0.9999);
+ // the fingers are the hold's shape, not a curl
+ let mut shaped = s.rest();
+ shape(&s, &mut shaped.turns, Side::R, &[-1.2, 0.0, 0.0]).unwrap();
+ let index = s.finger("index", Side::R).unwrap()[1];
+ assert!(f.pose.turns[index].dot(shaped.turns[index]).abs() > 0.9999);
+}
+
+#[test]
+fn a_heavy_thing_is_held_lower_lifted_slower_and_leant_against() {
+ let s = mannequin();
+ let thing = Thing::new(Solid::Box { half: Vec3::new(0.16, 0.1, 0.12) }, Vec3::new(0.0, 1.05, 0.42));
+ let grip = find_holds(&thing, s.rest().root, HoldOptions::default());
+ let (light, heavy) = (Take::new(thing, grip, 0.0), Take::new(thing, grip, 0.0).weighing(12.0));
+ assert!(heavy.carried > light.carried + 0.5 && heavy.carry.y < light.carry.y - 0.1);
+ let base = |_: f32| s.rest();
+ let (a, b) = (perform(&s, &base, &light, 3.0), perform(&s, &base, &heavy, 3.0));
+ assert!(b.thing.at.y < a.thing.at.y - 0.1);
+ // the chest is behind where the light take has it, and both palms are still on
+ let chest = s.joint("spine_03").unwrap();
+ assert!(s.fk(&b.pose).pos[chest].z < s.fk(&a.pose).pos[chest].z - 0.005);
+ assert!(b.miss < 6e-3, "{}", b.miss);
+ // no weight changes nothing
+ assert_eq!(perform(&s, &base, &Take::new(thing, grip, 0.0).weighing(0.0), 3.0).pose, a.pose);
+}