// Hand placement rules. // // Right hand: stays home; its fingertips snap a little toward the nearest logo edge. // Left hand: picks the best grip point on the logo boundary, landing the middle of its // fingers on the edge with the fingers pointing into the logo (wrapping over the stroke). // Both hands move rigidly and follow their targets through springs. import { BODY, LEFT_HAND, RIGHT_HAND } from './huggy'; import { CLS_OPAQUE, type LogoSampler } from './logo'; export interface Pose { x: number; // translation of the anchor point from its home position y: number; rot: number; // radians, around the anchor point } export class SpringPose { pose: Pose = { x: 0, y: 0, rot: 0 }; vel: Pose = { x: 0, y: 0, rot: 0 }; target: Pose = { x: 0, y: 0, rot: 0 }; constructor( private k = 140, private damp = 17, ) {} step(dt: number) { for (const key of ['x', 'y', 'rot'] as const) { const a = this.k * (this.target[key] - this.pose[key]) - this.damp * this.vel[key]; this.vel[key] += a * dt; this.pose[key] += this.vel[key] * dt; } } } export function solveRightHand(sampler: LogoSampler | null): Pose { if (!sampler) return { x: 0, y: 0, rot: 0 }; const { tip, snapRadius, maxShift } = RIGHT_HAND; if (sampler.cls(tip.x, tip.y) === CLS_OPAQUE) return { x: 0, y: 0, rot: 0 }; let best: { x: number; y: number } | null = null; let bestD = Infinity; for (let dy = -snapRadius; dy <= snapRadius; dy += 6) { for (let dx = -snapRadius; dx <= snapRadius; dx += 6) { const d = Math.hypot(dx, dy); if (d > snapRadius || d >= bestD) continue; if (sampler.cls(tip.x + dx, tip.y + dy) === CLS_OPAQUE) { best = { x: dx, y: dy }; bestD = d; } } } if (!best) return { x: 0, y: 0, rot: 0 }; // Move so the fingertips just overlap the edge, capped to a small range. const len = Math.hypot(best.x, best.y); const want = Math.min(maxShift, len + 8); return { x: (best.x / len) * want, y: (best.y / len) * want, rot: 0 }; } export interface GripChoice { pose: Pose; point: { x: number; y: number }; cost: number; key: number; // candidate index, for hysteresis } function wrapAngle(a: number) { while (a > Math.PI) a -= Math.PI * 2; while (a <= -Math.PI) a += Math.PI * 2; return a; } const HOME_KEY = -1; export function solveLeftHand( sampler: LogoSampler | null, previous: GripChoice | null, touching: Set, ): GripChoice | null { if (!sampler) return null; const R = BODY.r; const g0 = LEFT_HAND.grip; const reach = R * 1.45; const cands = sampler.asset.candidates; // If the logo already runs under the hand's home spot (Fal reference), staying home is a // perfectly good hug. const home: GripChoice | null = sampler.cls(g0.x, g0.y) === CLS_OPAQUE ? { pose: { x: 0, y: 0, rot: 0 }, point: { ...g0 }, cost: 60, key: HOME_KEY } : null; const evaluate = (idx: number): GripChoice | null => { const c = cands[idx]; const p = sampler.toScene(c.x, c.y); const n = sampler.dirToScene(c.nx, c.ny); const thick = c.thick * sampler.pl.scale; const dx = p.x - BODY.cx; const dy = p.y - BODY.cy; const dc = Math.hypot(dx, dy); if (dc < R * 0.82) return null; // would sit on the face const facing = (n.x * dx + n.y * dy) / dc; // >0: edge faces away from Huggy if (facing < -0.35) return null; const dist = Math.hypot(p.x - g0.x, p.y - g0.y); if (dist > reach) return null; const rot = wrapAngle(Math.atan2(-n.y, -n.x) - LEFT_HAND.fingerAngle); if (rot < LEFT_HAND.minRot || rot > LEFT_HAND.maxRot) return null; // Fingers rest over the stroke: move the grip point inward a bit. const inset = Math.min(thick * 0.5, 26); const gx = p.x - n.x * inset; const gy = p.y - n.y * inset; // The left hand reaches "a bit further" than home to wrap around the logo. // In the references the grip lands ~1.2R–1.7R from the body centre, around mid height. let cost = Math.abs(dc - R * 1.55) * 0.6 + dist * 0.1; if (!c.outer) cost += 140; // hugs wrap the outer silhouette, not hole edges if (touching.size && !touching.has(c.comp)) cost += 220; // hug the piece being touched cost += Math.max(0, thick - 95) * 1.2; // prefer strokes thin enough to wrap cost += Math.abs(p.y - BODY.cy) * 0.25; // grip around the body's mid height cost -= facing * 45; cost += Math.abs(rot) * 35; // prefer little rotation if (dc < R * 0.98) cost += (R * 0.98 - dc) * 2; // avoid crowding the body return { pose: { x: gx - g0.x, y: gy - g0.y, rot }, point: { x: gx, y: gy }, cost, key: idx }; }; let best: GripChoice | null = home; for (let i = 0; i < cands.length; i++) { const e = evaluate(i); if (e && (!best || e.cost < best.cost)) best = e; } // Hysteresis: keep the previous grip unless the new one is clearly better. if (previous && best && previous.key !== best.key) { const again = previous.key === HOME_KEY ? home : previous.key < cands.length ? evaluate(previous.key) : null; if (again && again.cost < best.cost + 40) return again; } return best; } /** SVG transform for a rigid hand pose around its anchor point. */ export function poseTransform(pose: Pose, anchor: { x: number; y: number }): string { const deg = (pose.rot * 180) / Math.PI; return `translate(${(anchor.x + pose.x).toFixed(2)} ${(anchor.y + pose.y).toFixed(2)}) rotate(${deg.toFixed(2)}) translate(${-anchor.x} ${-anchor.y})`; }