pose_controller.cpp 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823
  1. #include "pose_controller.h"
  2. #include "humanoid_math.h"
  3. #include "spear_pose_utils.h"
  4. #include <QVector3D>
  5. #include <algorithm>
  6. #include <cmath>
  7. #include <numbers>
  8. namespace Render::GL {
  9. HumanoidPoseController::HumanoidPoseController(
  10. HumanoidPose &pose, const HumanoidAnimationContext &anim_ctx)
  11. : m_pose(pose), m_anim_ctx(anim_ctx) {}
  12. void HumanoidPoseController::standIdle() {}
  13. void HumanoidPoseController::kneel(float depth) {
  14. using HP = HumanProportions;
  15. depth = std::clamp(depth, 0.0F, 1.0F);
  16. if (depth < 1e-6F) {
  17. return;
  18. }
  19. float const kneel_offset = depth * 0.40F;
  20. float const pelvis_y = HP::WAIST_Y - kneel_offset;
  21. m_pose.pelvis_pos.setY(pelvis_y);
  22. float const stance_narrow = 0.11F;
  23. float const left_knee_y = HP::GROUND_Y + 0.07F * depth;
  24. float const left_knee_z = -0.06F * depth;
  25. m_pose.knee_l = QVector3D(-stance_narrow, left_knee_y, left_knee_z);
  26. m_pose.foot_l = QVector3D(-stance_narrow - 0.025F, HP::GROUND_Y,
  27. left_knee_z - HP::LOWER_LEG_LEN * 0.93F * depth);
  28. float const right_knee_y = pelvis_y - 0.12F;
  29. float const right_foot_z = 0.28F * depth;
  30. m_pose.knee_r = QVector3D(stance_narrow, right_knee_y, right_foot_z - 0.05F);
  31. m_pose.foot_r = QVector3D(stance_narrow, HP::GROUND_Y + m_pose.foot_y_offset,
  32. right_foot_z);
  33. float const upper_body_drop = kneel_offset;
  34. m_pose.shoulder_l.setY(m_pose.shoulder_l.y() - upper_body_drop);
  35. m_pose.shoulder_r.setY(m_pose.shoulder_r.y() - upper_body_drop);
  36. m_pose.neck_base.setY(m_pose.neck_base.y() - upper_body_drop);
  37. m_pose.head_pos.setY(m_pose.head_pos.y() - upper_body_drop);
  38. }
  39. void HumanoidPoseController::lean(const QVector3D &direction, float amount) {
  40. amount = std::clamp(amount, 0.0F, 1.0F);
  41. QVector3D dir = direction;
  42. if (dir.lengthSquared() > 1e-6F) {
  43. dir.normalize();
  44. } else {
  45. dir = QVector3D(0.0F, 0.0F, 1.0F);
  46. }
  47. float const lean_magnitude = 0.12F * amount;
  48. QVector3D const lean_offset = dir * lean_magnitude;
  49. m_pose.shoulder_l += lean_offset;
  50. m_pose.shoulder_r += lean_offset;
  51. m_pose.neck_base += lean_offset * 0.85F;
  52. m_pose.head_pos += lean_offset * 0.75F;
  53. }
  54. void HumanoidPoseController::placeHandAt(bool is_left,
  55. const QVector3D &target_position) {
  56. get_hand(is_left) = target_position;
  57. const QVector3D &shoulder = get_shoulder(is_left);
  58. const QVector3D outward_dir = compute_outward_dir(is_left);
  59. float const along_frac = is_left ? 0.45F : 0.48F;
  60. float const lateral_offset = is_left ? 0.15F : 0.12F;
  61. float const y_bias = is_left ? -0.08F : 0.02F;
  62. float const outward_sign = 1.0F;
  63. get_elbow(is_left) =
  64. solve_elbow_ik(is_left, shoulder, target_position, outward_dir,
  65. along_frac, lateral_offset, y_bias, outward_sign);
  66. }
  67. auto HumanoidPoseController::solve_elbow_ik(
  68. bool, const QVector3D &shoulder, const QVector3D &hand,
  69. const QVector3D &outward_dir, float along_frac, float lateral_offset,
  70. float y_bias, float outward_sign) const -> QVector3D {
  71. return elbow_bend_torso(shoulder, hand, outward_dir, along_frac,
  72. lateral_offset, y_bias, outward_sign);
  73. }
  74. auto HumanoidPoseController::solve_knee_ik(
  75. bool is_left, const QVector3D &hip, const QVector3D &foot,
  76. float height_scale) const -> QVector3D {
  77. using HP = HumanProportions;
  78. QVector3D hip_to_foot = foot - hip;
  79. float const distance = hip_to_foot.length();
  80. if (distance < 1e-5F) {
  81. return hip;
  82. }
  83. float const upper_len = HP::UPPER_LEG_LEN * height_scale;
  84. float const lower_len = HP::LOWER_LEG_LEN * height_scale;
  85. float const reach = upper_len + lower_len;
  86. float const min_reach =
  87. std::max(std::abs(upper_len - lower_len) + 1e-4F, 1e-3F);
  88. float const max_reach = std::max(reach - 1e-4F, min_reach + 1e-4F);
  89. float const clamped_dist = std::clamp(distance, min_reach, max_reach);
  90. QVector3D const dir = hip_to_foot / distance;
  91. float cos_theta = (upper_len * upper_len + clamped_dist * clamped_dist -
  92. lower_len * lower_len) /
  93. (2.0F * upper_len * clamped_dist);
  94. cos_theta = std::clamp(cos_theta, -1.0F, 1.0F);
  95. float const sin_theta =
  96. std::sqrt(std::max(0.0F, 1.0F - cos_theta * cos_theta));
  97. QVector3D bend_pref =
  98. is_left ? QVector3D(-0.24F, 0.0F, 0.95F) : QVector3D(0.24F, 0.0F, 0.95F);
  99. bend_pref.normalize();
  100. QVector3D bend_axis = bend_pref - dir * QVector3D::dotProduct(dir, bend_pref);
  101. if (bend_axis.lengthSquared() < 1e-6F) {
  102. bend_axis = QVector3D::crossProduct(dir, QVector3D(0.0F, 1.0F, 0.0F));
  103. if (bend_axis.lengthSquared() < 1e-6F) {
  104. bend_axis = QVector3D::crossProduct(dir, QVector3D(1.0F, 0.0F, 0.0F));
  105. }
  106. }
  107. bend_axis.normalize();
  108. QVector3D knee =
  109. hip + dir * (cos_theta * upper_len) + bend_axis * (sin_theta * upper_len);
  110. float const knee_floor = HP::GROUND_Y + m_pose.foot_y_offset * 0.5F;
  111. if (knee.y() < knee_floor) {
  112. knee.setY(knee_floor);
  113. }
  114. if (knee.y() > hip.y()) {
  115. knee.setY(hip.y());
  116. }
  117. return knee;
  118. }
  119. auto HumanoidPoseController::get_shoulder(bool is_left) const
  120. -> const QVector3D & {
  121. return is_left ? m_pose.shoulder_l : m_pose.shoulder_r;
  122. }
  123. auto HumanoidPoseController::get_hand(bool is_left) -> QVector3D & {
  124. return is_left ? m_pose.hand_l : m_pose.hand_r;
  125. }
  126. auto HumanoidPoseController::get_hand(bool is_left) const -> const QVector3D & {
  127. return is_left ? m_pose.hand_l : m_pose.hand_r;
  128. }
  129. auto HumanoidPoseController::get_elbow(bool is_left) -> QVector3D & {
  130. return is_left ? m_pose.elbow_l : m_pose.elbow_r;
  131. }
  132. auto HumanoidPoseController::compute_right_axis() const -> QVector3D {
  133. QVector3D right_axis = m_pose.shoulder_r - m_pose.shoulder_l;
  134. right_axis.setY(0.0F);
  135. if (right_axis.lengthSquared() < 1e-8F) {
  136. right_axis = QVector3D(1.0F, 0.0F, 0.0F);
  137. }
  138. right_axis.normalize();
  139. return right_axis;
  140. }
  141. auto HumanoidPoseController::compute_outward_dir(bool is_left) const
  142. -> QVector3D {
  143. QVector3D const right_axis = compute_right_axis();
  144. return is_left ? -right_axis : right_axis;
  145. }
  146. auto HumanoidPoseController::get_shoulder_y(bool is_left) const -> float {
  147. return is_left ? m_pose.shoulder_l.y() : m_pose.shoulder_r.y();
  148. }
  149. auto HumanoidPoseController::get_pelvis_y() const -> float {
  150. return m_pose.pelvis_pos.y();
  151. }
  152. void HumanoidPoseController::aim_bow(float draw_phase) {
  153. using HP = HumanProportions;
  154. draw_phase = std::clamp(draw_phase, 0.0F, 1.0F);
  155. QVector3D const aim_pos(-0.02F, HP::SHOULDER_Y + 0.18F, 0.42F);
  156. QVector3D const draw_pos(-0.05F, HP::SHOULDER_Y + 0.12F, 0.22F);
  157. QVector3D const release_pos(-0.02F, HP::SHOULDER_Y + 0.20F, 0.34F);
  158. QVector3D hand_l_target;
  159. float shoulder_twist = 0.0F;
  160. float head_recoil = 0.0F;
  161. if (draw_phase < 0.20F) {
  162. float t = draw_phase / 0.20F;
  163. t = t * t;
  164. hand_l_target = aim_pos * (1.0F - t) + draw_pos * t;
  165. shoulder_twist = t * 0.08F;
  166. } else if (draw_phase < 0.50F) {
  167. hand_l_target = draw_pos;
  168. shoulder_twist = 0.08F;
  169. } else if (draw_phase < 0.58F) {
  170. float t = (draw_phase - 0.50F) / 0.08F;
  171. t = t * t * t;
  172. hand_l_target = draw_pos * (1.0F - t) + release_pos * t;
  173. shoulder_twist = 0.08F * (1.0F - t * 0.6F);
  174. head_recoil = t * 0.04F;
  175. } else {
  176. float t = (draw_phase - 0.58F) / 0.42F;
  177. t = 1.0F - (1.0F - t) * (1.0F - t);
  178. hand_l_target = release_pos * (1.0F - t) + aim_pos * t;
  179. shoulder_twist = 0.08F * 0.4F * (1.0F - t);
  180. head_recoil = 0.04F * (1.0F - t);
  181. }
  182. QVector3D const hand_r_target(0.03F, HP::SHOULDER_Y + 0.08F, 0.55F);
  183. placeHandAt(false, hand_r_target);
  184. placeHandAt(true, hand_l_target);
  185. if (shoulder_twist > 0.01F) {
  186. m_pose.shoulder_l.setY(m_pose.shoulder_l.y() + shoulder_twist);
  187. m_pose.shoulder_r.setY(m_pose.shoulder_r.y() - shoulder_twist * 0.5F);
  188. }
  189. if (head_recoil > 0.01F) {
  190. m_pose.head_pos.setZ(m_pose.head_pos.z() - head_recoil);
  191. }
  192. }
  193. void HumanoidPoseController::meleeStrike(float strike_phase) {
  194. using HP = HumanProportions;
  195. strike_phase = std::clamp(strike_phase, 0.0F, 1.0F);
  196. // Define key positions for a natural horizontal slash toward the target
  197. QVector3D const rest_pos(0.22F, HP::SHOULDER_Y + 0.02F, 0.18F);
  198. QVector3D const chamber_pos(0.30F, HP::SHOULDER_Y + 0.08F, 0.05F);
  199. QVector3D const strike_pos(0.28F, HP::SHOULDER_Y - 0.05F, 0.65F);
  200. QVector3D const followthrough_pos(0.10F, HP::SHOULDER_Y - 0.12F, 0.55F);
  201. QVector3D hand_r_target;
  202. QVector3D hand_l_target;
  203. // Body dynamics
  204. float torso_twist = 0.0F;
  205. float forward_lean = 0.0F;
  206. float shoulder_dip = 0.0F;
  207. float step_forward = 0.0F;
  208. if (strike_phase < 0.20F) {
  209. // Phase 1: Chamber - pull weapon back, twist torso away
  210. float t = strike_phase / 0.20F;
  211. float ease_t = t * t;
  212. hand_r_target = rest_pos * (1.0F - ease_t) + chamber_pos * ease_t;
  213. hand_l_target = QVector3D(-0.18F, HP::SHOULDER_Y + 0.02F, 0.22F - 0.08F * t);
  214. // Twist torso back to coil for the strike
  215. torso_twist = -0.04F * ease_t;
  216. shoulder_dip = -0.02F * ease_t;
  217. } else if (strike_phase < 0.28F) {
  218. // Phase 2: Brief anticipation hold
  219. hand_r_target = chamber_pos;
  220. hand_l_target = QVector3D(-0.18F, HP::SHOULDER_Y + 0.02F, 0.14F);
  221. torso_twist = -0.04F;
  222. shoulder_dip = -0.02F;
  223. } else if (strike_phase < 0.48F) {
  224. // Phase 3: Explosive strike - uncoil torso, step forward
  225. float t = (strike_phase - 0.28F) / 0.20F;
  226. float power_t = t * t * (3.0F - 2.0F * t); // smoothstep for power
  227. hand_r_target = chamber_pos * (1.0F - power_t) + strike_pos * power_t;
  228. hand_l_target = QVector3D(-0.18F + 0.06F * power_t,
  229. HP::SHOULDER_Y + 0.02F - 0.08F * power_t,
  230. 0.14F + 0.20F * power_t);
  231. // Uncoil torso forward and rotate into strike
  232. torso_twist = -0.04F + 0.10F * power_t;
  233. forward_lean = 0.08F * power_t;
  234. shoulder_dip = -0.02F + 0.05F * power_t;
  235. step_forward = 0.06F * power_t;
  236. } else if (strike_phase < 0.65F) {
  237. // Phase 4: Follow-through - weapon continues past target
  238. float t = (strike_phase - 0.48F) / 0.17F;
  239. float ease_t = t * t;
  240. hand_r_target = strike_pos * (1.0F - ease_t) + followthrough_pos * ease_t;
  241. hand_l_target = QVector3D(-0.12F, HP::SHOULDER_Y - 0.06F, 0.34F);
  242. torso_twist = 0.06F - 0.02F * t;
  243. forward_lean = 0.08F - 0.03F * t;
  244. shoulder_dip = 0.03F;
  245. step_forward = 0.06F;
  246. } else {
  247. // Phase 5: Recovery - return to guard
  248. float t = (strike_phase - 0.65F) / 0.35F;
  249. float ease_t = 1.0F - (1.0F - t) * (1.0F - t);
  250. hand_r_target = followthrough_pos * (1.0F - ease_t) + rest_pos * ease_t;
  251. hand_l_target = QVector3D(-0.12F + (-0.18F + 0.12F) * ease_t,
  252. HP::SHOULDER_Y - 0.06F * (1.0F - ease_t) +
  253. 0.02F * ease_t,
  254. 0.34F * (1.0F - ease_t) + 0.22F * ease_t);
  255. torso_twist = 0.04F * (1.0F - ease_t);
  256. forward_lean = 0.05F * (1.0F - ease_t);
  257. shoulder_dip = 0.03F * (1.0F - ease_t);
  258. step_forward = 0.06F * (1.0F - ease_t);
  259. }
  260. // Apply body dynamics
  261. if (std::abs(torso_twist) > 0.001F) {
  262. m_pose.shoulder_r.setZ(m_pose.shoulder_r.z() + torso_twist);
  263. m_pose.shoulder_l.setZ(m_pose.shoulder_l.z() - torso_twist * 0.5F);
  264. }
  265. if (forward_lean > 0.001F) {
  266. m_pose.shoulder_l.setZ(m_pose.shoulder_l.z() + forward_lean);
  267. m_pose.shoulder_r.setZ(m_pose.shoulder_r.z() + forward_lean);
  268. m_pose.neck_base.setZ(m_pose.neck_base.z() + forward_lean * 0.8F);
  269. m_pose.head_pos.setZ(m_pose.head_pos.z() + forward_lean * 0.6F);
  270. }
  271. if (std::abs(shoulder_dip) > 0.001F) {
  272. m_pose.shoulder_r.setY(m_pose.shoulder_r.y() + shoulder_dip);
  273. }
  274. if (step_forward > 0.001F) {
  275. m_pose.foot_r.setZ(m_pose.foot_r.z() + step_forward);
  276. m_pose.knee_r.setZ(m_pose.knee_r.z() + step_forward * 0.5F);
  277. }
  278. placeHandAt(false, hand_r_target);
  279. placeHandAt(true, hand_l_target);
  280. }
  281. void HumanoidPoseController::graspTwoHanded(const QVector3D &grip_center,
  282. float hand_separation) {
  283. hand_separation = std::clamp(hand_separation, 0.1F, 0.8F);
  284. QVector3D const right_axis = compute_right_axis();
  285. QVector3D const right_hand_pos =
  286. grip_center + right_axis * (hand_separation * 0.5F);
  287. QVector3D const left_hand_pos =
  288. grip_center - right_axis * (hand_separation * 0.5F);
  289. placeHandAt(false, right_hand_pos);
  290. placeHandAt(true, left_hand_pos);
  291. }
  292. void HumanoidPoseController::spearThrust(float attack_phase) {
  293. using HP = HumanProportions;
  294. attack_phase = std::clamp(attack_phase, 0.0F, 1.0F);
  295. QVector3D const guard_pos(0.28F, HP::SHOULDER_Y + 0.05F, 0.25F);
  296. QVector3D const prepare_pos(0.35F, HP::SHOULDER_Y + 0.08F, 0.05F);
  297. QVector3D const thrust_pos(0.32F, HP::SHOULDER_Y + 0.10F, 0.90F);
  298. QVector3D const recover_pos(0.28F, HP::SHOULDER_Y + 0.06F, 0.40F);
  299. QVector3D hand_r_target;
  300. QVector3D hand_l_target;
  301. auto easeInOutCubic = [](float t) {
  302. return t < 0.5F ? 4.0F * t * t * t
  303. : 1.0F - std::pow(-2.0F * t + 2.0F, 3.0F) / 2.0F;
  304. };
  305. auto smoothstep = [](float edge0, float edge1, float x) {
  306. float t = std::clamp((x - edge0) / (edge1 - edge0), 0.0F, 1.0F);
  307. return t * t * (3.0F - 2.0F * t);
  308. };
  309. auto lerp = [](float a, float b, float t) { return a * (1.0F - t) + b * t; };
  310. if (attack_phase < 0.20F) {
  311. float const t = easeInOutCubic(attack_phase / 0.20F);
  312. hand_r_target = guard_pos * (1.0F - t) + prepare_pos * t;
  313. hand_l_target = QVector3D(-0.10F, HP::SHOULDER_Y - 0.05F,
  314. 0.20F * (1.0F - t) + 0.08F * t);
  315. } else if (attack_phase < 0.30F) {
  316. hand_r_target = prepare_pos;
  317. hand_l_target = QVector3D(-0.10F, HP::SHOULDER_Y - 0.05F, 0.08F);
  318. } else if (attack_phase < 0.50F) {
  319. float t = (attack_phase - 0.30F) / 0.20F;
  320. t = t * t * t;
  321. hand_r_target = prepare_pos * (1.0F - t) + thrust_pos * t;
  322. hand_l_target =
  323. QVector3D(-0.10F + 0.05F * t, HP::SHOULDER_Y - 0.05F + 0.03F * t,
  324. 0.08F + 0.45F * t);
  325. } else if (attack_phase < 0.70F) {
  326. float const t = easeInOutCubic((attack_phase - 0.50F) / 0.20F);
  327. hand_r_target = thrust_pos * (1.0F - t) + recover_pos * t;
  328. hand_l_target = QVector3D(-0.05F * (1.0F - t) - 0.10F * t,
  329. HP::SHOULDER_Y - 0.02F * (1.0F - t) - 0.06F * t,
  330. lerp(0.53F, 0.35F, t));
  331. } else {
  332. float const t = smoothstep(0.70F, 1.0F, attack_phase);
  333. hand_r_target = recover_pos * (1.0F - t) + guard_pos * t;
  334. hand_l_target =
  335. QVector3D(-0.10F - 0.02F * (1.0F - t),
  336. HP::SHOULDER_Y - 0.06F + 0.01F * t, lerp(0.35F, 0.25F, t));
  337. }
  338. float const thrust_extent =
  339. std::clamp((attack_phase - 0.20F) / 0.60F, 0.0F, 1.0F);
  340. float const along_offset = -0.06F + 0.02F * thrust_extent;
  341. float const y_drop = 0.10F + 0.02F * thrust_extent;
  342. hand_l_target = computeOffhandSpearGrip(m_pose, m_anim_ctx, hand_r_target,
  343. false, along_offset, y_drop, -0.08F);
  344. placeHandAt(false, hand_r_target);
  345. placeHandAt(true, hand_l_target);
  346. }
  347. void HumanoidPoseController::sword_slash(float attack_phase) {
  348. using HP = HumanProportions;
  349. attack_phase = std::clamp(attack_phase, 0.0F, 1.0F);
  350. // Key positions for a diagonal downward slash
  351. QVector3D const rest_pos(0.20F, HP::SHOULDER_Y + 0.05F, 0.15F);
  352. QVector3D const chamber_pos(0.28F, HP::SHOULDER_Y + 0.20F, 0.02F);
  353. QVector3D const apex_pos(0.30F, HP::SHOULDER_Y + 0.25F, 0.08F);
  354. QVector3D const strike_pos(0.18F, HP::SHOULDER_Y - 0.15F, 0.62F);
  355. QVector3D const followthrough_pos(0.05F, HP::WAIST_Y + 0.10F, 0.50F);
  356. QVector3D const recover_pos(0.22F, HP::SHOULDER_Y + 0.02F, 0.22F);
  357. QVector3D hand_r_target;
  358. QVector3D hand_l_target;
  359. // Body dynamics
  360. float torso_twist = 0.0F;
  361. float forward_lean = 0.0F;
  362. float shoulder_rotation = 0.0F;
  363. float weight_shift = 0.0F;
  364. auto smoothstep = [](float t) { return t * t * (3.0F - 2.0F * t); };
  365. auto ease_out = [](float t) { return 1.0F - (1.0F - t) * (1.0F - t); };
  366. auto ease_in = [](float t) { return t * t; };
  367. if (attack_phase < 0.15F) {
  368. // Phase 1: Prepare - raise weapon to chamber position
  369. float t = attack_phase / 0.15F;
  370. float ease_t = ease_in(t);
  371. hand_r_target = rest_pos * (1.0F - ease_t) + chamber_pos * ease_t;
  372. hand_l_target = QVector3D(-0.20F, HP::SHOULDER_Y - 0.02F, 0.15F + 0.02F * t);
  373. // Rotate shoulders back, coiling for strike
  374. torso_twist = -0.05F * ease_t;
  375. shoulder_rotation = 0.03F * ease_t;
  376. } else if (attack_phase < 0.28F) {
  377. // Phase 2: Apex - weapon at highest point, brief tension
  378. float t = (attack_phase - 0.15F) / 0.13F;
  379. float ease_t = smoothstep(t);
  380. hand_r_target = chamber_pos * (1.0F - ease_t) + apex_pos * ease_t;
  381. hand_l_target = QVector3D(-0.20F, HP::SHOULDER_Y - 0.04F, 0.17F);
  382. torso_twist = -0.05F;
  383. shoulder_rotation = 0.03F + 0.02F * ease_t;
  384. weight_shift = -0.02F * ease_t; // Weight shifts back before strike
  385. } else if (attack_phase < 0.48F) {
  386. // Phase 3: Strike - explosive diagonal slash downward
  387. float t = (attack_phase - 0.28F) / 0.20F;
  388. float power_t = t * t * t; // Fast acceleration
  389. hand_r_target = apex_pos * (1.0F - power_t) + strike_pos * power_t;
  390. hand_l_target =
  391. QVector3D(-0.20F + 0.08F * power_t,
  392. HP::SHOULDER_Y - 0.04F - 0.06F * power_t, 0.17F + 0.22F * power_t);
  393. // Explosive uncoil - torso rotates into strike, body leans forward
  394. torso_twist = -0.05F + 0.14F * power_t;
  395. forward_lean = 0.10F * power_t;
  396. shoulder_rotation = 0.05F - 0.08F * power_t;
  397. weight_shift = -0.02F + 0.08F * power_t;
  398. } else if (attack_phase < 0.62F) {
  399. // Phase 4: Follow-through - weapon continues past target
  400. float t = (attack_phase - 0.48F) / 0.14F;
  401. float ease_t = smoothstep(t);
  402. hand_r_target = strike_pos * (1.0F - ease_t) + followthrough_pos * ease_t;
  403. hand_l_target = QVector3D(-0.12F, HP::SHOULDER_Y - 0.10F, 0.39F);
  404. torso_twist = 0.09F - 0.03F * t;
  405. forward_lean = 0.10F - 0.02F * t;
  406. weight_shift = 0.06F;
  407. } else {
  408. // Phase 5: Recovery - return to guard
  409. float t = (attack_phase - 0.62F) / 0.38F;
  410. float ease_t = ease_out(t);
  411. hand_r_target =
  412. followthrough_pos * (1.0F - ease_t) + recover_pos * 0.5F * ease_t +
  413. rest_pos * 0.5F * ease_t;
  414. hand_l_target =
  415. QVector3D(-0.12F - 0.08F * ease_t, HP::SHOULDER_Y - 0.10F * (1.0F - ease_t),
  416. 0.39F * (1.0F - ease_t) + 0.15F * ease_t);
  417. torso_twist = 0.06F * (1.0F - ease_t);
  418. forward_lean = 0.08F * (1.0F - ease_t);
  419. weight_shift = 0.06F * (1.0F - ease_t);
  420. }
  421. // Apply body dynamics
  422. if (std::abs(torso_twist) > 0.001F) {
  423. m_pose.shoulder_r.setZ(m_pose.shoulder_r.z() + torso_twist);
  424. m_pose.shoulder_l.setZ(m_pose.shoulder_l.z() - torso_twist * 0.6F);
  425. }
  426. if (std::abs(shoulder_rotation) > 0.001F) {
  427. m_pose.shoulder_r.setY(m_pose.shoulder_r.y() - shoulder_rotation);
  428. m_pose.shoulder_l.setY(m_pose.shoulder_l.y() + shoulder_rotation * 0.4F);
  429. }
  430. if (forward_lean > 0.001F) {
  431. m_pose.shoulder_l.setZ(m_pose.shoulder_l.z() + forward_lean);
  432. m_pose.shoulder_r.setZ(m_pose.shoulder_r.z() + forward_lean);
  433. m_pose.neck_base.setZ(m_pose.neck_base.z() + forward_lean * 0.7F);
  434. m_pose.head_pos.setZ(m_pose.head_pos.z() + forward_lean * 0.5F);
  435. m_pose.pelvis_pos.setZ(m_pose.pelvis_pos.z() + forward_lean * 0.3F);
  436. }
  437. if (std::abs(weight_shift) > 0.001F) {
  438. m_pose.foot_r.setZ(m_pose.foot_r.z() + weight_shift);
  439. m_pose.knee_r.setZ(m_pose.knee_r.z() + weight_shift * 0.6F);
  440. }
  441. placeHandAt(false, hand_r_target);
  442. placeHandAt(true, hand_l_target);
  443. }
  444. void HumanoidPoseController::mount_on_horse(float saddle_height) {
  445. float const offset_y = saddle_height - m_pose.pelvis_pos.y();
  446. m_pose.pelvis_pos.setY(saddle_height);
  447. }
  448. void HumanoidPoseController::hold_sword_and_shield() {
  449. using HP = HumanProportions;
  450. QVector3D const sword_hand_pos(0.30F, HP::SHOULDER_Y - 0.02F, 0.35F);
  451. QVector3D const shield_hand_pos(-0.22F, HP::SHOULDER_Y, 0.18F);
  452. placeHandAt(false, sword_hand_pos);
  453. placeHandAt(true, shield_hand_pos);
  454. }
  455. void HumanoidPoseController::look_at(const QVector3D &target) {
  456. QVector3D const head_to_target = target - m_pose.head_pos;
  457. if (head_to_target.lengthSquared() < 1e-6F) {
  458. return;
  459. }
  460. QVector3D const direction = head_to_target.normalized();
  461. float const max_head_turn = 0.03F;
  462. QVector3D const head_offset = direction * max_head_turn;
  463. m_pose.head_pos += QVector3D(head_offset.x(), 0.0F, head_offset.z());
  464. float const neck_follow = 0.5F;
  465. m_pose.neck_base += QVector3D(head_offset.x() * neck_follow, 0.0F,
  466. head_offset.z() * neck_follow);
  467. }
  468. void HumanoidPoseController::hit_flinch(float intensity) {
  469. intensity = std::clamp(intensity, 0.0F, 1.0F);
  470. if (intensity < 0.01F) {
  471. return;
  472. }
  473. float const flinch_back = intensity * 0.06F;
  474. float const flinch_down = intensity * 0.04F;
  475. float const shoulder_drop = intensity * 0.03F;
  476. m_pose.head_pos.setZ(m_pose.head_pos.z() - flinch_back);
  477. m_pose.head_pos.setY(m_pose.head_pos.y() - flinch_down * 0.5F);
  478. m_pose.neck_base.setZ(m_pose.neck_base.z() - flinch_back * 0.8F);
  479. m_pose.shoulder_l.setY(m_pose.shoulder_l.y() - shoulder_drop);
  480. m_pose.shoulder_r.setY(m_pose.shoulder_r.y() - shoulder_drop);
  481. m_pose.shoulder_l.setZ(m_pose.shoulder_l.z() - flinch_back * 0.6F);
  482. m_pose.shoulder_r.setZ(m_pose.shoulder_r.z() - flinch_back * 0.6F);
  483. m_pose.pelvis_pos.setY(m_pose.pelvis_pos.y() - flinch_down * 0.3F);
  484. }
  485. void HumanoidPoseController::sword_slash_variant(float attack_phase,
  486. std::uint8_t variant) {
  487. using HP = HumanProportions;
  488. attack_phase = std::clamp(attack_phase, 0.0F, 1.0F);
  489. // Base positions - will be modified by variant
  490. QVector3D rest_pos(0.20F, HP::SHOULDER_Y + 0.05F, 0.15F);
  491. QVector3D chamber_pos(0.28F, HP::SHOULDER_Y + 0.20F, 0.02F);
  492. QVector3D apex_pos(0.30F, HP::SHOULDER_Y + 0.25F, 0.08F);
  493. QVector3D strike_pos(0.18F, HP::SHOULDER_Y - 0.15F, 0.62F);
  494. QVector3D followthrough_pos(0.05F, HP::WAIST_Y + 0.10F, 0.50F);
  495. // Variant-specific attack patterns
  496. float strike_direction = 1.0F; // 1.0 = right-to-left, -1.0 = left-to-right
  497. switch (variant % 3) {
  498. case 1:
  499. // Left-to-right diagonal slash
  500. chamber_pos = QVector3D(-0.10F, HP::SHOULDER_Y + 0.22F, 0.04F);
  501. apex_pos = QVector3D(-0.08F, HP::SHOULDER_Y + 0.28F, 0.10F);
  502. strike_pos = QVector3D(0.32F, HP::SHOULDER_Y - 0.12F, 0.58F);
  503. followthrough_pos = QVector3D(0.40F, HP::WAIST_Y + 0.08F, 0.48F);
  504. strike_direction = -1.0F;
  505. break;
  506. case 2:
  507. // Horizontal slash from right
  508. chamber_pos = QVector3D(0.35F, HP::SHOULDER_Y + 0.10F, 0.0F);
  509. apex_pos = QVector3D(0.38F, HP::SHOULDER_Y + 0.08F, 0.06F);
  510. strike_pos = QVector3D(0.05F, HP::SHOULDER_Y - 0.05F, 0.65F);
  511. followthrough_pos = QVector3D(-0.10F, HP::SHOULDER_Y - 0.10F, 0.55F);
  512. break;
  513. default:
  514. break;
  515. }
  516. QVector3D hand_r_target;
  517. QVector3D hand_l_target;
  518. // Body dynamics
  519. float torso_twist = 0.0F;
  520. float forward_lean = 0.0F;
  521. float shoulder_rotation = 0.0F;
  522. float weight_shift = 0.0F;
  523. auto smoothstep = [](float t) { return t * t * (3.0F - 2.0F * t); };
  524. auto ease_out = [](float t) { return 1.0F - (1.0F - t) * (1.0F - t); };
  525. if (attack_phase < 0.15F) {
  526. float t = attack_phase / 0.15F;
  527. float ease_t = t * t;
  528. hand_r_target = rest_pos * (1.0F - ease_t) + chamber_pos * ease_t;
  529. hand_l_target = QVector3D(-0.20F, HP::SHOULDER_Y - 0.02F, 0.15F);
  530. torso_twist = -0.05F * strike_direction * ease_t;
  531. shoulder_rotation = 0.03F * ease_t;
  532. } else if (attack_phase < 0.28F) {
  533. float t = (attack_phase - 0.15F) / 0.13F;
  534. float ease_t = smoothstep(t);
  535. hand_r_target = chamber_pos * (1.0F - ease_t) + apex_pos * ease_t;
  536. hand_l_target = QVector3D(-0.20F, HP::SHOULDER_Y - 0.04F, 0.17F);
  537. torso_twist = -0.05F * strike_direction;
  538. shoulder_rotation = 0.03F + 0.02F * ease_t;
  539. weight_shift = -0.02F * ease_t;
  540. } else if (attack_phase < 0.48F) {
  541. float t = (attack_phase - 0.28F) / 0.20F;
  542. float power_t = t * t * t;
  543. hand_r_target = apex_pos * (1.0F - power_t) + strike_pos * power_t;
  544. hand_l_target =
  545. QVector3D(-0.20F + 0.08F * power_t,
  546. HP::SHOULDER_Y - 0.04F - 0.06F * power_t, 0.17F + 0.22F * power_t);
  547. torso_twist = -0.05F * strike_direction + 0.14F * strike_direction * power_t;
  548. forward_lean = 0.10F * power_t;
  549. shoulder_rotation = 0.05F - 0.08F * power_t;
  550. weight_shift = -0.02F + 0.08F * power_t;
  551. } else if (attack_phase < 0.62F) {
  552. float t = (attack_phase - 0.48F) / 0.14F;
  553. float ease_t = smoothstep(t);
  554. hand_r_target = strike_pos * (1.0F - ease_t) + followthrough_pos * ease_t;
  555. hand_l_target = QVector3D(-0.12F, HP::SHOULDER_Y - 0.10F, 0.39F);
  556. torso_twist = 0.09F * strike_direction - 0.03F * strike_direction * t;
  557. forward_lean = 0.10F - 0.02F * t;
  558. weight_shift = 0.06F;
  559. } else {
  560. float t = (attack_phase - 0.62F) / 0.38F;
  561. float ease_t = ease_out(t);
  562. hand_r_target = followthrough_pos * (1.0F - ease_t) + rest_pos * ease_t;
  563. hand_l_target =
  564. QVector3D(-0.12F - 0.08F * ease_t, HP::SHOULDER_Y - 0.10F * (1.0F - ease_t),
  565. 0.39F * (1.0F - ease_t) + 0.15F * ease_t);
  566. torso_twist = 0.06F * strike_direction * (1.0F - ease_t);
  567. forward_lean = 0.08F * (1.0F - ease_t);
  568. weight_shift = 0.06F * (1.0F - ease_t);
  569. }
  570. // Apply body dynamics
  571. if (std::abs(torso_twist) > 0.001F) {
  572. m_pose.shoulder_r.setZ(m_pose.shoulder_r.z() + torso_twist);
  573. m_pose.shoulder_l.setZ(m_pose.shoulder_l.z() - torso_twist * 0.6F);
  574. }
  575. if (std::abs(shoulder_rotation) > 0.001F) {
  576. m_pose.shoulder_r.setY(m_pose.shoulder_r.y() - shoulder_rotation);
  577. m_pose.shoulder_l.setY(m_pose.shoulder_l.y() + shoulder_rotation * 0.4F);
  578. }
  579. if (forward_lean > 0.001F) {
  580. m_pose.shoulder_l.setZ(m_pose.shoulder_l.z() + forward_lean);
  581. m_pose.shoulder_r.setZ(m_pose.shoulder_r.z() + forward_lean);
  582. m_pose.neck_base.setZ(m_pose.neck_base.z() + forward_lean * 0.7F);
  583. m_pose.head_pos.setZ(m_pose.head_pos.z() + forward_lean * 0.5F);
  584. }
  585. if (std::abs(weight_shift) > 0.001F) {
  586. m_pose.foot_r.setZ(m_pose.foot_r.z() + weight_shift);
  587. m_pose.knee_r.setZ(m_pose.knee_r.z() + weight_shift * 0.6F);
  588. }
  589. placeHandAt(false, hand_r_target);
  590. placeHandAt(true, hand_l_target);
  591. }
  592. void HumanoidPoseController::spear_thrust_variant(float attack_phase,
  593. std::uint8_t variant) {
  594. using HP = HumanProportions;
  595. attack_phase = std::clamp(attack_phase, 0.0F, 1.0F);
  596. QVector3D guard_pos(0.28F, HP::SHOULDER_Y + 0.05F, 0.25F);
  597. QVector3D prepare_pos(0.35F, HP::SHOULDER_Y + 0.08F, 0.05F);
  598. QVector3D thrust_pos(0.32F, HP::SHOULDER_Y + 0.10F, 0.90F);
  599. QVector3D recover_pos(0.28F, HP::SHOULDER_Y + 0.06F, 0.40F);
  600. switch (variant % 3) {
  601. case 1:
  602. prepare_pos = QVector3D(0.30F, HP::SHOULDER_Y + 0.15F, 0.0F);
  603. thrust_pos = QVector3D(0.28F, HP::WAIST_Y + 0.20F, 0.95F);
  604. break;
  605. case 2:
  606. prepare_pos = QVector3D(0.40F, HP::SHOULDER_Y, 0.10F);
  607. thrust_pos = QVector3D(0.35F, HP::SHOULDER_Y + 0.05F, 0.85F);
  608. break;
  609. default:
  610. break;
  611. }
  612. QVector3D hand_r_target;
  613. QVector3D hand_l_target;
  614. auto easeInOutCubic = [](float t) {
  615. return t < 0.5F ? 4.0F * t * t * t
  616. : 1.0F - std::pow(-2.0F * t + 2.0F, 3.0F) / 2.0F;
  617. };
  618. auto smoothstep = [](float edge0, float edge1, float x) {
  619. float t = std::clamp((x - edge0) / (edge1 - edge0), 0.0F, 1.0F);
  620. return t * t * (3.0F - 2.0F * t);
  621. };
  622. auto lerp = [](float a, float b, float t) { return a * (1.0F - t) + b * t; };
  623. if (attack_phase < 0.20F) {
  624. float const t = easeInOutCubic(attack_phase / 0.20F);
  625. hand_r_target = guard_pos * (1.0F - t) + prepare_pos * t;
  626. hand_l_target = QVector3D(-0.10F, HP::SHOULDER_Y - 0.05F,
  627. 0.20F * (1.0F - t) + 0.08F * t);
  628. } else if (attack_phase < 0.30F) {
  629. hand_r_target = prepare_pos;
  630. hand_l_target = QVector3D(-0.10F, HP::SHOULDER_Y - 0.05F, 0.08F);
  631. } else if (attack_phase < 0.50F) {
  632. float t = (attack_phase - 0.30F) / 0.20F;
  633. t = t * t * t;
  634. hand_r_target = prepare_pos * (1.0F - t) + thrust_pos * t;
  635. hand_l_target =
  636. QVector3D(-0.10F + 0.05F * t, HP::SHOULDER_Y - 0.05F + 0.03F * t,
  637. 0.08F + 0.45F * t);
  638. } else if (attack_phase < 0.70F) {
  639. float const t = easeInOutCubic((attack_phase - 0.50F) / 0.20F);
  640. hand_r_target = thrust_pos * (1.0F - t) + recover_pos * t;
  641. hand_l_target = QVector3D(-0.05F * (1.0F - t) - 0.10F * t,
  642. HP::SHOULDER_Y - 0.02F * (1.0F - t) - 0.06F * t,
  643. lerp(0.53F, 0.35F, t));
  644. } else {
  645. float const t = smoothstep(0.70F, 1.0F, attack_phase);
  646. hand_r_target = recover_pos * (1.0F - t) + guard_pos * t;
  647. hand_l_target =
  648. QVector3D(-0.10F - 0.02F * (1.0F - t),
  649. HP::SHOULDER_Y - 0.06F + 0.01F * t, lerp(0.35F, 0.25F, t));
  650. }
  651. placeHandAt(false, hand_r_target);
  652. placeHandAt(true, hand_l_target);
  653. }
  654. void HumanoidPoseController::tilt_torso(float side_tilt, float forward_tilt) {
  655. QVector3D const right = m_anim_ctx.heading_right();
  656. QVector3D const forward = m_anim_ctx.heading_forward();
  657. QVector3D const offset = right * side_tilt + forward * forward_tilt;
  658. m_pose.shoulder_l += offset;
  659. m_pose.shoulder_r += offset;
  660. m_pose.neck_base += offset * 1.2F;
  661. m_pose.head_pos += offset * 1.5F;
  662. m_pose.body_frames.torso.origin += offset;
  663. m_pose.body_frames.head.origin += offset * 1.5F;
  664. }
  665. } // namespace Render::GL