yuzhewu207's picture
ww_index: window list, captions, lags and scripts to rebuild the WildWorld windows from upstream
a54e6a7 verified
Raw History Blame Contribute Delete
14.6 kB
"""Combat moves built from the motion primitives. Each move edits a Track over a frame range and keeps feet planted
(or stepping) so the validator's foot-slide and floor checks pass without per-shot hand tuning.
Conventions: frame ranges are [t0, t1); world xy targets in cm; humanoid moves assume the BOSSV2V 17-joint layout with
the weapon in hand_r; quadruped moves assume the `quadruped`/`dragon` joint names.
"""
import numpy as np
from .geometry import rot_axis, rot_z, UP
from .motion import ease
flat = lambda v: np.array([v[0], v[1], 0.0])
unit = lambda v: np.asarray(v, float) / np.linalg.norm(v)
def _clip01(x):
return float(np.clip(x, 0.0, 1.0))
# ------------------------------------------------------------------------------------------------------ feet control
def plant(tr, t0, t1, targets=None, pole_up=0.3):
"""IK every leg to fixed world feet over [t0, t1). targets: {chain: xyz}; default = feet at t0 dropped to z=0."""
chains = tr.legs()
targets = targets or {c: flat(tr.poses[t0, tr.j(c[2])]) for c in chains}
for t in range(t0, min(t1, tr.T)):
for c in chains:
tr.two_bone_ik(t, *c, target=targets[c], pole=tr.forward(t) + UP * pole_up)
return targets
def step_to(tr, t0, t1, end_targets, lead=None, lift=None, pole_up=0.3, start_targets=None):
"""Move each foot from its t0 position to end_targets[chain] with a lifted step, leading legs in the first 60 %
of the range and trailing legs in the last 60 %; feet are planted otherwise. The body must already be where it
should be for these frames (translate/turn first)."""
chains = tr.legs()
lead = lead or ("front" if any(c[0].startswith("front") for c in chains) else "thigh_l")
lift = lift if lift is not None else 0.12 * tr.sk.height
start = start_targets or {c: flat(tr.poses[t0, tr.j(c[2])]) for c in chains}
for t in range(t0, min(t1, tr.T)):
u = (t - t0) / max(1, t1 - t0 - 1)
for c in chains:
s = _clip01(u / 0.6) if c[0].startswith(lead) else _clip01((u - 0.4) / 0.6)
k = ease(s)
target = start[c] * (1 - k) + end_targets[c] * k + UP * lift * np.sin(np.pi * s)
tr.two_bone_ik(t, *c, target=target, pole=tr.forward(t) + UP * pole_up)
return end_targets
def body_feet_offsets(tr, t):
"""Feet relative to the root in the body frame at frame t (for re-planting after moves)."""
R = rot_z(-tr.yaw[t]); root = flat(tr.poses[t, 0])
return {c: (flat(tr.poses[t, tr.j(c[2])]) - root) @ R.T for c in tr.legs()}
def feet_at(tr, t, offsets):
R = rot_z(tr.yaw[t]); root = flat(tr.poses[t, 0])
return {c: flat(root + o @ R.T) for c, o in offsets.items()}
# ----------------------------------------------------------------------------------------------------- body moves
def step_shift(tr, t0, t1, vec, hold_until=None, lead=None, extra=None):
"""Shift the body by vec (world cm) over [t0, t1) with stepping feet; hold the new position afterwards (until the
end of the track or hold_until). extra(t, u) can rotate subtrees before the feet are solved."""
vec = np.asarray(vec, float); end = tr.T if hold_until is None else hold_until
offs = body_feet_offsets(tr, t0); start = {c: flat(tr.poses[t0, tr.j(c[2])]) for c in tr.legs()}
for t in range(t0, end):
u = ease(_clip01((t - t0) / max(1, t1 - t0)))
tr.translate(t, t + 1, vec * u)
if extra:
extra(t, u)
ends = {c: start[c] + flat(vec) for c in start}
step_to(tr, t0, t1, ends, lead=lead, start_targets=start)
plant(tr, t1, end, ends)
_ = offs
def turn_in_place(tr, t0, t1, dyaw, hold_until=None):
"""Turn about the root by dyaw (rad) with stepping feet, holding the new facing afterwards."""
start = {c: flat(tr.poses[t0, tr.j(c[2])]) for c in tr.legs()}
offs = body_feet_offsets(tr, t0)
tr.turn(t0, t1, dyaw, hold=True)
end = tr.T if hold_until is None else hold_until
ends = feet_at(tr, min(t1, tr.T - 1), offs)
step_to(tr, t0, t1, ends, start_targets=start)
plant(tr, t1, end, ends)
def turn_toward(tr, t0, t1, target_xy, hold_until=None):
d = flat(np.asarray(target_xy, float)[:2].tolist() + [0]) - flat(tr.poses[t0, 0])
want = np.arctan2(d[1], d[0]); dy = (want - tr.yaw[t0] + np.pi) % (2 * np.pi) - np.pi
turn_in_place(tr, t0, t1, dy, hold_until)
def crouch(tr, t0, t1, depth, hold_until=None, release=None):
"""Lower the body by depth cm (eased) with planted feet; hold until hold_until, then rise over release frames."""
feet = {c: flat(tr.poses[t0, tr.j(c[2])]) for c in tr.legs()}
end = tr.T if hold_until is None else hold_until
for t in range(t0, min(tr.T, end + (release or 0))):
if t < t1:
u = ease((t - t0) / max(1, t1 - t0))
elif t < end:
u = 1.0
else:
u = 1 - ease((t - end) / release)
tr.translate(t, t + 1, -UP * depth * u)
plant(tr, t0, min(tr.T, end + (release or 0)), feet)
# ------------------------------------------------------------------------------------------------ quadruped moves
def pounce(tr, t0, t_takeoff, t_land, target_xy, height=120.0, crouch_depth=None, recover=14, bite=0.5):
"""Quadruped leap: crouch [t0, t_takeoff), fly to put the front paws at target_xy, land and stand back up.
Front legs reach forward and hind legs stretch back in the air; the head dips to bite on the way down."""
H = tr.sk.meta.get("shoulder_height", tr.sk.height * 0.5)
crouch_depth = crouch_depth if crouch_depth is not None else 0.2 * H
crouch(tr, t0, t_takeoff, crouch_depth, hold_until=t_takeoff)
offs = body_feet_offsets(tr, t0)
fwd = tr.forward(t_takeoff)
# a wyvern's forelimbs are its wings, so it has no "front" legs and lands on its hind feet instead
front_feet = [c for c in tr.legs() if c[0].startswith("front")] or list(tr.legs())
front_off = np.mean([offs[c] for c in front_feet], axis=0) # body-frame front-paw centroid
root0 = tr.poses[t_takeoff, 0].copy()
land_root_xy = np.asarray(target_xy, float)[:2] - (rot_z(tr.yaw[t_takeoff]) @ front_off)[:2]
delta = np.array([land_root_xy[0] - root0[0], land_root_xy[1] - root0[1], 0.0])
leg_len = np.mean([tr.sk.bone_lengths[tr.j(c[1])] + tr.sk.bone_lengths[tr.j(c[2])] for c in tr.legs()])
for t in range(t_takeoff, tr.T):
u = _clip01((t - t_takeoff) / max(1, t_land - t_takeoff))
z = 4 * height * u * (1 - u) - crouch_depth * (1 - ease(_clip01(u * 4))) # rise out of the crouch smoothly
tr.translate(t, t + 1, delta * ease(u) + UP * (z if t < t_land else 0.0))
for t in range(t_takeoff, t_land):
u = (t - t_takeoff) / max(1, t_land - t_takeoff)
if bite:
tr.rotate_subtree(t, "neck_01", rot_axis(tr.right(t), bite * np.sin(np.pi * u)))
for c in tr.legs():
hip = tr.poses[t, tr.j(c[0])]
if c[0].startswith("front"):
target = hip + fwd * 0.55 * leg_len - UP * 0.55 * leg_len
else:
target = hip - fwd * 0.6 * leg_len - UP * 0.45 * leg_len
tr.two_bone_ik(t, *c, target=target, pole=fwd + UP * 0.3)
land_feet = feet_at(tr, min(t_land, tr.T - 1), offs)
crouch(tr, t_land, t_land + 3, 0.15 * H, hold_until=t_land + 3, release=recover)
plant(tr, t_land, tr.T, land_feet)
# ------------------------------------------------------------------------------------------------- humanoid moves
LEGS_H = (("thigh_l", "calf_l", "foot_l"), ("thigh_r", "calf_r", "foot_r"))
def stance_targets(tr, t, front_local=(58.0, -12.0), back_local=(-58.0, 12.0), front="l"):
"""World feet of a lunge stance for the root and facing at frame t; *_local are body-frame (forward, right) cm."""
R = rot_z(tr.yaw[t]); root = flat(tr.poses[t, 0])
loc = {front: front_local, ("r" if front == "l" else "l"): back_local}
return {c: flat(root + R @ np.array([loc[c[0][-1]][0], loc[c[0][-1]][1], 0.0])) for c in LEGS_H}
def stance_offsets(front_local=(58.0, -12.0), back_local=(-58.0, 12.0), front="l"):
"""The same stance as body-frame offsets per leg chain (for Track.gait(foot_offsets=...))."""
loc = {front: front_local, ("r" if front == "l" else "l"): back_local}
return {c: np.array([loc[c[0][-1]][0], loc[c[0][-1]][1], 0.0]) for c in LEGS_H}
def stance(tr, t0, t1, planted=True, from_current=False, **kw):
"""Humanoid lunge stance over [t0, t1): feet planted at their t0 stance positions (or following the root).
from_current plants the feet where they already are at t0 - 1 (continuity after a gait or another move)."""
fixed = ({c: flat(tr.poses[max(t0 - 1, 0), tr.j(c[2])]) for c in LEGS_H} if from_current and t0 > 0
else stance_targets(tr, t0, **kw))
for t in range(t0, min(t1, tr.T)):
tg = fixed if planted else stance_targets(tr, t, **kw)
for c in LEGS_H:
tr.two_bone_ik(t, *c, target=tg[c], pole=tr.forward(t) + UP * 0.2)
def _airborne_legs(tr, t0, t1, start, land, tuck_fn):
"""Feet through a jump: blend from the take-off feet to the tucked pose in the first 25 %, tucked in the middle,
blend to the landing stance in the last 25 %; never below the floor."""
def mix(a, b, kh, kv):
return np.array([a[0] * (1 - kh) + b[0] * kh, a[1] * (1 - kh) + b[1] * kh, a[2] * (1 - kv) + b[2] * kv])
for t in range(t0, t1):
u = (t - t0) / max(1, t1 - t0 - 1) # reaches 1 on the last airborne frame
for i, c in enumerate(LEGS_H):
tuck = tuck_fn(t, u, i, c)
if u < 0.25: # take-off: lift first, then draw the foot in
k = u / 0.25; tg = mix(start[c], tuck, ease(_clip01((k - 0.4) / 0.6)), ease(_clip01(k / 0.4)))
if tg[2] < 8.0: # still touching the ground: no horizontal travel yet
tg[:2] = start[c][:2]
elif u > 0.75: # landing: line up over the landing spot, then put it down
k = (u - 0.75) / 0.25; tg = mix(tuck, land[c], ease(_clip01(k / 0.6)), ease(_clip01((k - 0.6) / 0.4)))
if tg[2] < 8.0: # touching down: already over the landing spot
tg[:2] = land[c][:2]
else:
tg = tuck.copy()
tg[2] = max(tg[2], 0.0)
tr.two_bone_ik(t, *c, target=tg, pole=tr.forward(t))
def side_hop(tr, t0, t1, offset_xy, height=35.0, tilt=0.45, stance_kw=None):
"""Dodge hop with a sideways body roll, landing back into a planted stance."""
stance_kw = stance_kw or {}
start = {c: flat(tr.poses[t0, tr.j(c[2])]) for c in LEGS_H}
tr.jump(t0, t1, height=height, offset=offset_xy)
off = np.array([offset_xy[0], offset_xy[1], 0.0]); side = np.sign(off @ tr.right(t0)) or 1.0
leg = tr.sk.bone_lengths[tr.j("calf_l")] + tr.sk.bone_lengths[tr.j("foot_l")]
land = stance_targets(tr, min(t1, tr.T - 1), **stance_kw)
for t in range(t0, t1):
u = (t - t0) / max(1, t1 - t0)
tr.rotate_subtree(t, "spine_01", rot_axis(tr.forward(t), -side * tilt * np.sin(np.pi * u)))
_airborne_legs(tr, t0, t1, start, land,
lambda t, u, i, c: tr.poses[t, tr.j(c[0])] - UP * leg * (0.95 - 0.4 * np.sin(np.pi * u)) + tr.forward(t) * 10)
stance(tr, t1, tr.T, **stance_kw)
def arm_arc(tr, t0, t1, axis, angles, joint="upperarm_r"):
"""Rotate an arm subtree about a body axis ('right', 'up', 'forward' or a vector) following keyframed angles:
angles = [(u, rad), ...] with u in [0, 1] over [t0, t1); held weapons follow."""
us = np.array([a[0] for a in angles]); vs = np.array([a[1] for a in angles])
for t in range(t0, min(t1, tr.T)):
u = (t - t0) / max(1, t1 - t0 - 1)
k = np.searchsorted(us, u, side="right") - 1; k = min(max(k, 0), len(us) - 2)
w = ease(_clip01((u - us[k]) / max(us[k + 1] - us[k], 1e-6)))
a = vs[k] * (1 - w) + vs[k + 1] * w
ax = {"right": tr.right(t), "forward": tr.forward(t), "up": UP}[axis] if isinstance(axis, str) else axis
tr.rotate_subtree(t, joint, rot_axis(ax, a))
def slash(tr, t0, t1, direction="rl", arc=1.8, twist=0.45, recover=10):
"""Horizontal sword slash: wind back, shoulder sweep with a torso twist, back to guard over `recover` frames."""
s = -1.0 if direction == "rl" else 1.0
x = (t1 - t0) / (t1 - t0 + recover)
arm_arc(tr, t0, t1 + recover, "up", [(0.0, 0.0), (0.3 * x, -s * 0.6), (x, s * arc), (1.0, 0.0)])
arm_arc(tr, t0, t1 + recover, "up", [(0.0, 0.0), (0.3 * x, -s * 0.2), (x, s * twist), (1.0, 0.0)], joint="spine_01")
def jump_attack(tr, t0, t_land, target_xy, height=70.0, windup=6, recover=10, stance_kw=None):
"""Leaping overhead chop: crouch windup, fly to target_xy with the sword raised overhead, chop down through the
landing into a planted lunge stance."""
stance_kw = stance_kw or {}
t_take = t0 + windup
start = {c: flat(tr.poses[max(t0 - 1, 0), tr.j(c[2])]) for c in LEGS_H} # take off from where the feet are
root0 = tr.poses[t_take, 0].copy()
d = np.array([target_xy[0] - root0[0], target_xy[1] - root0[1], 0.0])
for t in range(t0, t_take):
tr.translate(t, t + 1, -UP * 12 * ease((t - t0) / max(1, windup)))
for t in range(t_take, tr.T):
u = _clip01((t - t_take) / max(1, t_land - t_take))
z = 4 * height * u * (1 - u) - 12 * (1 - ease(_clip01(u * 3))) # leave the 12 cm windup crouch smoothly
tr.translate(t, t + 1, d * ease(u) + UP * (z if t < t_land else 0.0))
leg = tr.sk.bone_lengths[tr.j("calf_l")] + tr.sk.bone_lengths[tr.j("foot_l")]
for t in range(t0, t_take):
for c in LEGS_H:
tr.two_bone_ik(t, *c, target=start[c], pole=tr.forward(t) + UP * 0.2)
land = stance_targets(tr, min(t_land, tr.T - 1), **stance_kw)
_airborne_legs(tr, t_take, t_land, start, land,
lambda t, u, i, c: tr.poses[t, tr.j(c[0])] - UP * leg * (0.95 - 0.45 * np.sin(np.pi * u))
+ tr.forward(t) * ((18 if i == 0 else -12) * np.sin(np.pi * u)))
stance(tr, t_land, tr.T, **stance_kw)
total = (t_land + recover) - t0
arm_arc(tr, t0, t_land + recover, "right",
[(0.0, 0.0), ((t_take - t0 + 0.55 * (t_land - t_take)) / total, -3.3), ((t_land + 2 - t0) / total, -0.9), (1.0, 0.0)])
arm_arc(tr, t_take, t_land + recover, "right", [(0.0, 0.0), (0.5, -0.25), ((t_land + 2 - t_take) / (t_land + recover - t_take), 0.35), (1.0, 0.0)], joint="spine_01")