<?phpnamespace App\Revisited\Render;/** * Where the items held in first person are and how they turn (pure maths, no GL: tested alone): the rest of each * hand, the blows (anticipation → strike → recovery, per Weapons::anim() kind), the torch's sweep. * * A pose is [dx, dy, dz, rx, ry, rz]: metres in the eye's frame (right, up, forward) added to the hand's rest place, * and radians about the item's own x (positive tips it forward / down), y (turns it) and z (rolls it) axes. Poses * are written for the right hand; mirror() gives the left hand's (dx, ry, rz change sign). */final class HeldPose{ /** the hands' rest places in the eye's frame: right, up, forward */ public const RIGHT = [0.3, -0.34, 0.5]; public const LEFT = [-0.31, -0.33, 0.48]; /** the peak poses: [anticipation, strike] per kind */ private const PEAKS = [ // club, axe, stick, torch: raised back over the shoulder, then brought down across the view (a roll: +rz tips the // head toward the middle) 'swing' => [[0.05, 0.15, -0.07, -0.95, 0.2, -0.3], [-0.12, 0.0, 0.12, 0.45, -0.2, 1.0]], // machete: a diagonal slash, wrist cocked out, then across and down 'chop' => [[0.1, 0.11, -0.04, -0.75, 0.45, -0.65], [-0.18, -0.02, 0.1, 0.35, -0.4, 1.15]], // knife: drawn back low, then a short stab forward 'stab' => [[0.03, -0.03, -0.13, -0.25, 0.0, 0.12], [-0.07, 0.05, 0.3, 0.35, -0.12, 0.0]], // spear, harpoon, pole: pulled back, then thrust far forward 'thrust' => [[0.06, -0.05, -0.24, -0.1, 0.08, 0.0], [-0.15, 0.09, 0.55, 0.12, -0.16, 0.1]], // bow: drawn to the cheek (the string's tension: a tremble), then the release snaps forward 'bow' => [[-0.09, 0.05, -0.13, 0.0, -0.08, -0.15], [-0.02, 0.07, 0.07, -0.18, 0.0, -0.05]], // crossbow: shouldered, then the recoil kicks it up 'crossbow' => [[-0.02, 0.02, -0.02, 0.0, 0.0, 0.0], [-0.02, 0.04, -0.09, -0.18, 0.0, 0.0]], // guns: aimed, then a hard recoil 'gun' => [[-0.03, 0.03, 0.0, 0.0, 0.0, 0.0], [-0.03, 0.05, -0.12, -0.26, 0.0, 0.05]], // sling: whirled over the hand (see attack()), then the release flings it forward 'sling' => [[0.0, 0.14, 0.0, 0.0, 0.0, 0.0], [-0.1, 0.1, 0.24, 0.6, 0.0, 0.0]], // fists: nothing drawn, the view nudges 'punch' => [[0.02, -0.02, -0.08, 0.0, 0.0, 0.0], [-0.05, 0.03, 0.2, 0.0, 0.0, 0.0]], ]; /** * The pose of a blow: $p 0..1 through it (Game::swingPhase()), $strike the fraction where it lands * (windup / duration). Anticipation eases in, the strike is fast, the recovery eases back to rest. * @return array{0: float, 1: float, 2: float, 3: float, 4: float, 5: float} */ public static function attack(string $kind, float $p, float $strike): array { [$wind, $hit] = self::PEAKS[$kind] ?? self::PEAKS['swing']; $rest = [0.0, 0.0, 0.0, 0.0, 0.0, 0.0]; if ($p <= 0.0 || $p >= 1.0) { return $rest; } $s = max(0.05, min(0.85, $strike)); $peak = min(1.0, $s + max(0.05, min(0.12, (1 - $s) * 0.35))); if ($p < $s) { $pose = self::mix($rest, $wind, self::ease($p / $s)); if ($kind === 'bow' && $p > $s * 0.55) { // full draw: the string pulls, the arms tremble $pose[0] += sin($p * 230.0) * 0.004; $pose[1] += sin($p * 170.0 + 1.0) * 0.003; } if ($kind === 'sling') { // the whirl: the pouch spins faster and faster over the hand $a = $p / $s; $turn = $a * $a * 3.0 * 2 * M_PI; $pose[0] += cos($turn) * 0.09 * $a; $pose[2] += sin($turn) * 0.09 * $a; $pose[4] += $turn; } return $pose; } if ($p < $peak) { // the strike: fast, a little past the peak's ease return self::mix($wind, $hit, sqrt(($p - $s) / max(0.001, $peak - $s))); } return self::mix($hit, $rest, self::ease(($p - $peak) / max(0.001, 1 - $peak))); } /** * The torch swept before Rob (Game::brandishPhase()), in the right hand's terms: out toward the middle of the * view, raised, rolled, and back. @return array{0: float, 1: float, 2: float, 3: float, 4: float, 5: float} */ public static function brandish(float $p): array { if ($p <= 0.0 || $p >= 1.0) { return [0.0, 0.0, 0.0, 0.0, 0.0, 0.0]; } $k = sin($p * M_PI); $across = sin($p * M_PI * 2.0); // out, then back across return [-0.36 * $k - 0.1 * $across, 0.12 * $k, 0.2 * $k, 0.45 * $k, -0.3 * $across, 0.8 * $k + 0.35 * $across]; } /** * The shield on the left arm (Game::guardPhase(): 0 lowered .. 1 raised), already in the LEFT hand's terms (no * mirror()): lowered at the side, nearly edge-on; raised across the lower left of the view, its top edge just under * the crosshair, its back (frame, grip) toward the eye. @return array{0: float, 1: float, 2: float, 3: float, 4: float, 5: float} */ public static function shield(float $raise): array { $low = [-0.1, -0.16, -0.04, 0.18, -1.0, -0.12]; $high = [0.0, -0.04, 0.08, -0.12, -0.32, 0.05]; return self::mix($low, $high, self::ease($raise)); } /** 0..1: the muzzle flash of a shot just after the strike (guns, flare gun), 0 for the rest. */ public static function flash(string $kind, float $p, float $strike): float { if ($kind !== 'gun' || $p < $strike) { return 0.0; } $a = ($p - $strike) / 0.08; return $a < 1.0 ? 1.0 - $a : 0.0; } /** The left hand's version of a right-hand pose. @param array{0: float, 1: float, 2: float, 3: float, 4: float, 5: float} $pose @return array{0: float, 1: float, 2: float, 3: float, 4: float, 5: float} */ public static function mirror(array $pose): array { return [-$pose[0], $pose[1], $pose[2], $pose[3], -$pose[4], -$pose[5]]; } /** * The item's axes in the eye's frame after its pose's rotation (y, then x, then z): x, y, z columns. * @return array{0: array{0: float, 1: float, 2: float}, 1: array{0: float, 1: float, 2: float}, 2: array{0: float, 1: float, 2: float}} */ public static function axes(float $rx, float $ry, float $rz): array { $out = []; foreach ([[1.0, 0.0, 0.0], [0.0, 1.0, 0.0], [0.0, 0.0, 1.0]] as $v) { // roll (z) [$x, $y, $z] = [$v[0] * cos($rz) - $v[1] * sin($rz), $v[0] * sin($rz) + $v[1] * cos($rz), $v[2]]; // tip (x): +rx brings y toward z (forward) [$y, $z] = [$y * cos($rx) - $z * sin($rx), $y * sin($rx) + $z * cos($rx)]; // turn (y) [$x, $z] = [$x * cos($ry) + $z * sin($ry), -$x * sin($ry) + $z * cos($ry)]; $out[] = [$x, $y, $z]; } return [$out[0], $out[1], $out[2]]; } /** @param array<int, float> $a @param array<int, float> $b @return array{0: float, 1: float, 2: float, 3: float, 4: float, 5: float} */ private static function mix(array $a, array $b, float $t): array { $o = []; for ($i = 0; $i < 6; $i++) { $o[] = $a[$i] + ($b[$i] - $a[$i]) * $t; } return [$o[0], $o[1], $o[2], $o[3], $o[4], $o[5]]; } private static function ease(float $t): float { $t = max(0.0, min(1.0, $t)); return $t * $t * (3 - 2 * $t); }}