extends SkeletonModifier3D ## Weapon hold arm modifier (Boss 2026-10-21 "the staff is glued along the arm"; weapon_hold.gd adds it to a rigged ## hero's skeleton for every held weapon but the two-hand fire gun, which has gun_pose.gd). Runs after the ## AnimationPlayer every frame: ## 1. upright carry (staves, the fire rod, bows): while the hero stands / walks / runs / rides (clip in `clips`) ## the right arm carries the weapon the way a staff is held - upper arm down by the side, elbow bent, forearm ## forward, the fist turned so the shaft (weapon mount +Z, through the fist) stands up beside the body. Any other ## clip (strikes, skills, hurt, cast...) plays untouched: the carry fades out in ~0.07 s and back in after; ## 2. ground guard (every weapon): when a clip would push the weapon's far end into the ground (generic strike / ## thrust clips were never made for a held blade), the wrist turns it up just enough to stay above the ground; ## what the wrist cannot clear the shoulder raises, then the wrist once more (a long Solar blade in a crouched ## thrust went 0.47 m into the ground); ## 3. two-hand guard carry (two-hand swords, katanas, two-hand axes / spears; weapon polish 2026-10-22 "a greatsword ## held in one hand"): while standing / walking / running / riding both fists meet in front of the belly, the ## blade forward and up (the chibi arms are too short to reach the right fist where the clips hold it); ## 4. off hand (every two-hand weapon, not with a shield): the left arm (two-bone IK) closes its fist on the ## hilt / shaft next to the right fist in every clip where it can reach it; it lets go (fades out) when the clip ## carries the fist out of reach or needs the hand (cast, spell, throw, shoot...). ## Directions come from the rest pose (up = pelvis -> head, forward = toes), so it works on every Tripo hero rig. const ARM := ["R_Upperarm", "R_Forearm", "R_Hand"] const ARM_L := ["L_Upperarm", "L_Forearm", "L_Hand"] const GRIP := Vector3(0, -0.42, 0) ## the left fist in L_Hand bone space (the right one, HAND_GRIP, mirrored across the bone's X) const HAND_GRIP_L := Vector3(0.012, 0.04, 0.0) ## clips that need the off hand for themselves (it lets go of the hilt) const OFF_FREE := ["cast", "spell", "powerup", "summon", "throw", "taunt", "cheer", "sit", "death", "block", "guard", "shoot", "jump"] ## player.gd view (dead / sitting / clip / ground) and its weapon mount (weapon_parent, under the R_Hand attachment). var view = null var mount: Node3D = null ## Upright carry on (staves / rod / bows) and the clips it covers. var upright := false var clips: Array = ["idle", "walk", "run"] ## Weapon ends in mount space (the point farthest from the grip + the farthest one the other way, for staves / ## bows / spears that reach past the fist both ways; weapon_hold.gd) and the ground clearance (m). var tips: Array = [] var clearance := 0.04 ## Carry shape: elbow (forward / out, fractions), forearm (up / out), shaft lean (forward / out). var elbow_fwd := 0.22 var elbow_out := 0.22 var fore_up := 0.18 var fore_out := 0.3 var lean_fwd := 0.1 var lean_out := 0.08 ## 0..1 carry blend now, frames solved, last ground lift (radians) - tests read them. var weight := 0.0 var runs := 0 var lifted := 0.0 ## Final skeleton-space poses of upper arm / forearm / hand after this frame's modification (the skeleton restores ## the clip pose after modifiers, so scripts reading bones later see the unmodified arm - tests read these) and the ## guard's last numbers (debug). var arm_final: Array = [] var dbg := {} var _b: Array = [] var _up := Vector3.UP var _fwd := Vector3.FORWARD var _left := Vector3.LEFT var _ok := false var _head := -1 ## shoulder width (skeleton units, rest pose) and the shaft's wanted clearance from the head in shoulder widths var _shoulders := 0.3 var head_clear := 0.55 ## last solve's shaft -> head distance (tests) var head_gap := 0.0 ## Two-hand weapon: the off hand grips it (`two_hand`), the guard carry holds it in front (`guard`, not the staves' ## upright carry); `guard_pitch` = blade above the horizontal (deg); `off_dist` = the off fist this far (weapon mount ## units = m) along the weapon from the grip, toward the tip (`off_both`: either side, the nearer one - shafts). var two_hand := false var guard := false var guard_pitch := 35.0 ## guard carry turned out to the weapon side (deg; 0 = straight ahead) so the blade does not stand in front of the face var guard_yaw := 0.0 var off_dist := 0.11 var off_both := false ## off-hand blend now, the off fist -> its spot on the weapon last frame (m, -1 = not gripping) and the left arm's final ## skeleton-space poses (tests / audit sheets read them) var off_w := 0.0 var off_gap := -1.0 var off_final: Array = [] var _lb: Array = [] ## last guard carry fist target (skeleton space, debug) var guard_fist := Vector3.ZERO func _setup() -> void: var sk := get_skeleton() if sk == null: return _b = [] for n in ARM: _b.append(sk.find_bone(n)) _lb = [] for n in ARM_L: _lb.append(sk.find_bone(n)) var head := sk.find_bone("Head") var pel := sk.find_bone("Pelvis") var lt := sk.find_bone("L_Thigh") var rt := sk.find_bone("R_Thigh") var lf := sk.find_bone("L_Foot") var ltoe := sk.find_bone("L_ToeBase") if -1 in _b or head < 0 or pel < 0 or lt < 0 or rt < 0 or lf < 0 or ltoe < 0: return _up = (sk.get_bone_global_rest(head).origin - sk.get_bone_global_rest(pel).origin).normalized() _head = head _shoulders = sk.get_bone_global_rest(_b[0]).origin.distance_to(sk.get_bone_global_rest(sk.find_bone("L_Upperarm")).origin) if sk.find_bone("L_Upperarm") >= 0 else 0.3 _left = sk.get_bone_global_rest(lt).origin - sk.get_bone_global_rest(rt).origin _left = (_left - _up * _left.dot(_up)).normalized() _fwd = _left.cross(_up).normalized() var toe: Vector3 = sk.get_bone_global_rest(ltoe).origin - sk.get_bone_global_rest(lf).origin if _fwd.dot(toe) < 0.0: _fwd = -_fwd _ok = true func up_dir() -> Vector3: return _up func forward_dir() -> Vector3: return _fwd func _alive() -> bool: return view != null and is_instance_valid(view) and mount != null and is_instance_valid(mount) ## The upright / guard carry is wanted now (a carry clip, nothing one-shot playing, alive, not sitting). func wanted() -> bool: if not (upright or guard) or not _alive(): return false if bool(view.get("dead")) or bool(view.get("sitting")): return false # one-shot clips (strikes, skills, hurt, cheer) have their own names: only the carry clips hold var an = view.get("anim") if an == null: return false # assigned_animation: a paused player (hit-stop) keeps its clip (player.gd reads it the same way) return str((an as AnimationPlayer).assigned_animation) in clips func _process_modification_with_delta(delta: float) -> void: var sk := get_skeleton() if sk == null or not _alive(): return if not _ok: _setup() if not _ok: return runs += 1 lifted = 0.0 var w := wanted() weight = move_toward(weight, 1.0 if w else 0.0, delta * (9.0 if w else 15.0)) if weight > 0.001: var q0: Array = [] for b in _b: q0.append(sk.get_bone_pose_rotation(b)) solve(sk) if weight < 0.999: var k := weight * weight * (3.0 - 2.0 * weight) for i in 3: var qt: Quaternion = sk.get_bone_pose_rotation(_b[i]) sk.set_bone_pose_rotation(_b[i], (q0[i] as Quaternion).slerp(qt, k)) # lowest end first, then the other one (a lift never pushes the first back under) for i in 5: _ground_guard(sk) # a crouched swing with a big-headed staff: turning the wrist cannot clear it - raise the arm at the shoulder _shoulder_lift(sk) # what the shoulder lift left (its turn is capped) the wrist takes again for i in 5: _ground_guard(sk) # a weapon reaching past the fist both ways (a staff, a Solar blade's hilt) in a crouch: a few more tries, each # undone when it leaves the weapon lower than before for i in 3: _kept(sk, false) _kept(sk, true) for i in 3: _kept(sk, false) arm_final = [glob(sk, _b[0]), glob(sk, _b[1]), glob(sk, _b[2])] # the off hand last: it follows the weapon wherever the carry / guard left it _off_hand(sk, delta) ## The weapon's lowest end now (world y, from `tips`). func _lowest(sk: Skeleton3D) -> float: var mt: Transform3D = sk.global_transform * glob(sk, _b[2]) * mount.transform var low := INF for t in tips: low = minf(low, (mt * (t as Vector3)).y) return low ## One ground guard step (wrist, or the shoulder lift), undone when it leaves the lowest end lower than before. func _kept(sk: Skeleton3D, shoulder: bool) -> void: if tips.is_empty(): return var q_u := sk.get_bone_pose_rotation(_b[0]) var q_h := sk.get_bone_pose_rotation(_b[2]) var l0 := lifted var before := _lowest(sk) if shoulder: _shoulder_lift(sk) else: _ground_guard(sk) if _lowest(sk) < before - 0.001: sk.set_bone_pose_rotation(_b[0], q_u) sk.set_bone_pose_rotation(_b[2], q_h) lifted = l0 ## Raises the whole arm at the shoulder by what the wrist turns could not clear (lowest weapon point below the ground). func _shoulder_lift(sk: Skeleton3D) -> void: if tips.is_empty(): return var skg := sk.global_transform var mt: Transform3D = skg * glob(sk, _b[2]) * mount.transform var low := Vector3(0, INF, 0) for t in tips: var q: Vector3 = mt * (t as Vector3) if q.y < low.y: low = q var floor_y: float = (view as Node3D).global_position.y + clearance if low.y >= floor_y: return var s: Vector3 = skg * glob(sk, _b[0]).origin var v := low - s var flat := Vector3(v.x, 0.0, v.z) if flat.length() < 0.05: return var a := asin(clampf((floor_y - low.y) / flat.length(), 0.0, 0.6)) var axis := Vector3.UP.cross(flat.normalized()).normalized() var bs := skg.basis.orthonormalized() var rs: Basis = bs.inverse() * Basis(axis, -a) * bs set_global_rot(sk, _b[0], rs * glob(sk, _b[0]).basis.orthonormalized()) lifted += a ## The carried shaft's lower end stays above the ground (world) with the fist where the arm has it now. func _butt_clear(sk: Skeleton3D, z: Vector3) -> bool: var below := 0.0 for t in tips: below = maxf(below, -((t as Vector3) - Vector3(0, -0.42, 0)).z) if below <= 0.0: return true below *= mount.transform.basis.z.length() var gp: Vector3 = glob(sk, _b[2]) * (mount.transform * Vector3(0, -0.42, 0)) var butt: Vector3 = sk.global_transform * (gp - z * below) return butt.y >= (view as Node3D).global_position.y + clearance + 0.01 ## Horizontal distance (skeleton space) from the Head bone to the shaft line through the fist along `z`. func _shaft_head_gap(sk: Skeleton3D, z: Vector3) -> float: if _head < 0: return INF var hand := glob(sk, _b[2]) var p: Vector3 = hand * (mount.transform * Vector3(0, -0.42, 0)) var v: Vector3 = glob(sk, _head).origin - p return (v - z * v.dot(z)).length() ## Global pose (skeleton space) of bone `b` from the local poses up its real parent chain (twist bones included; the ## skeleton's cached global poses can lag inside a modification). static func glob(sk: Skeleton3D, b: int) -> Transform3D: if b < 0: return Transform3D.IDENTITY return glob(sk, sk.get_bone_parent(b)) * sk.get_bone_pose(b) ## Sets bone `b` so its global rotation is `want` (its parent chain as it is now). static func set_global_rot(sk: Skeleton3D, b: int, want: Basis) -> void: var pb: Basis = glob(sk, sk.get_bone_parent(b)).basis.orthonormalized() sk.set_bone_pose_rotation(b, (pb.inverse() * want.orthonormalized()).get_rotation_quaternion()) ## Turns bone `b` so the bone `child` (any descendant) lies from it along `want`. static func aim(sk: Skeleton3D, b: int, child: int, want: Vector3) -> void: var g := glob(sk, b) var cur: Vector3 = glob(sk, child).origin - g.origin if cur.length() < 0.000001: return set_global_rot(sk, b, Basis(Quaternion(cur.normalized(), want.normalized())) * g.basis.orthonormalized()) ## Poses the arm fully into the upright carry (skeleton space). Narrow bodies (the pilot novice) would bring the fist ## in front of the chest and the shaft across the face: the elbow / forearm open out until the shaft clears the head ## by `head_clear` x the shoulder width. func solve(sk: Skeleton3D) -> void: if guard: solve_guard(sk) return var bu: int = _b[0] var bf: int = _b[1] var bh: int = _b[2] var out := -_left var spread := 0.0 var lift := 0.0 var f_dir := Vector3.ZERO var z := (_up + _fwd * lean_fwd + out * lean_out).normalized() for step in 6: var e_dir := (-_up + _fwd * (elbow_fwd + lift * 0.5) + out * (elbow_out + spread * 0.6)).normalized() f_dir = (_fwd + _up * (fore_up + lift) + out * (fore_out + spread)).normalized() aim(sk, bu, bf, e_dir) aim(sk, bf, bh, f_dir) var d := _shaft_head_gap(sk, z) head_gap = d var low_ok := _butt_clear(sk, z) if (d >= head_clear * _shoulders or spread >= 0.9) and (low_ok or lift >= 0.9): break if d < head_clear * _shoulders and spread < 0.9: spread += 0.3 if not low_ok and lift < 0.9: # short bodies: the shaft's butt would touch the ground - carry it higher instead of tilting it lift += 0.3 # the fist: weapon mount +Z (the shaft through the fist) up with a little lean, mount +Y back along the forearm var y := -f_dir y = (y - z * y.dot(z)).normalized() var mb := Basis(y.cross(z).normalized(), y, z) set_global_rot(sk, bh, mb * mount.transform.basis.orthonormalized().inverse()) # scaled bones: measure the shaft as posed and turn the hand the rest of the way for k in 2: var hg := glob(sk, bh) var cz: Vector3 = (hg.basis * mount.transform.basis).z.normalized() if cz.dot(z) > 0.9999: break set_global_rot(sk, bh, Basis(Quaternion(cz, z)) * hg.basis.orthonormalized()) ## Wrist lift so the weapon's far end stays `clearance` above the ground under the hero. func _ground_guard(sk: Skeleton3D) -> void: if tips.is_empty(): return var bh: int = _b[2] var skg := sk.global_transform var hg := glob(sk, bh) var hand_w := skg * hg var mt := hand_w * mount.transform var grip: Vector3 = mt * Vector3(0, -0.42, 0) var tp: Vector3 = mt * (tips[0] as Vector3) for t in tips: var q: Vector3 = mt * (t as Vector3) if q.y < tp.y: tp = q var floor_y: float = (view as Node3D).global_position.y + clearance dbg = {"tip": snappedf(tp.y, 0.01), "grip": snappedf(grip.y, 0.01), "floor": snappedf(floor_y, 0.01), "n": tips.size()} # a fist at / just under the ground (a roll / crouched whirl) can still turn the far end up over it: only a fist # buried deep leaves it to the shoulder lift if tp.y >= floor_y or grip.y <= floor_y - 0.1: return var v := tp - grip var l := v.length() if l < 0.05: return var e := asin(clampf(v.y / l, -1.0, 1.0)) # a hair over the floor: a wide blade turned about one axis drops its other edge a little var et := asin(clampf((floor_y + 0.015 - grip.y) / l, -1.0, 1.0)) var flat := Vector3(v.x, 0.0, v.z) if flat.length() < 0.02: # straight down: tip it toward the hero's forward var f: Vector3 = (skg.basis * _fwd) flat = Vector3(f.x, 0.0, f.z) # axis = up x flat: a rotation by -a about it raises v by a var axis := Vector3.UP.cross(flat.normalized()).normalized() var a := et - e lifted += a var rw := Basis(axis, -a) # world rotation about the grip -> hand bone (skeleton space, about its own origin: the fist is a few cm off) var bs := skg.basis.orthonormalized() var rs: Basis = bs.inverse() * rw * bs set_global_rot(sk, bh, rs * hg.basis.orthonormalized()) # ---------------------------------------------------------------- two-hand weapons (guard carry + off hand) ## Shoulder -> wrist length of an arm chain (skeleton units, as posed). static func reach_of(sk: Skeleton3D, chain: Array) -> float: var s := glob(sk, chain[0]).origin var e := glob(sk, chain[1]).origin return s.distance_to(e) + e.distance_to(glob(sk, chain[2]).origin) ## Two-bone IK (skeleton space): turns upper arm + forearm so the fist (`grip`, hand-bone space) reaches `target` ## with the hand's global rotation `hb`, the elbow bent toward `pole`. Returns the fist's miss (skeleton units). static func reach(sk: Skeleton3D, chain: Array, target: Vector3, hb: Basis, grip: Vector3, pole: Vector3) -> float: var bu: int = chain[0] var bf: int = chain[1] var bh: int = chain[2] var hs: Vector3 = glob(sk, bh).basis.get_scale() var wrist := target - hb.orthonormalized() * (grip * hs) for it in 2: var s := glob(sk, bu).origin var e0 := glob(sk, bf).origin var a := s.distance_to(e0) var b := e0.distance_to(glob(sk, bh).origin) var v := wrist - s var d := clampf(v.length(), 0.0001, (a + b) * 0.999) var dir := v.normalized() var ca := clampf((a * a + d * d - b * b) / (2.0 * a * d), -1.0, 1.0) var p := pole - dir * pole.dot(dir) if p.length() < 0.0001: p = (e0 - s) - dir * (e0 - s).dot(dir) p = p.normalized() var e := s + dir * (a * ca) + p * (a * sqrt(maxf(0.0, 1.0 - ca * ca))) aim(sk, bu, bf, e - s) aim(sk, bf, bh, wrist - glob(sk, bf).origin) set_global_rot(sk, bh, hb) # a hand bone off its parent's axis / a scaled bone: correct by the miss once var miss: Vector3 = target - glob(sk, bh) * grip if miss.length() < 0.002: break wrist += miss return (glob(sk, bh) * grip).distance_to(target) ## Skeleton units per weapon-mount unit (the mount is solved in world metres). func _mount_len(sk: Skeleton3D) -> float: return (glob(sk, _b[2]).basis * mount.transform.basis).z.length() ## Right hand rotation that turns the weapon mount to +Z = `z` (the weapon's way), +Y back along the forearm `f`. func _hand_for(z: Vector3, f: Vector3) -> Basis: var y := -f y = (y - z * y.dot(z)).normalized() return Basis(y.cross(z).normalized(), y, z) * mount.transform.basis.orthonormalized().inverse() ## Left hand rotation gripping the weapon like the right one, mirrored (both thumbs toward the tip): the right hand's ## mount frame mirrored across the hand bone's X (Tripo L / R hand bones are mirror images) turned to +Z = `z`. func _off_basis(z: Vector3, f: Vector3) -> Basis: var m := mount.transform.basis.orthonormalized() var fy := Vector3(-m.y.x, m.y.y, m.y.z) var fz := Vector3(-m.z.x, m.z.y, m.z.z) var vm := Basis(fy.cross(fz).normalized(), fy, fz) var y := -f y = (y - z * y.dot(z)).normalized() return Basis(y.cross(z).normalized(), y, z) * vm.inverse() ## Two-hand guard carry: the right fist in front of the belly a little right of centre, the weapon forward and ## `guard_pitch` up, close enough for the left fist to reach its spot on the weapon (`off_dist` along it). func solve_guard(sk: Skeleton3D) -> void: var rs := glob(sk, _b[0]).origin var ra := reach_of(sk, _b) var ls := rs var la := ra if _lb.size() == 3 and not (-1 in _lb): ls = glob(sk, _lb[0]).origin la = reach_of(sk, _lb) var out := -_left var mid := (rs + ls) * 0.5 var pr := deg_to_rad(guard_pitch) var yw := deg_to_rad(guard_yaw) var z := ((_fwd * cos(yw) + out * sin(yw)) * cos(pr) + _up * sin(pr)).normalized() var k := _mount_len(sk) var fist := mid + _fwd * (0.55 * ra) - _up * (0.6 * ra) + out * ((0.15 + 0.35 * sin(yw)) * _shoulders) var home := mid + _fwd * (0.35 * ra) - _up * (0.45 * ra) for i in 8: if fist.distance_to(rs) <= ra * 0.92 and (fist + z * (off_dist * k)).distance_to(ls) <= la * 0.92: break fist = fist.lerp(home, 0.35) guard_fist = fist var pole := -_up + out * 0.7 - _fwd * 0.3 var grip: Vector3 = mount.transform * GRIP var f := (fist - rs).normalized() for p in 2: reach(sk, _b, fist, _hand_for(z, f), grip, pole) f = (glob(sk, _b[2]).origin - glob(sk, _b[1]).origin).normalized() ## The off hand may hold the weapon now (a two-hand weapon, alive, not sitting, the clip does not need the hand). func off_wanted() -> bool: if not two_hand or not _alive() or _lb.size() != 3 or -1 in _lb: return false if bool(view.get("dead")) or bool(view.get("sitting")): return false var an = view.get("anim") if an == null: return false return not (str((an as AnimationPlayer).assigned_animation) in OFF_FREE) ## Left fist onto the weapon (`off_dist` from the grip along it) by two-bone IK; fades out while it cannot reach. func _off_hand(sk: Skeleton3D, delta: float) -> void: off_gap = -1.0 off_final = [] if not two_hand or _lb.size() != 3 or -1 in _lb: return var mt: Transform3D = glob(sk, _b[2]) * mount.transform var ls := glob(sk, _lb[0]).origin var t: Vector3 = mt * (GRIP + Vector3(0, 0, off_dist)) if off_both: var t2: Vector3 = mt * (GRIP - Vector3(0, 0, off_dist)) if t2.distance_to(ls) < t.distance_to(ls): t = t2 var r := ls.distance_to(t) / maxf(reach_of(sk, _lb), 0.0001) var tw := clampf((1.2 - r) / 0.2, 0.0, 1.0) if off_wanted() else 0.0 off_w = move_toward(off_w, tw, delta * (10.0 if tw > off_w else 14.0)) if off_w < 0.001: return var q0: Array = [] for b in _lb: q0.append(sk.get_bone_pose_rotation(b)) var z: Vector3 = mt.basis.z.normalized() var pole := -_up + _left * 0.7 - _fwd * 0.3 var f := (t - ls).normalized() for p in 2: reach(sk, _lb, t, _off_basis(z, f), HAND_GRIP_L, pole) f = (glob(sk, _lb[2]).origin - glob(sk, _lb[1]).origin).normalized() if off_w < 0.999: var kk := off_w * off_w * (3.0 - 2.0 * off_w) for i in 3: var qt: Quaternion = sk.get_bone_pose_rotation(_lb[i]) sk.set_bone_pose_rotation(_lb[i], (q0[i] as Quaternion).slerp(qt, kk)) off_final = [glob(sk, _lb[0]), glob(sk, _lb[1]), glob(sk, _lb[2])] off_gap = (glob(sk, _lb[2]) * HAND_GRIP_L).distance_to(t) * sk.global_transform.basis.get_scale().x