pose_controller.cpp 51 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474
  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::stand_idle() {}
  13. void HumanoidPoseController::apply_micro_idle(float, std::uint32_t) {}
  14. namespace {
  15. constexpr float k_min_idle_duration = 5.0F;
  16. constexpr float k_ambient_duration = 6.0F;
  17. constexpr float k_seed_offset_divisor = 50.0F;
  18. constexpr float k_base_cycle_period = 25.0F;
  19. constexpr float k_cycle_period_range = 15.0F;
  20. constexpr float k_tap_frequency_multiplier = 6.0F;
  21. } // namespace
  22. auto HumanoidPoseController::get_ambient_idle_type(
  23. float time, std::uint32_t seed, float idle_duration) -> AmbientIdleType {
  24. if (idle_duration < k_min_idle_duration) {
  25. return AmbientIdleType::None;
  26. }
  27. float const seed_offset =
  28. static_cast<float>(seed % 1000) / k_seed_offset_divisor;
  29. float const cycle_period =
  30. k_base_cycle_period +
  31. static_cast<float>(seed % 1500) / (1500.0F / k_cycle_period_range);
  32. float const cycle_time = std::fmod(time + seed_offset, cycle_period);
  33. auto const cycle_number =
  34. static_cast<std::uint32_t>((time + seed_offset) / cycle_period);
  35. std::uint32_t const soldier_selector = seed ^ (cycle_number * 2654435761U);
  36. if ((soldier_selector % 10) > 1) {
  37. return AmbientIdleType::None;
  38. }
  39. if (cycle_time > k_ambient_duration) {
  40. return AmbientIdleType::None;
  41. }
  42. std::uint32_t const anim_selector = seed ^ (cycle_number * 1664525U);
  43. auto const idle_type = static_cast<std::uint8_t>(anim_selector % 8);
  44. return static_cast<AmbientIdleType>(idle_type + 1);
  45. }
  46. void HumanoidPoseController::apply_ambient_idle(float time, std::uint32_t seed,
  47. float idle_duration) {
  48. AmbientIdleType const idle_type =
  49. get_ambient_idle_type(time, seed, idle_duration);
  50. if (idle_type == AmbientIdleType::None) {
  51. return;
  52. }
  53. float const seed_offset =
  54. static_cast<float>(seed % 1000) / k_seed_offset_divisor;
  55. float const cycle_period =
  56. k_base_cycle_period +
  57. static_cast<float>(seed % 1500) / (1500.0F / k_cycle_period_range);
  58. float const cycle_time = std::fmod(time + seed_offset, cycle_period);
  59. float phase = cycle_time / k_ambient_duration;
  60. apply_ambient_idle_explicit(idle_type, phase);
  61. }
  62. void HumanoidPoseController::apply_ambient_idle_explicit(
  63. AmbientIdleType idle_type, float phase) {
  64. using HP = HumanProportions;
  65. if (idle_type == AmbientIdleType::None) {
  66. return;
  67. }
  68. phase = std::clamp(phase, 0.0F, 1.0F);
  69. float const intensity =
  70. (phase < 0.5F) ? (2.0F * phase * phase)
  71. : (1.0F - std::pow(-2.0F * phase + 2.0F, 2.0F) / 2.0F);
  72. switch (idle_type) {
  73. case AmbientIdleType::SitDown: {
  74. float sit_intensity = 0.0F;
  75. if (phase < 0.4F) {
  76. sit_intensity = phase / 0.4F;
  77. } else if (phase < 0.6F) {
  78. sit_intensity = 1.0F;
  79. } else {
  80. sit_intensity = 1.0F - (phase - 0.6F) / 0.4F;
  81. }
  82. sit_intensity =
  83. sit_intensity * sit_intensity * (3.0F - 2.0F * sit_intensity);
  84. float const sit_drop = sit_intensity * 0.35F;
  85. m_pose.pelvis_pos.setY(m_pose.pelvis_pos.y() - sit_drop);
  86. m_pose.shoulder_l.setY(m_pose.shoulder_l.y() - sit_drop);
  87. m_pose.shoulder_r.setY(m_pose.shoulder_r.y() - sit_drop);
  88. m_pose.neck_base.setY(m_pose.neck_base.y() - sit_drop);
  89. m_pose.head_pos.setY(m_pose.head_pos.y() - sit_drop);
  90. m_pose.knee_l.setY(m_pose.knee_l.y() - sit_drop * 0.5F);
  91. m_pose.knee_r.setY(m_pose.knee_r.y() - sit_drop * 0.5F);
  92. m_pose.knee_l.setZ(m_pose.knee_l.z() + sit_intensity * 0.12F);
  93. m_pose.knee_r.setZ(m_pose.knee_r.z() + sit_intensity * 0.12F);
  94. m_pose.foot_l.setX(m_pose.foot_l.x() - sit_intensity * 0.04F);
  95. m_pose.foot_r.setX(m_pose.foot_r.x() + sit_intensity * 0.04F);
  96. break;
  97. }
  98. case AmbientIdleType::ShuffleFeet: {
  99. float const shuffle_phase = phase * 2.0F * std::numbers::pi_v<float>;
  100. float const shuffle_amount = std::sin(shuffle_phase) * intensity * 0.04F;
  101. m_pose.foot_l.setZ(m_pose.foot_l.z() + shuffle_amount);
  102. m_pose.foot_r.setZ(m_pose.foot_r.z() - shuffle_amount);
  103. m_pose.knee_l.setZ(m_pose.knee_l.z() + shuffle_amount * 0.5F);
  104. m_pose.knee_r.setZ(m_pose.knee_r.z() - shuffle_amount * 0.5F);
  105. break;
  106. }
  107. case AmbientIdleType::TapFoot: {
  108. float const tap_phase = std::fmod(phase * k_tap_frequency_multiplier, 1.0F);
  109. float const tap_lift =
  110. (tap_phase < 0.3F)
  111. ? std::sin(tap_phase / 0.3F * std::numbers::pi_v<float>)
  112. : 0.0F;
  113. float const tap_amount = tap_lift * intensity * 0.03F;
  114. m_pose.foot_r.setY(m_pose.foot_r.y() + tap_amount);
  115. m_pose.knee_r.setY(m_pose.knee_r.y() + tap_amount * 0.3F);
  116. break;
  117. }
  118. case AmbientIdleType::ShiftWeight: {
  119. float const shift_phase = phase * std::numbers::pi_v<float>;
  120. float const shift_amount = std::sin(shift_phase) * intensity * 0.04F;
  121. m_pose.pelvis_pos.setX(m_pose.pelvis_pos.x() + shift_amount);
  122. m_pose.shoulder_l.setX(m_pose.shoulder_l.x() + shift_amount);
  123. m_pose.shoulder_r.setX(m_pose.shoulder_r.x() + shift_amount);
  124. m_pose.neck_base.setX(m_pose.neck_base.x() + shift_amount);
  125. m_pose.head_pos.setX(m_pose.head_pos.x() + shift_amount);
  126. m_pose.knee_l.setY(m_pose.knee_l.y() - shift_amount * 0.3F);
  127. m_pose.knee_r.setY(m_pose.knee_r.y() + shift_amount * 0.2F);
  128. break;
  129. }
  130. case AmbientIdleType::StepInPlace: {
  131. float step_phase = phase * 2.0F;
  132. bool const is_left_step = step_phase < 1.0F;
  133. if (!is_left_step) {
  134. step_phase -= 1.0F;
  135. }
  136. float const step_lift =
  137. std::sin(step_phase * std::numbers::pi_v<float>) * intensity * 0.05F;
  138. if (is_left_step) {
  139. m_pose.foot_l.setY(m_pose.foot_l.y() + step_lift);
  140. m_pose.knee_l.setY(m_pose.knee_l.y() + step_lift * 0.6F);
  141. } else {
  142. m_pose.foot_r.setY(m_pose.foot_r.y() + step_lift);
  143. m_pose.knee_r.setY(m_pose.knee_r.y() + step_lift * 0.6F);
  144. }
  145. break;
  146. }
  147. case AmbientIdleType::BendKnee: {
  148. float const bend_amount = intensity * 0.06F;
  149. m_pose.knee_l.setY(m_pose.knee_l.y() - bend_amount);
  150. m_pose.knee_l.setZ(m_pose.knee_l.z() + bend_amount * 0.4F);
  151. m_pose.foot_l.setY(m_pose.foot_l.y() + bend_amount * 0.2F);
  152. float const shift = bend_amount * 0.25F;
  153. m_pose.pelvis_pos.setX(m_pose.pelvis_pos.x() + shift);
  154. m_pose.shoulder_l.setX(m_pose.shoulder_l.x() + shift);
  155. m_pose.shoulder_r.setX(m_pose.shoulder_r.x() + shift);
  156. m_pose.neck_base.setX(m_pose.neck_base.x() + shift);
  157. m_pose.head_pos.setX(m_pose.head_pos.x() + shift);
  158. break;
  159. }
  160. case AmbientIdleType::RaiseWeapon: {
  161. float raise_intensity = 0.0F;
  162. if (phase < 0.3F) {
  163. raise_intensity = phase / 0.3F;
  164. } else if (phase < 0.7F) {
  165. raise_intensity = 1.0F;
  166. } else {
  167. raise_intensity = 1.0F - (phase - 0.7F) / 0.3F;
  168. }
  169. raise_intensity =
  170. raise_intensity * raise_intensity * (3.0F - 2.0F * raise_intensity);
  171. float const raise_amount = raise_intensity * 0.15F;
  172. m_pose.hand_l.setY(m_pose.hand_l.y() + raise_amount);
  173. m_pose.hand_r.setY(m_pose.hand_r.y() + raise_amount);
  174. m_pose.elbow_l.setY(m_pose.elbow_l.y() + raise_amount * 0.6F);
  175. m_pose.elbow_r.setY(m_pose.elbow_r.y() + raise_amount * 0.6F);
  176. m_pose.head_pos.setZ(m_pose.head_pos.z() - raise_intensity * 0.02F);
  177. break;
  178. }
  179. case AmbientIdleType::Jump: {
  180. float jump_height = 0.0F;
  181. float crouch_amount = 0.0F;
  182. if (phase < 0.15F) {
  183. crouch_amount = phase / 0.15F;
  184. } else if (phase < 0.4F) {
  185. float const launch_phase = (phase - 0.15F) / 0.25F;
  186. jump_height = std::sin(launch_phase * std::numbers::pi_v<float> * 0.5F);
  187. } else if (phase < 0.6F) {
  188. jump_height = 1.0F;
  189. } else if (phase < 0.85F) {
  190. float const land_phase = (phase - 0.6F) / 0.25F;
  191. jump_height =
  192. 1.0F - std::sin(land_phase * std::numbers::pi_v<float> * 0.5F);
  193. } else {
  194. crouch_amount = 1.0F - (phase - 0.85F) / 0.15F;
  195. }
  196. crouch_amount =
  197. crouch_amount * crouch_amount * (3.0F - 2.0F * crouch_amount);
  198. float const max_jump = 0.12F;
  199. float const max_crouch = 0.06F;
  200. float const vertical = jump_height * max_jump - crouch_amount * max_crouch;
  201. m_pose.pelvis_pos.setY(m_pose.pelvis_pos.y() + vertical);
  202. m_pose.shoulder_l.setY(m_pose.shoulder_l.y() + vertical);
  203. m_pose.shoulder_r.setY(m_pose.shoulder_r.y() + vertical);
  204. m_pose.neck_base.setY(m_pose.neck_base.y() + vertical);
  205. m_pose.head_pos.setY(m_pose.head_pos.y() + vertical);
  206. m_pose.foot_l.setY(m_pose.foot_l.y() + vertical);
  207. m_pose.foot_r.setY(m_pose.foot_r.y() + vertical);
  208. m_pose.knee_l.setY(m_pose.knee_l.y() + vertical);
  209. m_pose.knee_r.setY(m_pose.knee_r.y() + vertical);
  210. if (crouch_amount > 0.0F) {
  211. m_pose.knee_l.setZ(m_pose.knee_l.z() + crouch_amount * 0.08F);
  212. m_pose.knee_r.setZ(m_pose.knee_r.z() + crouch_amount * 0.08F);
  213. }
  214. break;
  215. }
  216. case AmbientIdleType::None:
  217. default:
  218. break;
  219. }
  220. }
  221. void HumanoidPoseController::kneel(float depth) {
  222. using HP = HumanProportions;
  223. depth = std::clamp(depth, 0.0F, 1.0F);
  224. if (depth < 1e-6F) {
  225. return;
  226. }
  227. float const eased_depth = depth * depth * (3.0F - 2.0F * depth);
  228. float const kneel_offset = eased_depth * 0.40F;
  229. float const pelvis_y = HP::WAIST_Y - kneel_offset;
  230. m_pose.pelvis_pos.setY(pelvis_y);
  231. float const stance_narrow = 0.11F;
  232. float const left_knee_y = HP::GROUND_Y + 0.07F * eased_depth;
  233. float const left_knee_z = -0.06F * eased_depth;
  234. m_pose.knee_l = QVector3D(-stance_narrow, left_knee_y, left_knee_z);
  235. m_pose.foot_l =
  236. QVector3D(-stance_narrow - 0.025F, HP::GROUND_Y,
  237. left_knee_z - HP::LOWER_LEG_LEN * 0.93F * eased_depth);
  238. float const right_knee_y = pelvis_y - 0.12F;
  239. float const right_foot_z = 0.28F * eased_depth;
  240. m_pose.knee_r = QVector3D(stance_narrow, right_knee_y, right_foot_z - 0.05F);
  241. m_pose.foot_r = QVector3D(stance_narrow, HP::GROUND_Y + m_pose.foot_y_offset,
  242. right_foot_z);
  243. float const upper_body_drop = kneel_offset;
  244. float const forward_lean = 0.03F * eased_depth;
  245. m_pose.shoulder_l.setY(m_pose.shoulder_l.y() - upper_body_drop);
  246. m_pose.shoulder_r.setY(m_pose.shoulder_r.y() - upper_body_drop);
  247. m_pose.neck_base.setY(m_pose.neck_base.y() - upper_body_drop);
  248. m_pose.head_pos.setY(m_pose.head_pos.y() - upper_body_drop);
  249. m_pose.shoulder_l.setZ(m_pose.shoulder_l.z() + forward_lean);
  250. m_pose.shoulder_r.setZ(m_pose.shoulder_r.z() + forward_lean);
  251. m_pose.neck_base.setZ(m_pose.neck_base.z() + forward_lean * 0.8F);
  252. m_pose.head_pos.setZ(m_pose.head_pos.z() + forward_lean * 0.6F);
  253. }
  254. void HumanoidPoseController::kneel_transition(float progress, bool standing_up) {
  255. using HP = HumanProportions;
  256. progress = std::clamp(progress, 0.0F, 1.0F);
  257. auto ease_in_out = [](float t) { return t * t * (3.0F - 2.0F * t); };
  258. float kneel_amount = standing_up ? (1.0F - progress) : progress;
  259. float eased_progress = ease_in_out(progress);
  260. kneel(kneel_amount);
  261. if (standing_up) {
  262. if (progress < 0.35F) {
  263. float t = progress / 0.35F;
  264. float push_t = ease_in_out(t);
  265. m_pose.foot_r.setZ(m_pose.foot_r.z() - 0.08F * push_t);
  266. m_pose.knee_r.setZ(m_pose.knee_r.z() - 0.05F * push_t);
  267. float momentum_lean = 0.06F * push_t;
  268. m_pose.shoulder_l.setZ(m_pose.shoulder_l.z() + momentum_lean);
  269. m_pose.shoulder_r.setZ(m_pose.shoulder_r.z() + momentum_lean);
  270. m_pose.neck_base.setZ(m_pose.neck_base.z() + momentum_lean * 0.9F);
  271. m_pose.head_pos.setZ(m_pose.head_pos.z() + momentum_lean * 0.7F);
  272. m_pose.hand_l.setZ(m_pose.hand_l.z() + 0.04F * push_t);
  273. m_pose.hand_r.setZ(m_pose.hand_r.z() + 0.04F * push_t);
  274. }
  275. else if (progress < 0.70F) {
  276. float t = (progress - 0.35F) / 0.35F;
  277. float rise_t = ease_in_out(t);
  278. float lift_boost = 0.02F * std::sin(rise_t * std::numbers::pi_v<float>);
  279. m_pose.pelvis_pos.setY(m_pose.pelvis_pos.y() + lift_boost);
  280. m_pose.shoulder_l.setY(m_pose.shoulder_l.y() + lift_boost);
  281. m_pose.shoulder_r.setY(m_pose.shoulder_r.y() + lift_boost);
  282. m_pose.foot_l.setZ(m_pose.foot_l.z() + 0.15F * rise_t);
  283. m_pose.knee_l.setZ(m_pose.knee_l.z() + 0.10F * rise_t);
  284. m_pose.knee_l.setY(m_pose.knee_l.y() + 0.20F * rise_t);
  285. }
  286. else {
  287. float t = (progress - 0.70F) / 0.30F;
  288. float settle_t = ease_in_out(t);
  289. float correct_lean = -0.04F * settle_t * (1.0F - kneel_amount);
  290. m_pose.shoulder_l.setZ(m_pose.shoulder_l.z() + correct_lean);
  291. m_pose.shoulder_r.setZ(m_pose.shoulder_r.z() + correct_lean);
  292. }
  293. } else {
  294. if (progress < 0.30F) {
  295. float t = progress / 0.30F;
  296. float prep_t = ease_in_out(t);
  297. m_pose.pelvis_pos.setZ(m_pose.pelvis_pos.z() - 0.03F * prep_t);
  298. m_pose.hand_l.setY(m_pose.hand_l.y() - 0.02F * prep_t);
  299. m_pose.hand_r.setY(m_pose.hand_r.y() - 0.02F * prep_t);
  300. }
  301. else if (progress < 0.75F) {
  302. float t = (progress - 0.30F) / 0.45F;
  303. float controlled_lean = 0.04F * std::sin(t * std::numbers::pi_v<float>);
  304. m_pose.shoulder_l.setZ(m_pose.shoulder_l.z() + controlled_lean);
  305. m_pose.shoulder_r.setZ(m_pose.shoulder_r.z() + controlled_lean);
  306. }
  307. else {
  308. float t = (progress - 0.75F) / 0.25F;
  309. float settle_t = ease_in_out(t);
  310. m_pose.knee_l.setY(m_pose.knee_l.y() - 0.01F * settle_t);
  311. }
  312. }
  313. }
  314. void HumanoidPoseController::lean(const QVector3D &direction, float amount) {
  315. amount = std::clamp(amount, 0.0F, 1.0F);
  316. QVector3D dir = direction;
  317. if (dir.lengthSquared() > 1e-6F) {
  318. dir.normalize();
  319. } else {
  320. dir = QVector3D(0.0F, 0.0F, 1.0F);
  321. }
  322. float const lean_magnitude = 0.12F * amount;
  323. QVector3D const lean_offset = dir * lean_magnitude;
  324. m_pose.shoulder_l += lean_offset;
  325. m_pose.shoulder_r += lean_offset;
  326. m_pose.neck_base += lean_offset * 0.85F;
  327. m_pose.head_pos += lean_offset * 0.75F;
  328. }
  329. void HumanoidPoseController::place_hand_at(bool is_left,
  330. const QVector3D &target_position) {
  331. get_hand(is_left) = target_position;
  332. const QVector3D &shoulder = get_shoulder(is_left);
  333. const QVector3D outward_dir = compute_outward_dir(is_left);
  334. float const along_frac = is_left ? 0.45F : 0.48F;
  335. float const lateral_offset = is_left ? 0.15F : 0.12F;
  336. float const y_bias = is_left ? -0.08F : 0.02F;
  337. float const outward_sign = 1.0F;
  338. get_elbow(is_left) =
  339. solve_elbow_ik(is_left, shoulder, target_position, outward_dir,
  340. along_frac, lateral_offset, y_bias, outward_sign);
  341. }
  342. auto HumanoidPoseController::solve_elbow_ik(
  343. bool, const QVector3D &shoulder, const QVector3D &hand,
  344. const QVector3D &outward_dir, float along_frac, float lateral_offset,
  345. float y_bias, float outward_sign) const -> QVector3D {
  346. return elbow_bend_torso(shoulder, hand, outward_dir, along_frac,
  347. lateral_offset, y_bias, outward_sign);
  348. }
  349. auto HumanoidPoseController::solve_knee_ik(
  350. bool is_left, const QVector3D &hip, const QVector3D &foot,
  351. float height_scale) const -> QVector3D {
  352. using HP = HumanProportions;
  353. QVector3D hip_to_foot = foot - hip;
  354. float const distance = hip_to_foot.length();
  355. if (distance < 1e-5F) {
  356. return hip;
  357. }
  358. float const upper_len = HP::UPPER_LEG_LEN * height_scale;
  359. float const lower_len = HP::LOWER_LEG_LEN * height_scale;
  360. float const reach = upper_len + lower_len;
  361. float const min_reach =
  362. std::max(std::abs(upper_len - lower_len) + 1e-4F, 1e-3F);
  363. float const max_reach = std::max(reach - 1e-4F, min_reach + 1e-4F);
  364. float const clamped_dist = std::clamp(distance, min_reach, max_reach);
  365. QVector3D const dir = hip_to_foot / distance;
  366. float cos_theta = (upper_len * upper_len + clamped_dist * clamped_dist -
  367. lower_len * lower_len) /
  368. (2.0F * upper_len * clamped_dist);
  369. cos_theta = std::clamp(cos_theta, -1.0F, 1.0F);
  370. float const sin_theta =
  371. std::sqrt(std::max(0.0F, 1.0F - cos_theta * cos_theta));
  372. QVector3D bend_pref =
  373. is_left ? QVector3D(-0.24F, 0.0F, 0.95F) : QVector3D(0.24F, 0.0F, 0.95F);
  374. bend_pref.normalize();
  375. QVector3D bend_axis = bend_pref - dir * QVector3D::dotProduct(dir, bend_pref);
  376. if (bend_axis.lengthSquared() < 1e-6F) {
  377. bend_axis = QVector3D::crossProduct(dir, QVector3D(0.0F, 1.0F, 0.0F));
  378. if (bend_axis.lengthSquared() < 1e-6F) {
  379. bend_axis = QVector3D::crossProduct(dir, QVector3D(1.0F, 0.0F, 0.0F));
  380. }
  381. }
  382. bend_axis.normalize();
  383. QVector3D knee =
  384. hip + dir * (cos_theta * upper_len) + bend_axis * (sin_theta * upper_len);
  385. float const knee_floor = HP::GROUND_Y + m_pose.foot_y_offset * 0.5F;
  386. if (knee.y() < knee_floor) {
  387. knee.setY(knee_floor);
  388. }
  389. if (knee.y() > hip.y()) {
  390. knee.setY(hip.y());
  391. }
  392. return knee;
  393. }
  394. auto HumanoidPoseController::get_shoulder(bool is_left) const
  395. -> const QVector3D & {
  396. return is_left ? m_pose.shoulder_l : m_pose.shoulder_r;
  397. }
  398. auto HumanoidPoseController::get_hand(bool is_left) -> QVector3D & {
  399. return is_left ? m_pose.hand_l : m_pose.hand_r;
  400. }
  401. auto HumanoidPoseController::get_hand(bool is_left) const -> const QVector3D & {
  402. return is_left ? m_pose.hand_l : m_pose.hand_r;
  403. }
  404. auto HumanoidPoseController::get_elbow(bool is_left) -> QVector3D & {
  405. return is_left ? m_pose.elbow_l : m_pose.elbow_r;
  406. }
  407. auto HumanoidPoseController::compute_right_axis() const -> QVector3D {
  408. QVector3D right_axis = m_pose.shoulder_r - m_pose.shoulder_l;
  409. right_axis.setY(0.0F);
  410. if (right_axis.lengthSquared() < 1e-8F) {
  411. right_axis = QVector3D(1.0F, 0.0F, 0.0F);
  412. }
  413. right_axis.normalize();
  414. return right_axis;
  415. }
  416. auto HumanoidPoseController::compute_outward_dir(bool is_left) const
  417. -> QVector3D {
  418. QVector3D const right_axis = compute_right_axis();
  419. return is_left ? -right_axis : right_axis;
  420. }
  421. auto HumanoidPoseController::get_shoulder_y(bool is_left) const -> float {
  422. return is_left ? m_pose.shoulder_l.y() : m_pose.shoulder_r.y();
  423. }
  424. auto HumanoidPoseController::get_pelvis_y() const -> float {
  425. return m_pose.pelvis_pos.y();
  426. }
  427. void HumanoidPoseController::aim_bow(float draw_phase) {
  428. using HP = HumanProportions;
  429. draw_phase = std::clamp(draw_phase, 0.0F, 1.0F);
  430. QVector3D const aim_pos(-0.02F, HP::SHOULDER_Y + 0.18F, 0.42F);
  431. QVector3D const draw_pos(-0.05F, HP::SHOULDER_Y + 0.12F, 0.22F);
  432. QVector3D const release_pos(-0.02F, HP::SHOULDER_Y + 0.20F, 0.34F);
  433. QVector3D hand_l_target;
  434. float shoulder_twist = 0.0F;
  435. float head_recoil = 0.0F;
  436. if (draw_phase < 0.20F) {
  437. float t = draw_phase / 0.20F;
  438. t = t * t;
  439. hand_l_target = aim_pos * (1.0F - t) + draw_pos * t;
  440. shoulder_twist = t * 0.08F;
  441. } else if (draw_phase < 0.50F) {
  442. hand_l_target = draw_pos;
  443. shoulder_twist = 0.08F;
  444. } else if (draw_phase < 0.58F) {
  445. float t = (draw_phase - 0.50F) / 0.08F;
  446. t = t * t * t;
  447. hand_l_target = draw_pos * (1.0F - t) + release_pos * t;
  448. shoulder_twist = 0.08F * (1.0F - t * 0.6F);
  449. head_recoil = t * 0.04F;
  450. } else {
  451. float t = (draw_phase - 0.58F) / 0.42F;
  452. t = 1.0F - (1.0F - t) * (1.0F - t);
  453. hand_l_target = release_pos * (1.0F - t) + aim_pos * t;
  454. shoulder_twist = 0.08F * 0.4F * (1.0F - t);
  455. head_recoil = 0.04F * (1.0F - t);
  456. }
  457. QVector3D const hand_r_target(0.03F, HP::SHOULDER_Y + 0.08F, 0.55F);
  458. place_hand_at(false, hand_r_target);
  459. place_hand_at(true, hand_l_target);
  460. if (shoulder_twist > 0.01F) {
  461. m_pose.shoulder_l.setY(m_pose.shoulder_l.y() + shoulder_twist);
  462. m_pose.shoulder_r.setY(m_pose.shoulder_r.y() - shoulder_twist * 0.5F);
  463. }
  464. if (head_recoil > 0.01F) {
  465. m_pose.head_pos.setZ(m_pose.head_pos.z() - head_recoil);
  466. }
  467. }
  468. void HumanoidPoseController::melee_strike(float strike_phase) {
  469. using HP = HumanProportions;
  470. strike_phase = std::clamp(strike_phase, 0.0F, 1.0F);
  471. QVector3D const rest_pos(0.22F, HP::SHOULDER_Y + 0.02F, 0.18F);
  472. QVector3D const chamber_pos(0.30F, HP::SHOULDER_Y + 0.08F, 0.05F);
  473. QVector3D const strike_pos(0.28F, HP::SHOULDER_Y - 0.05F, 0.65F);
  474. QVector3D const followthrough_pos(0.10F, HP::SHOULDER_Y - 0.12F, 0.55F);
  475. QVector3D hand_r_target;
  476. QVector3D hand_l_target;
  477. float torso_twist = 0.0F;
  478. float forward_lean = 0.0F;
  479. float shoulder_dip = 0.0F;
  480. float step_forward = 0.0F;
  481. if (strike_phase < 0.20F) {
  482. float t = strike_phase / 0.20F;
  483. float ease_t = t * t;
  484. hand_r_target = rest_pos * (1.0F - ease_t) + chamber_pos * ease_t;
  485. hand_l_target =
  486. QVector3D(-0.18F, HP::SHOULDER_Y + 0.02F, 0.22F - 0.08F * t);
  487. torso_twist = -0.04F * ease_t;
  488. shoulder_dip = -0.02F * ease_t;
  489. } else if (strike_phase < 0.28F) {
  490. hand_r_target = chamber_pos;
  491. hand_l_target = QVector3D(-0.18F, HP::SHOULDER_Y + 0.02F, 0.14F);
  492. torso_twist = -0.04F;
  493. shoulder_dip = -0.02F;
  494. } else if (strike_phase < 0.48F) {
  495. float t = (strike_phase - 0.28F) / 0.20F;
  496. float power_t = t * t * (3.0F - 2.0F * t);
  497. hand_r_target = chamber_pos * (1.0F - power_t) + strike_pos * power_t;
  498. hand_l_target = QVector3D(-0.18F + 0.06F * power_t,
  499. HP::SHOULDER_Y + 0.02F - 0.08F * power_t,
  500. 0.14F + 0.20F * power_t);
  501. torso_twist = -0.04F + 0.10F * power_t;
  502. forward_lean = 0.08F * power_t;
  503. shoulder_dip = -0.02F + 0.05F * power_t;
  504. step_forward = 0.06F * power_t;
  505. } else if (strike_phase < 0.65F) {
  506. float t = (strike_phase - 0.48F) / 0.17F;
  507. float ease_t = t * t;
  508. hand_r_target = strike_pos * (1.0F - ease_t) + followthrough_pos * ease_t;
  509. hand_l_target = QVector3D(-0.12F, HP::SHOULDER_Y - 0.06F, 0.34F);
  510. torso_twist = 0.06F - 0.02F * t;
  511. forward_lean = 0.08F - 0.03F * t;
  512. shoulder_dip = 0.03F;
  513. step_forward = 0.06F;
  514. } else {
  515. float t = (strike_phase - 0.65F) / 0.35F;
  516. float ease_t = 1.0F - (1.0F - t) * (1.0F - t);
  517. hand_r_target = followthrough_pos * (1.0F - ease_t) + rest_pos * ease_t;
  518. hand_l_target =
  519. QVector3D(-0.12F + (-0.18F + 0.12F) * ease_t,
  520. HP::SHOULDER_Y - 0.06F * (1.0F - ease_t) + 0.02F * ease_t,
  521. 0.34F * (1.0F - ease_t) + 0.22F * ease_t);
  522. torso_twist = 0.04F * (1.0F - ease_t);
  523. forward_lean = 0.05F * (1.0F - ease_t);
  524. shoulder_dip = 0.03F * (1.0F - ease_t);
  525. step_forward = 0.06F * (1.0F - ease_t);
  526. }
  527. if (std::abs(torso_twist) > 0.001F) {
  528. float const twist = torso_twist * 0.0F;
  529. m_pose.shoulder_r.setZ(m_pose.shoulder_r.z() + twist);
  530. m_pose.shoulder_l.setZ(m_pose.shoulder_l.z() - twist * 0.5F);
  531. }
  532. if (forward_lean > 0.001F) {
  533. m_pose.shoulder_l.setZ(m_pose.shoulder_l.z() + forward_lean);
  534. m_pose.shoulder_r.setZ(m_pose.shoulder_r.z() + forward_lean);
  535. m_pose.neck_base.setZ(m_pose.neck_base.z() + forward_lean * 0.8F);
  536. m_pose.head_pos.setZ(m_pose.head_pos.z() + forward_lean * 0.6F);
  537. }
  538. if (std::abs(shoulder_dip) > 0.001F) {
  539. m_pose.shoulder_r.setY(m_pose.shoulder_r.y() + shoulder_dip);
  540. }
  541. if (step_forward > 0.001F) {
  542. m_pose.foot_r.setZ(m_pose.foot_r.z() + step_forward);
  543. m_pose.knee_r.setZ(m_pose.knee_r.z() + step_forward * 0.5F);
  544. }
  545. place_hand_at(false, hand_r_target);
  546. place_hand_at(true, hand_l_target);
  547. }
  548. void HumanoidPoseController::grasp_two_handed(const QVector3D &grip_center,
  549. float hand_separation) {
  550. hand_separation = std::clamp(hand_separation, 0.1F, 0.8F);
  551. QVector3D const right_axis = compute_right_axis();
  552. QVector3D const right_hand_pos =
  553. grip_center + right_axis * (hand_separation * 0.5F);
  554. QVector3D const left_hand_pos =
  555. grip_center - right_axis * (hand_separation * 0.5F);
  556. place_hand_at(false, right_hand_pos);
  557. place_hand_at(true, left_hand_pos);
  558. }
  559. void HumanoidPoseController::spear_thrust(float attack_phase) {
  560. using HP = HumanProportions;
  561. attack_phase = std::clamp(attack_phase, 0.0F, 1.0F);
  562. QVector3D const guard_pos(0.26F, HP::SHOULDER_Y + 0.08F, 0.28F);
  563. QVector3D const chamber_pos(0.32F, HP::SHOULDER_Y + 0.12F, 0.02F);
  564. QVector3D const thrust_pos(0.28F, HP::SHOULDER_Y + 0.05F, 0.95F);
  565. QVector3D const extended_pos(0.25F, HP::SHOULDER_Y + 0.02F, 1.05F);
  566. QVector3D const recover_pos(0.28F, HP::SHOULDER_Y + 0.06F, 0.45F);
  567. QVector3D hand_r_target;
  568. QVector3D hand_l_target;
  569. float forward_lean = 0.0F;
  570. float torso_twist = 0.0F;
  571. float shoulder_drop = 0.0F;
  572. float step_forward = 0.0F;
  573. float hip_rotation = 0.0F;
  574. auto ease_in_out_cubic = [](float t) {
  575. return t < 0.5F ? 4.0F * t * t * t
  576. : 1.0F - std::pow(-2.0F * t + 2.0F, 3.0F) / 2.0F;
  577. };
  578. auto smoothstep = [](float t) { return t * t * (3.0F - 2.0F * t); };
  579. auto ease_out = [](float t) { return 1.0F - (1.0F - t) * (1.0F - t); };
  580. if (attack_phase < 0.18F) {
  581. float const t = ease_in_out_cubic(attack_phase / 0.18F);
  582. hand_r_target = guard_pos * (1.0F - t) + chamber_pos * t;
  583. hand_l_target = QVector3D(-0.08F, HP::SHOULDER_Y - 0.04F,
  584. 0.22F * (1.0F - t) + 0.06F * t);
  585. torso_twist = -0.06F * t;
  586. hip_rotation = -0.04F * t;
  587. forward_lean = -0.03F * t;
  588. } else if (attack_phase < 0.28F) {
  589. float const t = (attack_phase - 0.18F) / 0.10F;
  590. hand_r_target = chamber_pos;
  591. hand_l_target = QVector3D(-0.08F, HP::SHOULDER_Y - 0.04F, 0.06F);
  592. torso_twist = -0.06F;
  593. hip_rotation = -0.04F;
  594. forward_lean = -0.03F - 0.02F * t;
  595. } else if (attack_phase < 0.48F) {
  596. float t = (attack_phase - 0.28F) / 0.20F;
  597. float power_t = t * t * t;
  598. hand_r_target = chamber_pos * (1.0F - power_t) + thrust_pos * power_t;
  599. hand_l_target = QVector3D(-0.08F + 0.06F * power_t,
  600. HP::SHOULDER_Y - 0.04F + 0.02F * power_t,
  601. 0.06F + 0.50F * power_t);
  602. torso_twist = -0.06F + 0.14F * power_t;
  603. hip_rotation = -0.04F + 0.10F * power_t;
  604. forward_lean = -0.05F + 0.18F * power_t;
  605. shoulder_drop = 0.05F * power_t;
  606. step_forward = 0.10F * power_t;
  607. } else if (attack_phase < 0.60F) {
  608. float const t = smoothstep((attack_phase - 0.48F) / 0.12F);
  609. hand_r_target = thrust_pos * (1.0F - t) + extended_pos * t;
  610. hand_l_target =
  611. QVector3D(-0.02F, HP::SHOULDER_Y - 0.02F, 0.56F + 0.10F * t);
  612. torso_twist = 0.08F;
  613. hip_rotation = 0.06F;
  614. forward_lean = 0.13F + 0.05F * t;
  615. shoulder_drop = 0.05F + 0.02F * t;
  616. step_forward = 0.10F + 0.04F * t;
  617. } else if (attack_phase < 0.78F) {
  618. float const t = ease_in_out_cubic((attack_phase - 0.60F) / 0.18F);
  619. hand_r_target = extended_pos * (1.0F - t) + recover_pos * t;
  620. hand_l_target = QVector3D(-0.02F * (1.0F - t) - 0.08F * t,
  621. HP::SHOULDER_Y - 0.02F * (1.0F - t) - 0.05F * t,
  622. 0.66F * (1.0F - t) + 0.38F * t);
  623. torso_twist = 0.08F * (1.0F - t);
  624. hip_rotation = 0.06F * (1.0F - t);
  625. forward_lean = 0.18F * (1.0F - t) + 0.04F * t;
  626. shoulder_drop = 0.07F * (1.0F - t);
  627. step_forward = 0.14F * (1.0F - t * 0.5F);
  628. } else {
  629. float const t = ease_out((attack_phase - 0.78F) / 0.22F);
  630. hand_r_target = recover_pos * (1.0F - t) + guard_pos * t;
  631. hand_l_target =
  632. QVector3D(-0.08F, HP::SHOULDER_Y - 0.05F * (1.0F - t) - 0.02F * t,
  633. 0.38F * (1.0F - t) + 0.22F * t);
  634. forward_lean = 0.04F * (1.0F - t);
  635. step_forward = 0.07F * (1.0F - t);
  636. }
  637. if (std::abs(torso_twist) > 0.001F) {
  638. float const twist = torso_twist * 0.0F;
  639. m_pose.shoulder_r.setZ(m_pose.shoulder_r.z() + twist);
  640. m_pose.shoulder_l.setZ(m_pose.shoulder_l.z() - twist * 0.4F);
  641. }
  642. if (std::abs(hip_rotation) > 0.001F) {
  643. m_pose.pelvis_pos.setZ(m_pose.pelvis_pos.z() + hip_rotation * 0.5F);
  644. }
  645. if (std::abs(forward_lean) > 0.001F) {
  646. m_pose.shoulder_l.setZ(m_pose.shoulder_l.z() + forward_lean);
  647. m_pose.shoulder_r.setZ(m_pose.shoulder_r.z() + forward_lean);
  648. m_pose.neck_base.setZ(m_pose.neck_base.z() + forward_lean * 0.85F);
  649. m_pose.head_pos.setZ(m_pose.head_pos.z() + forward_lean * 0.7F);
  650. }
  651. if (shoulder_drop > 0.001F) {
  652. m_pose.shoulder_r.setY(m_pose.shoulder_r.y() - shoulder_drop);
  653. m_pose.shoulder_l.setY(m_pose.shoulder_l.y() - shoulder_drop * 0.3F);
  654. }
  655. if (step_forward > 0.001F) {
  656. m_pose.foot_r.setZ(m_pose.foot_r.z() + step_forward);
  657. m_pose.knee_r.setZ(m_pose.knee_r.z() + step_forward * 0.6F);
  658. m_pose.foot_l.setZ(m_pose.foot_l.z() - step_forward * 0.15F);
  659. }
  660. float const thrust_extent =
  661. std::clamp((attack_phase - 0.18F) / 0.60F, 0.0F, 1.0F);
  662. float const along_offset = -0.08F + 0.04F * thrust_extent;
  663. float const y_drop = 0.08F + 0.03F * thrust_extent;
  664. hand_l_target = compute_offhand_spear_grip(
  665. m_pose, m_anim_ctx, hand_r_target, false, along_offset, y_drop, -0.06F);
  666. place_hand_at(false, hand_r_target);
  667. place_hand_at(true, hand_l_target);
  668. }
  669. void HumanoidPoseController::spear_thrust_from_hold(float attack_phase,
  670. float hold_depth) {
  671. using HP = HumanProportions;
  672. attack_phase = std::clamp(attack_phase, 0.0F, 1.0F);
  673. hold_depth = std::clamp(hold_depth, 0.0F, 1.0F);
  674. float const height_offset = -hold_depth * 0.35F;
  675. QVector3D const guard_pos(0.22F, HP::SHOULDER_Y + height_offset + 0.05F,
  676. 0.32F);
  677. QVector3D const chamber_pos(0.28F, HP::SHOULDER_Y + height_offset + 0.10F,
  678. 0.08F);
  679. QVector3D const thrust_pos(0.24F, HP::SHOULDER_Y + height_offset - 0.08F,
  680. 0.90F);
  681. QVector3D const extended_pos(0.22F, HP::SHOULDER_Y + height_offset - 0.12F,
  682. 1.00F);
  683. QVector3D const recover_pos(0.24F, HP::SHOULDER_Y + height_offset + 0.02F,
  684. 0.48F);
  685. QVector3D hand_r_target;
  686. QVector3D hand_l_target;
  687. float forward_lean = 0.0F;
  688. float torso_twist = 0.0F;
  689. float shoulder_extension = 0.0F;
  690. auto smoothstep = [](float t) { return t * t * (3.0F - 2.0F * t); };
  691. auto ease_out = [](float t) { return 1.0F - (1.0F - t) * (1.0F - t); };
  692. auto ease_in = [](float t) { return t * t; };
  693. if (attack_phase < 0.15F) {
  694. float const t = ease_in(attack_phase / 0.15F);
  695. hand_r_target = guard_pos * (1.0F - t) + chamber_pos * t;
  696. hand_l_target = QVector3D(-0.06F, HP::SHOULDER_Y + height_offset - 0.03F,
  697. 0.28F * (1.0F - t) + 0.10F * t);
  698. torso_twist = -0.04F * t;
  699. } else if (attack_phase < 0.22F) {
  700. hand_r_target = chamber_pos;
  701. hand_l_target =
  702. QVector3D(-0.06F, HP::SHOULDER_Y + height_offset - 0.03F, 0.10F);
  703. torso_twist = -0.04F;
  704. } else if (attack_phase < 0.42F) {
  705. float t = (attack_phase - 0.22F) / 0.20F;
  706. float power_t = t * t * t;
  707. hand_r_target = chamber_pos * (1.0F - power_t) + thrust_pos * power_t;
  708. hand_l_target =
  709. QVector3D(-0.06F + 0.05F * power_t,
  710. HP::SHOULDER_Y + height_offset - 0.03F + 0.01F * power_t,
  711. 0.10F + 0.48F * power_t);
  712. torso_twist = -0.04F + 0.10F * power_t;
  713. forward_lean = 0.12F * power_t;
  714. shoulder_extension = 0.06F * power_t;
  715. } else if (attack_phase < 0.55F) {
  716. float const t = smoothstep((attack_phase - 0.42F) / 0.13F);
  717. hand_r_target = thrust_pos * (1.0F - t) + extended_pos * t;
  718. hand_l_target = QVector3D(-0.01F, HP::SHOULDER_Y + height_offset - 0.02F,
  719. 0.58F + 0.08F * t);
  720. torso_twist = 0.06F;
  721. forward_lean = 0.12F + 0.04F * t;
  722. shoulder_extension = 0.06F + 0.03F * t;
  723. } else if (attack_phase < 0.75F) {
  724. float const t = smoothstep((attack_phase - 0.55F) / 0.20F);
  725. hand_r_target = extended_pos * (1.0F - t) + recover_pos * t;
  726. hand_l_target = QVector3D(-0.01F * (1.0F - t) - 0.05F * t,
  727. HP::SHOULDER_Y + height_offset -
  728. 0.02F * (1.0F - t) - 0.04F * t,
  729. 0.66F * (1.0F - t) + 0.40F * t);
  730. torso_twist = 0.06F * (1.0F - t);
  731. forward_lean = 0.16F * (1.0F - t) + 0.03F * t;
  732. shoulder_extension = 0.09F * (1.0F - t);
  733. } else {
  734. float const t = ease_out((attack_phase - 0.75F) / 0.25F);
  735. hand_r_target = recover_pos * (1.0F - t) + guard_pos * t;
  736. hand_l_target = QVector3D(-0.05F - 0.01F * t,
  737. HP::SHOULDER_Y + height_offset -
  738. 0.04F * (1.0F - t) - 0.03F * t,
  739. 0.40F * (1.0F - t) + 0.28F * t);
  740. forward_lean = 0.03F * (1.0F - t);
  741. }
  742. if (std::abs(torso_twist) > 0.001F) {
  743. float const twist = torso_twist * 0.0F;
  744. m_pose.shoulder_r.setZ(m_pose.shoulder_r.z() + twist);
  745. m_pose.shoulder_l.setZ(m_pose.shoulder_l.z() - twist * 0.3F);
  746. }
  747. if (forward_lean > 0.001F) {
  748. m_pose.shoulder_l.setZ(m_pose.shoulder_l.z() + forward_lean);
  749. m_pose.shoulder_r.setZ(m_pose.shoulder_r.z() + forward_lean);
  750. m_pose.neck_base.setZ(m_pose.neck_base.z() + forward_lean * 0.9F);
  751. m_pose.head_pos.setZ(m_pose.head_pos.z() + forward_lean * 0.75F);
  752. }
  753. if (shoulder_extension > 0.001F) {
  754. m_pose.shoulder_r.setZ(m_pose.shoulder_r.z() + shoulder_extension);
  755. m_pose.shoulder_r.setY(m_pose.shoulder_r.y() - shoulder_extension * 0.3F);
  756. }
  757. float const thrust_extent =
  758. std::clamp((attack_phase - 0.15F) / 0.55F, 0.0F, 1.0F);
  759. float const along_offset = -0.06F + 0.03F * thrust_extent;
  760. float const y_drop = 0.06F + 0.02F * thrust_extent;
  761. hand_l_target = compute_offhand_spear_grip(
  762. m_pose, m_anim_ctx, hand_r_target, false, along_offset, y_drop, -0.05F);
  763. place_hand_at(false, hand_r_target);
  764. place_hand_at(true, hand_l_target);
  765. }
  766. void HumanoidPoseController::sword_slash(float attack_phase) {
  767. using HP = HumanProportions;
  768. attack_phase = std::clamp(attack_phase, 0.0F, 1.0F);
  769. QVector3D const rest_pos(0.20F, HP::SHOULDER_Y + 0.05F, 0.15F);
  770. QVector3D const chamber_pos(0.28F, HP::SHOULDER_Y + 0.20F, 0.02F);
  771. QVector3D const apex_pos(0.30F, HP::SHOULDER_Y + 0.25F, 0.08F);
  772. QVector3D const strike_pos(0.18F, HP::SHOULDER_Y - 0.15F, 0.62F);
  773. QVector3D const followthrough_pos(0.05F, HP::WAIST_Y + 0.10F, 0.50F);
  774. QVector3D const recover_pos(0.22F, HP::SHOULDER_Y + 0.02F, 0.22F);
  775. QVector3D hand_r_target;
  776. QVector3D hand_l_target;
  777. float torso_twist = 0.0F;
  778. float forward_lean = 0.0F;
  779. float shoulder_rotation = 0.0F;
  780. float weight_shift = 0.0F;
  781. auto smoothstep = [](float t) { return t * t * (3.0F - 2.0F * t); };
  782. auto ease_out = [](float t) { return 1.0F - (1.0F - t) * (1.0F - t); };
  783. auto ease_in = [](float t) { return t * t; };
  784. if (attack_phase < 0.15F) {
  785. float t = attack_phase / 0.15F;
  786. float ease_t = ease_in(t);
  787. hand_r_target = rest_pos * (1.0F - ease_t) + chamber_pos * ease_t;
  788. hand_l_target =
  789. QVector3D(-0.20F, HP::SHOULDER_Y - 0.02F, 0.15F + 0.02F * t);
  790. torso_twist = -0.05F * ease_t;
  791. shoulder_rotation = 0.03F * ease_t;
  792. } else if (attack_phase < 0.28F) {
  793. float t = (attack_phase - 0.15F) / 0.13F;
  794. float ease_t = smoothstep(t);
  795. hand_r_target = chamber_pos * (1.0F - ease_t) + apex_pos * ease_t;
  796. hand_l_target = QVector3D(-0.20F, HP::SHOULDER_Y - 0.04F, 0.17F);
  797. torso_twist = -0.05F;
  798. shoulder_rotation = 0.03F + 0.02F * ease_t;
  799. weight_shift = -0.02F * ease_t;
  800. } else if (attack_phase < 0.48F) {
  801. float t = (attack_phase - 0.28F) / 0.20F;
  802. float power_t = t * t * t;
  803. hand_r_target = apex_pos * (1.0F - power_t) + strike_pos * power_t;
  804. hand_l_target = QVector3D(-0.20F + 0.08F * power_t,
  805. HP::SHOULDER_Y - 0.04F - 0.06F * power_t,
  806. 0.17F + 0.22F * power_t);
  807. torso_twist = -0.05F + 0.14F * power_t;
  808. forward_lean = 0.10F * power_t;
  809. shoulder_rotation = 0.05F - 0.08F * power_t;
  810. weight_shift = -0.02F + 0.08F * power_t;
  811. } else if (attack_phase < 0.62F) {
  812. float t = (attack_phase - 0.48F) / 0.14F;
  813. float ease_t = smoothstep(t);
  814. hand_r_target = strike_pos * (1.0F - ease_t) + followthrough_pos * ease_t;
  815. hand_l_target = QVector3D(-0.12F, HP::SHOULDER_Y - 0.10F, 0.39F);
  816. torso_twist = 0.09F - 0.03F * t;
  817. forward_lean = 0.10F - 0.02F * t;
  818. weight_shift = 0.06F;
  819. } else {
  820. float t = (attack_phase - 0.62F) / 0.38F;
  821. float ease_t = ease_out(t);
  822. hand_r_target = followthrough_pos * (1.0F - ease_t) +
  823. recover_pos * 0.5F * ease_t + rest_pos * 0.5F * ease_t;
  824. hand_l_target = QVector3D(-0.12F - 0.08F * ease_t,
  825. HP::SHOULDER_Y - 0.10F * (1.0F - ease_t),
  826. 0.39F * (1.0F - ease_t) + 0.15F * ease_t);
  827. torso_twist = 0.06F * (1.0F - ease_t);
  828. forward_lean = 0.08F * (1.0F - ease_t);
  829. weight_shift = 0.06F * (1.0F - ease_t);
  830. }
  831. if (std::abs(torso_twist) > 0.001F) {
  832. float const twist = torso_twist * 0.0F;
  833. m_pose.shoulder_r.setZ(m_pose.shoulder_r.z() + twist);
  834. m_pose.shoulder_l.setZ(m_pose.shoulder_l.z() - twist * 0.6F);
  835. }
  836. if (std::abs(shoulder_rotation) > 0.001F) {
  837. m_pose.shoulder_r.setY(m_pose.shoulder_r.y() - shoulder_rotation);
  838. m_pose.shoulder_l.setY(m_pose.shoulder_l.y() + shoulder_rotation * 0.4F);
  839. }
  840. if (forward_lean > 0.001F) {
  841. m_pose.shoulder_l.setZ(m_pose.shoulder_l.z() + forward_lean);
  842. m_pose.shoulder_r.setZ(m_pose.shoulder_r.z() + forward_lean);
  843. m_pose.neck_base.setZ(m_pose.neck_base.z() + forward_lean * 0.7F);
  844. m_pose.head_pos.setZ(m_pose.head_pos.z() + forward_lean * 0.5F);
  845. m_pose.pelvis_pos.setZ(m_pose.pelvis_pos.z() + forward_lean * 0.3F);
  846. }
  847. if (std::abs(weight_shift) > 0.001F) {
  848. m_pose.foot_r.setZ(m_pose.foot_r.z() + weight_shift);
  849. m_pose.knee_r.setZ(m_pose.knee_r.z() + weight_shift * 0.6F);
  850. }
  851. place_hand_at(false, hand_r_target);
  852. place_hand_at(true, hand_l_target);
  853. }
  854. void HumanoidPoseController::mount_on_horse(float saddle_height) {
  855. float const offset_y = saddle_height - m_pose.pelvis_pos.y();
  856. m_pose.pelvis_pos.setY(saddle_height);
  857. }
  858. void HumanoidPoseController::hold_sword_and_shield() {
  859. using HP = HumanProportions;
  860. QVector3D const sword_hand_pos(0.30F, HP::SHOULDER_Y - 0.02F, 0.35F);
  861. QVector3D const shield_hand_pos(-0.22F, HP::SHOULDER_Y, 0.18F);
  862. place_hand_at(false, sword_hand_pos);
  863. place_hand_at(true, shield_hand_pos);
  864. }
  865. void HumanoidPoseController::look_at(const QVector3D &target) {
  866. QVector3D const head_to_target = target - m_pose.head_pos;
  867. if (head_to_target.lengthSquared() < 1e-6F) {
  868. return;
  869. }
  870. QVector3D const direction = head_to_target.normalized();
  871. float const max_head_turn = 0.03F;
  872. QVector3D const head_offset = direction * max_head_turn;
  873. m_pose.head_pos += QVector3D(head_offset.x(), 0.0F, head_offset.z());
  874. float const neck_follow = 0.5F;
  875. m_pose.neck_base += QVector3D(head_offset.x() * neck_follow, 0.0F,
  876. head_offset.z() * neck_follow);
  877. }
  878. void HumanoidPoseController::hit_flinch(float intensity) {
  879. intensity = std::clamp(intensity, 0.0F, 1.0F);
  880. if (intensity < 0.01F) {
  881. return;
  882. }
  883. float const flinch_back = intensity * 0.06F;
  884. float const flinch_down = intensity * 0.04F;
  885. float const shoulder_drop = intensity * 0.03F;
  886. m_pose.head_pos.setZ(m_pose.head_pos.z() - flinch_back);
  887. m_pose.head_pos.setY(m_pose.head_pos.y() - flinch_down * 0.5F);
  888. m_pose.neck_base.setZ(m_pose.neck_base.z() - flinch_back * 0.8F);
  889. m_pose.shoulder_l.setY(m_pose.shoulder_l.y() - shoulder_drop);
  890. m_pose.shoulder_r.setY(m_pose.shoulder_r.y() - shoulder_drop);
  891. m_pose.shoulder_l.setZ(m_pose.shoulder_l.z() - flinch_back * 0.6F);
  892. m_pose.shoulder_r.setZ(m_pose.shoulder_r.z() - flinch_back * 0.6F);
  893. m_pose.pelvis_pos.setY(m_pose.pelvis_pos.y() - flinch_down * 0.3F);
  894. }
  895. void HumanoidPoseController::sword_slash_variant(float attack_phase,
  896. std::uint8_t variant) {
  897. using HP = HumanProportions;
  898. attack_phase = std::clamp(attack_phase, 0.0F, 1.0F);
  899. constexpr float k_strike_right_to_left = 1.0F;
  900. constexpr float k_strike_left_to_right = -1.0F;
  901. QVector3D rest_pos(0.20F, HP::SHOULDER_Y + 0.05F, 0.15F);
  902. QVector3D chamber_pos(0.28F, HP::SHOULDER_Y + 0.20F, 0.02F);
  903. QVector3D apex_pos(0.30F, HP::SHOULDER_Y + 0.25F, 0.08F);
  904. QVector3D strike_pos(0.18F, HP::SHOULDER_Y - 0.15F, 0.62F);
  905. QVector3D followthrough_pos(0.05F, HP::WAIST_Y + 0.10F, 0.50F);
  906. float strike_direction = k_strike_right_to_left;
  907. switch (variant % 3) {
  908. case 1:
  909. chamber_pos = QVector3D(-0.10F, HP::SHOULDER_Y + 0.22F, 0.04F);
  910. apex_pos = QVector3D(-0.08F, HP::SHOULDER_Y + 0.28F, 0.10F);
  911. strike_pos = QVector3D(0.32F, HP::SHOULDER_Y - 0.12F, 0.58F);
  912. followthrough_pos = QVector3D(0.40F, HP::WAIST_Y + 0.08F, 0.48F);
  913. strike_direction = k_strike_left_to_right;
  914. break;
  915. case 2:
  916. chamber_pos = QVector3D(0.35F, HP::SHOULDER_Y + 0.10F, 0.0F);
  917. apex_pos = QVector3D(0.38F, HP::SHOULDER_Y + 0.08F, 0.06F);
  918. strike_pos = QVector3D(0.05F, HP::SHOULDER_Y - 0.05F, 0.65F);
  919. followthrough_pos = QVector3D(-0.10F, HP::SHOULDER_Y - 0.10F, 0.55F);
  920. break;
  921. default:
  922. break;
  923. }
  924. QVector3D hand_r_target;
  925. QVector3D hand_l_target;
  926. float torso_twist = 0.0F;
  927. float forward_lean = 0.0F;
  928. float shoulder_rotation = 0.0F;
  929. float weight_shift = 0.0F;
  930. auto smoothstep = [](float t) { return t * t * (3.0F - 2.0F * t); };
  931. auto ease_out = [](float t) { return 1.0F - (1.0F - t) * (1.0F - t); };
  932. if (attack_phase < 0.15F) {
  933. float t = attack_phase / 0.15F;
  934. float ease_t = t * t;
  935. hand_r_target = rest_pos * (1.0F - ease_t) + chamber_pos * ease_t;
  936. hand_l_target = QVector3D(-0.20F, HP::SHOULDER_Y - 0.02F, 0.15F);
  937. torso_twist = strike_direction * (-0.05F * ease_t);
  938. shoulder_rotation = 0.03F * ease_t;
  939. } else if (attack_phase < 0.28F) {
  940. float t = (attack_phase - 0.15F) / 0.13F;
  941. float ease_t = smoothstep(t);
  942. hand_r_target = chamber_pos * (1.0F - ease_t) + apex_pos * ease_t;
  943. hand_l_target = QVector3D(-0.20F, HP::SHOULDER_Y - 0.04F, 0.17F);
  944. torso_twist = strike_direction * (-0.05F);
  945. shoulder_rotation = 0.03F + 0.02F * ease_t;
  946. weight_shift = -0.02F * ease_t;
  947. } else if (attack_phase < 0.48F) {
  948. float t = (attack_phase - 0.28F) / 0.20F;
  949. float power_t = t * t * t;
  950. hand_r_target = apex_pos * (1.0F - power_t) + strike_pos * power_t;
  951. hand_l_target = QVector3D(-0.20F + 0.08F * power_t,
  952. HP::SHOULDER_Y - 0.04F - 0.06F * power_t,
  953. 0.17F + 0.22F * power_t);
  954. torso_twist = strike_direction * (-0.05F + 0.14F * power_t);
  955. forward_lean = 0.10F * power_t;
  956. shoulder_rotation = 0.05F - 0.08F * power_t;
  957. weight_shift = -0.02F + 0.08F * power_t;
  958. } else if (attack_phase < 0.62F) {
  959. float t = (attack_phase - 0.48F) / 0.14F;
  960. float ease_t = smoothstep(t);
  961. hand_r_target = strike_pos * (1.0F - ease_t) + followthrough_pos * ease_t;
  962. hand_l_target = QVector3D(-0.12F, HP::SHOULDER_Y - 0.10F, 0.39F);
  963. torso_twist = strike_direction * (0.09F - 0.03F * t);
  964. forward_lean = 0.10F - 0.02F * t;
  965. weight_shift = 0.06F;
  966. } else {
  967. float t = (attack_phase - 0.62F) / 0.38F;
  968. float ease_t = ease_out(t);
  969. hand_r_target = followthrough_pos * (1.0F - ease_t) + rest_pos * ease_t;
  970. hand_l_target = QVector3D(-0.12F - 0.08F * ease_t,
  971. HP::SHOULDER_Y - 0.10F * (1.0F - ease_t),
  972. 0.39F * (1.0F - ease_t) + 0.15F * ease_t);
  973. torso_twist = 0.06F * strike_direction * (1.0F - ease_t);
  974. forward_lean = 0.08F * (1.0F - ease_t);
  975. weight_shift = 0.06F * (1.0F - ease_t);
  976. }
  977. if (std::abs(torso_twist) > 0.001F) {
  978. float const twist = torso_twist * 0.0F;
  979. m_pose.shoulder_r.setZ(m_pose.shoulder_r.z() + twist);
  980. m_pose.shoulder_l.setZ(m_pose.shoulder_l.z() - twist * 0.6F);
  981. }
  982. if (std::abs(shoulder_rotation) > 0.001F) {
  983. m_pose.shoulder_r.setY(m_pose.shoulder_r.y() - shoulder_rotation);
  984. m_pose.shoulder_l.setY(m_pose.shoulder_l.y() + shoulder_rotation * 0.4F);
  985. }
  986. if (forward_lean > 0.001F) {
  987. m_pose.shoulder_l.setZ(m_pose.shoulder_l.z() + forward_lean);
  988. m_pose.shoulder_r.setZ(m_pose.shoulder_r.z() + forward_lean);
  989. m_pose.neck_base.setZ(m_pose.neck_base.z() + forward_lean * 0.7F);
  990. m_pose.head_pos.setZ(m_pose.head_pos.z() + forward_lean * 0.5F);
  991. }
  992. if (std::abs(weight_shift) > 0.001F) {
  993. m_pose.foot_r.setZ(m_pose.foot_r.z() + weight_shift);
  994. m_pose.knee_r.setZ(m_pose.knee_r.z() + weight_shift * 0.6F);
  995. }
  996. place_hand_at(false, hand_r_target);
  997. place_hand_at(true, hand_l_target);
  998. }
  999. void HumanoidPoseController::spear_thrust_variant(float attack_phase,
  1000. std::uint8_t variant) {
  1001. using HP = HumanProportions;
  1002. attack_phase = std::clamp(attack_phase, 0.0F, 1.0F);
  1003. constexpr float k_thrust_high = 1.0F;
  1004. constexpr float k_thrust_middle = 0.0F;
  1005. constexpr float k_thrust_low = -1.0F;
  1006. QVector3D guard_pos(0.26F, HP::SHOULDER_Y + 0.08F, 0.28F);
  1007. QVector3D chamber_pos(0.32F, HP::SHOULDER_Y + 0.12F, 0.02F);
  1008. QVector3D thrust_pos(0.28F, HP::SHOULDER_Y + 0.05F, 0.95F);
  1009. QVector3D extended_pos(0.25F, HP::SHOULDER_Y + 0.02F, 1.05F);
  1010. QVector3D recover_pos(0.28F, HP::SHOULDER_Y + 0.06F, 0.45F);
  1011. float thrust_height = k_thrust_middle;
  1012. float crouch_amount = 0.0F;
  1013. switch (variant % 3) {
  1014. case 1:
  1015. chamber_pos = QVector3D(0.30F, HP::SHOULDER_Y + 0.18F, 0.0F);
  1016. thrust_pos = QVector3D(0.28F, HP::WAIST_Y + 0.15F, 0.98F);
  1017. extended_pos = QVector3D(0.25F, HP::WAIST_Y + 0.10F, 1.08F);
  1018. recover_pos = QVector3D(0.28F, HP::SHOULDER_Y - 0.05F, 0.42F);
  1019. thrust_height = k_thrust_low;
  1020. crouch_amount = 0.08F;
  1021. break;
  1022. case 2:
  1023. chamber_pos = QVector3D(0.35F, HP::SHOULDER_Y + 0.05F, 0.08F);
  1024. thrust_pos = QVector3D(0.30F, HP::SHOULDER_Y + 0.12F, 0.92F);
  1025. extended_pos = QVector3D(0.28F, HP::SHOULDER_Y + 0.15F, 1.02F);
  1026. thrust_height = k_thrust_high;
  1027. break;
  1028. default:
  1029. break;
  1030. }
  1031. QVector3D hand_r_target;
  1032. QVector3D hand_l_target;
  1033. float forward_lean = 0.0F;
  1034. float torso_twist = 0.0F;
  1035. float shoulder_drop = 0.0F;
  1036. float step_forward = 0.0F;
  1037. float hip_rotation = 0.0F;
  1038. float crouch_factor = 0.0F;
  1039. auto ease_in_out_cubic = [](float t) {
  1040. return t < 0.5F ? 4.0F * t * t * t
  1041. : 1.0F - std::pow(-2.0F * t + 2.0F, 3.0F) / 2.0F;
  1042. };
  1043. auto smoothstep = [](float t) { return t * t * (3.0F - 2.0F * t); };
  1044. auto ease_out = [](float t) { return 1.0F - (1.0F - t) * (1.0F - t); };
  1045. if (attack_phase < 0.18F) {
  1046. float const t = ease_in_out_cubic(attack_phase / 0.18F);
  1047. hand_r_target = guard_pos * (1.0F - t) + chamber_pos * t;
  1048. hand_l_target = QVector3D(-0.08F, HP::SHOULDER_Y - 0.04F,
  1049. 0.22F * (1.0F - t) + 0.06F * t);
  1050. torso_twist = -0.06F * t;
  1051. hip_rotation = -0.04F * t;
  1052. forward_lean = -0.03F * t;
  1053. crouch_factor = crouch_amount * t;
  1054. } else if (attack_phase < 0.28F) {
  1055. hand_r_target = chamber_pos;
  1056. hand_l_target = QVector3D(-0.08F, HP::SHOULDER_Y - 0.04F, 0.06F);
  1057. torso_twist = -0.06F;
  1058. hip_rotation = -0.04F;
  1059. forward_lean = -0.03F;
  1060. crouch_factor = crouch_amount;
  1061. } else if (attack_phase < 0.48F) {
  1062. float t = (attack_phase - 0.28F) / 0.20F;
  1063. float power_t = t * t * t;
  1064. hand_r_target = chamber_pos * (1.0F - power_t) + thrust_pos * power_t;
  1065. hand_l_target = QVector3D(-0.08F + 0.06F * power_t,
  1066. HP::SHOULDER_Y - 0.04F + 0.02F * power_t,
  1067. 0.06F + 0.50F * power_t);
  1068. torso_twist = -0.06F + 0.14F * power_t;
  1069. hip_rotation = -0.04F + 0.10F * power_t;
  1070. forward_lean = -0.05F + 0.20F * power_t;
  1071. shoulder_drop = 0.05F * power_t;
  1072. step_forward = 0.12F * power_t;
  1073. crouch_factor = crouch_amount * (1.0F - power_t * 0.3F);
  1074. if (thrust_height < 0) {
  1075. crouch_factor += 0.06F * power_t;
  1076. } else if (thrust_height > 0) {
  1077. crouch_factor -= 0.03F * power_t;
  1078. }
  1079. } else if (attack_phase < 0.60F) {
  1080. float const t = smoothstep((attack_phase - 0.48F) / 0.12F);
  1081. hand_r_target = thrust_pos * (1.0F - t) + extended_pos * t;
  1082. hand_l_target =
  1083. QVector3D(-0.02F, HP::SHOULDER_Y - 0.02F, 0.56F + 0.10F * t);
  1084. torso_twist = 0.08F;
  1085. hip_rotation = 0.06F;
  1086. forward_lean = 0.15F + 0.05F * t;
  1087. shoulder_drop = 0.05F + 0.02F * t;
  1088. step_forward = 0.12F + 0.04F * t;
  1089. crouch_factor = crouch_amount * 0.7F;
  1090. } else if (attack_phase < 0.78F) {
  1091. float const t = ease_in_out_cubic((attack_phase - 0.60F) / 0.18F);
  1092. hand_r_target = extended_pos * (1.0F - t) + recover_pos * t;
  1093. hand_l_target = QVector3D(-0.02F * (1.0F - t) - 0.08F * t,
  1094. HP::SHOULDER_Y - 0.02F * (1.0F - t) - 0.05F * t,
  1095. 0.66F * (1.0F - t) + 0.38F * t);
  1096. torso_twist = 0.08F * (1.0F - t);
  1097. hip_rotation = 0.06F * (1.0F - t);
  1098. forward_lean = 0.20F * (1.0F - t) + 0.04F * t;
  1099. shoulder_drop = 0.07F * (1.0F - t);
  1100. step_forward = 0.16F * (1.0F - t * 0.5F);
  1101. crouch_factor = crouch_amount * 0.7F * (1.0F - t);
  1102. } else {
  1103. float const t = ease_out((attack_phase - 0.78F) / 0.22F);
  1104. hand_r_target = recover_pos * (1.0F - t) + guard_pos * t;
  1105. hand_l_target =
  1106. QVector3D(-0.08F, HP::SHOULDER_Y - 0.05F * (1.0F - t) - 0.02F * t,
  1107. 0.38F * (1.0F - t) + 0.22F * t);
  1108. forward_lean = 0.04F * (1.0F - t);
  1109. step_forward = 0.08F * (1.0F - t);
  1110. }
  1111. if (std::abs(torso_twist) > 0.001F) {
  1112. float const twist = torso_twist * 0.0F;
  1113. m_pose.shoulder_r.setZ(m_pose.shoulder_r.z() + twist);
  1114. m_pose.shoulder_l.setZ(m_pose.shoulder_l.z() - twist * 0.4F);
  1115. }
  1116. if (std::abs(hip_rotation) > 0.001F) {
  1117. m_pose.pelvis_pos.setZ(m_pose.pelvis_pos.z() + hip_rotation * 0.5F);
  1118. }
  1119. if (std::abs(forward_lean) > 0.001F) {
  1120. m_pose.shoulder_l.setZ(m_pose.shoulder_l.z() + forward_lean);
  1121. m_pose.shoulder_r.setZ(m_pose.shoulder_r.z() + forward_lean);
  1122. m_pose.neck_base.setZ(m_pose.neck_base.z() + forward_lean * 0.85F);
  1123. m_pose.head_pos.setZ(m_pose.head_pos.z() + forward_lean * 0.7F);
  1124. }
  1125. if (shoulder_drop > 0.001F) {
  1126. m_pose.shoulder_r.setY(m_pose.shoulder_r.y() - shoulder_drop);
  1127. m_pose.shoulder_l.setY(m_pose.shoulder_l.y() - shoulder_drop * 0.3F);
  1128. }
  1129. if (step_forward > 0.001F) {
  1130. m_pose.foot_r.setZ(m_pose.foot_r.z() + step_forward);
  1131. m_pose.knee_r.setZ(m_pose.knee_r.z() + step_forward * 0.6F);
  1132. m_pose.foot_l.setZ(m_pose.foot_l.z() - step_forward * 0.15F);
  1133. }
  1134. if (crouch_factor > 0.001F) {
  1135. m_pose.pelvis_pos.setY(m_pose.pelvis_pos.y() - crouch_factor);
  1136. m_pose.shoulder_l.setY(m_pose.shoulder_l.y() - crouch_factor * 0.6F);
  1137. m_pose.shoulder_r.setY(m_pose.shoulder_r.y() - crouch_factor * 0.6F);
  1138. m_pose.neck_base.setY(m_pose.neck_base.y() - crouch_factor * 0.5F);
  1139. m_pose.head_pos.setY(m_pose.head_pos.y() - crouch_factor * 0.4F);
  1140. }
  1141. place_hand_at(false, hand_r_target);
  1142. place_hand_at(true, hand_l_target);
  1143. }
  1144. void HumanoidPoseController::tilt_torso(float side_tilt, float forward_tilt) {
  1145. QVector3D const right = m_anim_ctx.heading_right();
  1146. QVector3D const forward = m_anim_ctx.heading_forward();
  1147. QVector3D const offset = right * side_tilt + forward * forward_tilt;
  1148. m_pose.shoulder_l += offset;
  1149. m_pose.shoulder_r += offset;
  1150. m_pose.neck_base += offset * 1.2F;
  1151. m_pose.head_pos += offset * 1.5F;
  1152. m_pose.body_frames.torso.origin += offset;
  1153. m_pose.body_frames.head.origin += offset * 1.5F;
  1154. }
  1155. } // namespace Render::GL