pose_controller.cpp 51 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475
  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,
  255. bool standing_up) {
  256. using HP = HumanProportions;
  257. progress = std::clamp(progress, 0.0F, 1.0F);
  258. auto ease_in_out = [](float t) { return t * t * (3.0F - 2.0F * t); };
  259. float kneel_amount = standing_up ? (1.0F - progress) : progress;
  260. float eased_progress = ease_in_out(progress);
  261. kneel(kneel_amount);
  262. if (standing_up) {
  263. if (progress < 0.35F) {
  264. float t = progress / 0.35F;
  265. float push_t = ease_in_out(t);
  266. m_pose.foot_r.setZ(m_pose.foot_r.z() - 0.08F * push_t);
  267. m_pose.knee_r.setZ(m_pose.knee_r.z() - 0.05F * push_t);
  268. float momentum_lean = 0.06F * push_t;
  269. m_pose.shoulder_l.setZ(m_pose.shoulder_l.z() + momentum_lean);
  270. m_pose.shoulder_r.setZ(m_pose.shoulder_r.z() + momentum_lean);
  271. m_pose.neck_base.setZ(m_pose.neck_base.z() + momentum_lean * 0.9F);
  272. m_pose.head_pos.setZ(m_pose.head_pos.z() + momentum_lean * 0.7F);
  273. m_pose.hand_l.setZ(m_pose.hand_l.z() + 0.04F * push_t);
  274. m_pose.hand_r.setZ(m_pose.hand_r.z() + 0.04F * push_t);
  275. }
  276. else if (progress < 0.70F) {
  277. float t = (progress - 0.35F) / 0.35F;
  278. float rise_t = ease_in_out(t);
  279. float lift_boost = 0.02F * std::sin(rise_t * std::numbers::pi_v<float>);
  280. m_pose.pelvis_pos.setY(m_pose.pelvis_pos.y() + lift_boost);
  281. m_pose.shoulder_l.setY(m_pose.shoulder_l.y() + lift_boost);
  282. m_pose.shoulder_r.setY(m_pose.shoulder_r.y() + lift_boost);
  283. m_pose.foot_l.setZ(m_pose.foot_l.z() + 0.15F * rise_t);
  284. m_pose.knee_l.setZ(m_pose.knee_l.z() + 0.10F * rise_t);
  285. m_pose.knee_l.setY(m_pose.knee_l.y() + 0.20F * rise_t);
  286. }
  287. else {
  288. float t = (progress - 0.70F) / 0.30F;
  289. float settle_t = ease_in_out(t);
  290. float correct_lean = -0.04F * settle_t * (1.0F - kneel_amount);
  291. m_pose.shoulder_l.setZ(m_pose.shoulder_l.z() + correct_lean);
  292. m_pose.shoulder_r.setZ(m_pose.shoulder_r.z() + correct_lean);
  293. }
  294. } else {
  295. if (progress < 0.30F) {
  296. float t = progress / 0.30F;
  297. float prep_t = ease_in_out(t);
  298. m_pose.pelvis_pos.setZ(m_pose.pelvis_pos.z() - 0.03F * prep_t);
  299. m_pose.hand_l.setY(m_pose.hand_l.y() - 0.02F * prep_t);
  300. m_pose.hand_r.setY(m_pose.hand_r.y() - 0.02F * prep_t);
  301. }
  302. else if (progress < 0.75F) {
  303. float t = (progress - 0.30F) / 0.45F;
  304. float controlled_lean = 0.04F * std::sin(t * std::numbers::pi_v<float>);
  305. m_pose.shoulder_l.setZ(m_pose.shoulder_l.z() + controlled_lean);
  306. m_pose.shoulder_r.setZ(m_pose.shoulder_r.z() + controlled_lean);
  307. }
  308. else {
  309. float t = (progress - 0.75F) / 0.25F;
  310. float settle_t = ease_in_out(t);
  311. m_pose.knee_l.setY(m_pose.knee_l.y() - 0.01F * settle_t);
  312. }
  313. }
  314. }
  315. void HumanoidPoseController::lean(const QVector3D &direction, float amount) {
  316. amount = std::clamp(amount, 0.0F, 1.0F);
  317. QVector3D dir = direction;
  318. if (dir.lengthSquared() > 1e-6F) {
  319. dir.normalize();
  320. } else {
  321. dir = QVector3D(0.0F, 0.0F, 1.0F);
  322. }
  323. float const lean_magnitude = 0.12F * amount;
  324. QVector3D const lean_offset = dir * lean_magnitude;
  325. m_pose.shoulder_l += lean_offset;
  326. m_pose.shoulder_r += lean_offset;
  327. m_pose.neck_base += lean_offset * 0.85F;
  328. m_pose.head_pos += lean_offset * 0.75F;
  329. }
  330. void HumanoidPoseController::place_hand_at(bool is_left,
  331. const QVector3D &target_position) {
  332. get_hand(is_left) = target_position;
  333. const QVector3D &shoulder = get_shoulder(is_left);
  334. const QVector3D outward_dir = compute_outward_dir(is_left);
  335. float const along_frac = is_left ? 0.45F : 0.48F;
  336. float const lateral_offset = is_left ? 0.15F : 0.12F;
  337. float const y_bias = is_left ? -0.08F : 0.02F;
  338. float const outward_sign = 1.0F;
  339. get_elbow(is_left) =
  340. solve_elbow_ik(is_left, shoulder, target_position, outward_dir,
  341. along_frac, lateral_offset, y_bias, outward_sign);
  342. }
  343. auto HumanoidPoseController::solve_elbow_ik(
  344. bool, const QVector3D &shoulder, const QVector3D &hand,
  345. const QVector3D &outward_dir, float along_frac, float lateral_offset,
  346. float y_bias, float outward_sign) const -> QVector3D {
  347. return elbow_bend_torso(shoulder, hand, outward_dir, along_frac,
  348. lateral_offset, y_bias, outward_sign);
  349. }
  350. auto HumanoidPoseController::solve_knee_ik(
  351. bool is_left, const QVector3D &hip, const QVector3D &foot,
  352. float height_scale) const -> QVector3D {
  353. using HP = HumanProportions;
  354. QVector3D hip_to_foot = foot - hip;
  355. float const distance = hip_to_foot.length();
  356. if (distance < 1e-5F) {
  357. return hip;
  358. }
  359. float const upper_len = HP::UPPER_LEG_LEN * height_scale;
  360. float const lower_len = HP::LOWER_LEG_LEN * height_scale;
  361. float const reach = upper_len + lower_len;
  362. float const min_reach =
  363. std::max(std::abs(upper_len - lower_len) + 1e-4F, 1e-3F);
  364. float const max_reach = std::max(reach - 1e-4F, min_reach + 1e-4F);
  365. float const clamped_dist = std::clamp(distance, min_reach, max_reach);
  366. QVector3D const dir = hip_to_foot / distance;
  367. float cos_theta = (upper_len * upper_len + clamped_dist * clamped_dist -
  368. lower_len * lower_len) /
  369. (2.0F * upper_len * clamped_dist);
  370. cos_theta = std::clamp(cos_theta, -1.0F, 1.0F);
  371. float const sin_theta =
  372. std::sqrt(std::max(0.0F, 1.0F - cos_theta * cos_theta));
  373. QVector3D bend_pref =
  374. is_left ? QVector3D(-0.24F, 0.0F, 0.95F) : QVector3D(0.24F, 0.0F, 0.95F);
  375. bend_pref.normalize();
  376. QVector3D bend_axis = bend_pref - dir * QVector3D::dotProduct(dir, bend_pref);
  377. if (bend_axis.lengthSquared() < 1e-6F) {
  378. bend_axis = QVector3D::crossProduct(dir, QVector3D(0.0F, 1.0F, 0.0F));
  379. if (bend_axis.lengthSquared() < 1e-6F) {
  380. bend_axis = QVector3D::crossProduct(dir, QVector3D(1.0F, 0.0F, 0.0F));
  381. }
  382. }
  383. bend_axis.normalize();
  384. QVector3D knee =
  385. hip + dir * (cos_theta * upper_len) + bend_axis * (sin_theta * upper_len);
  386. float const knee_floor = HP::GROUND_Y + m_pose.foot_y_offset * 0.5F;
  387. if (knee.y() < knee_floor) {
  388. knee.setY(knee_floor);
  389. }
  390. if (knee.y() > hip.y()) {
  391. knee.setY(hip.y());
  392. }
  393. return knee;
  394. }
  395. auto HumanoidPoseController::get_shoulder(bool is_left) const
  396. -> const QVector3D & {
  397. return is_left ? m_pose.shoulder_l : m_pose.shoulder_r;
  398. }
  399. auto HumanoidPoseController::get_hand(bool is_left) -> QVector3D & {
  400. return is_left ? m_pose.hand_l : m_pose.hand_r;
  401. }
  402. auto HumanoidPoseController::get_hand(bool is_left) const -> const QVector3D & {
  403. return is_left ? m_pose.hand_l : m_pose.hand_r;
  404. }
  405. auto HumanoidPoseController::get_elbow(bool is_left) -> QVector3D & {
  406. return is_left ? m_pose.elbow_l : m_pose.elbow_r;
  407. }
  408. auto HumanoidPoseController::compute_right_axis() const -> QVector3D {
  409. QVector3D right_axis = m_pose.shoulder_r - m_pose.shoulder_l;
  410. right_axis.setY(0.0F);
  411. if (right_axis.lengthSquared() < 1e-8F) {
  412. right_axis = QVector3D(1.0F, 0.0F, 0.0F);
  413. }
  414. right_axis.normalize();
  415. return right_axis;
  416. }
  417. auto HumanoidPoseController::compute_outward_dir(bool is_left) const
  418. -> QVector3D {
  419. QVector3D const right_axis = compute_right_axis();
  420. return is_left ? -right_axis : right_axis;
  421. }
  422. auto HumanoidPoseController::get_shoulder_y(bool is_left) const -> float {
  423. return is_left ? m_pose.shoulder_l.y() : m_pose.shoulder_r.y();
  424. }
  425. auto HumanoidPoseController::get_pelvis_y() const -> float {
  426. return m_pose.pelvis_pos.y();
  427. }
  428. void HumanoidPoseController::aim_bow(float draw_phase) {
  429. using HP = HumanProportions;
  430. draw_phase = std::clamp(draw_phase, 0.0F, 1.0F);
  431. QVector3D const aim_pos(-0.02F, HP::SHOULDER_Y + 0.18F, 0.42F);
  432. QVector3D const draw_pos(-0.05F, HP::SHOULDER_Y + 0.12F, 0.22F);
  433. QVector3D const release_pos(-0.02F, HP::SHOULDER_Y + 0.20F, 0.34F);
  434. QVector3D hand_l_target;
  435. float shoulder_twist = 0.0F;
  436. float head_recoil = 0.0F;
  437. if (draw_phase < 0.20F) {
  438. float t = draw_phase / 0.20F;
  439. t = t * t;
  440. hand_l_target = aim_pos * (1.0F - t) + draw_pos * t;
  441. shoulder_twist = t * 0.08F;
  442. } else if (draw_phase < 0.50F) {
  443. hand_l_target = draw_pos;
  444. shoulder_twist = 0.08F;
  445. } else if (draw_phase < 0.58F) {
  446. float t = (draw_phase - 0.50F) / 0.08F;
  447. t = t * t * t;
  448. hand_l_target = draw_pos * (1.0F - t) + release_pos * t;
  449. shoulder_twist = 0.08F * (1.0F - t * 0.6F);
  450. head_recoil = t * 0.04F;
  451. } else {
  452. float t = (draw_phase - 0.58F) / 0.42F;
  453. t = 1.0F - (1.0F - t) * (1.0F - t);
  454. hand_l_target = release_pos * (1.0F - t) + aim_pos * t;
  455. shoulder_twist = 0.08F * 0.4F * (1.0F - t);
  456. head_recoil = 0.04F * (1.0F - t);
  457. }
  458. QVector3D const hand_r_target(0.03F, HP::SHOULDER_Y + 0.08F, 0.55F);
  459. place_hand_at(false, hand_r_target);
  460. place_hand_at(true, hand_l_target);
  461. if (shoulder_twist > 0.01F) {
  462. m_pose.shoulder_l.setY(m_pose.shoulder_l.y() + shoulder_twist);
  463. m_pose.shoulder_r.setY(m_pose.shoulder_r.y() - shoulder_twist * 0.5F);
  464. }
  465. if (head_recoil > 0.01F) {
  466. m_pose.head_pos.setZ(m_pose.head_pos.z() - head_recoil);
  467. }
  468. }
  469. void HumanoidPoseController::melee_strike(float strike_phase) {
  470. using HP = HumanProportions;
  471. strike_phase = std::clamp(strike_phase, 0.0F, 1.0F);
  472. QVector3D const rest_pos(0.22F, HP::SHOULDER_Y + 0.02F, 0.18F);
  473. QVector3D const chamber_pos(0.30F, HP::SHOULDER_Y + 0.08F, 0.05F);
  474. QVector3D const strike_pos(0.28F, HP::SHOULDER_Y - 0.05F, 0.65F);
  475. QVector3D const followthrough_pos(0.10F, HP::SHOULDER_Y - 0.12F, 0.55F);
  476. QVector3D hand_r_target;
  477. QVector3D hand_l_target;
  478. float torso_twist = 0.0F;
  479. float forward_lean = 0.0F;
  480. float shoulder_dip = 0.0F;
  481. float step_forward = 0.0F;
  482. if (strike_phase < 0.20F) {
  483. float t = strike_phase / 0.20F;
  484. float ease_t = t * t;
  485. hand_r_target = rest_pos * (1.0F - ease_t) + chamber_pos * ease_t;
  486. hand_l_target =
  487. QVector3D(-0.18F, HP::SHOULDER_Y + 0.02F, 0.22F - 0.08F * t);
  488. torso_twist = -0.04F * ease_t;
  489. shoulder_dip = -0.02F * ease_t;
  490. } else if (strike_phase < 0.28F) {
  491. hand_r_target = chamber_pos;
  492. hand_l_target = QVector3D(-0.18F, HP::SHOULDER_Y + 0.02F, 0.14F);
  493. torso_twist = -0.04F;
  494. shoulder_dip = -0.02F;
  495. } else if (strike_phase < 0.48F) {
  496. float t = (strike_phase - 0.28F) / 0.20F;
  497. float power_t = t * t * (3.0F - 2.0F * t);
  498. hand_r_target = chamber_pos * (1.0F - power_t) + strike_pos * power_t;
  499. hand_l_target = QVector3D(-0.18F + 0.06F * power_t,
  500. HP::SHOULDER_Y + 0.02F - 0.08F * power_t,
  501. 0.14F + 0.20F * power_t);
  502. torso_twist = -0.04F + 0.10F * power_t;
  503. forward_lean = 0.08F * power_t;
  504. shoulder_dip = -0.02F + 0.05F * power_t;
  505. step_forward = 0.06F * power_t;
  506. } else if (strike_phase < 0.65F) {
  507. float t = (strike_phase - 0.48F) / 0.17F;
  508. float ease_t = t * t;
  509. hand_r_target = strike_pos * (1.0F - ease_t) + followthrough_pos * ease_t;
  510. hand_l_target = QVector3D(-0.12F, HP::SHOULDER_Y - 0.06F, 0.34F);
  511. torso_twist = 0.06F - 0.02F * t;
  512. forward_lean = 0.08F - 0.03F * t;
  513. shoulder_dip = 0.03F;
  514. step_forward = 0.06F;
  515. } else {
  516. float t = (strike_phase - 0.65F) / 0.35F;
  517. float ease_t = 1.0F - (1.0F - t) * (1.0F - t);
  518. hand_r_target = followthrough_pos * (1.0F - ease_t) + rest_pos * ease_t;
  519. hand_l_target =
  520. QVector3D(-0.12F + (-0.18F + 0.12F) * ease_t,
  521. HP::SHOULDER_Y - 0.06F * (1.0F - ease_t) + 0.02F * ease_t,
  522. 0.34F * (1.0F - ease_t) + 0.22F * ease_t);
  523. torso_twist = 0.04F * (1.0F - ease_t);
  524. forward_lean = 0.05F * (1.0F - ease_t);
  525. shoulder_dip = 0.03F * (1.0F - ease_t);
  526. step_forward = 0.06F * (1.0F - ease_t);
  527. }
  528. if (std::abs(torso_twist) > 0.001F) {
  529. float const twist = torso_twist * 0.0F;
  530. m_pose.shoulder_r.setZ(m_pose.shoulder_r.z() + twist);
  531. m_pose.shoulder_l.setZ(m_pose.shoulder_l.z() - twist * 0.5F);
  532. }
  533. if (forward_lean > 0.001F) {
  534. m_pose.shoulder_l.setZ(m_pose.shoulder_l.z() + forward_lean);
  535. m_pose.shoulder_r.setZ(m_pose.shoulder_r.z() + forward_lean);
  536. m_pose.neck_base.setZ(m_pose.neck_base.z() + forward_lean * 0.8F);
  537. m_pose.head_pos.setZ(m_pose.head_pos.z() + forward_lean * 0.6F);
  538. }
  539. if (std::abs(shoulder_dip) > 0.001F) {
  540. m_pose.shoulder_r.setY(m_pose.shoulder_r.y() + shoulder_dip);
  541. }
  542. if (step_forward > 0.001F) {
  543. m_pose.foot_r.setZ(m_pose.foot_r.z() + step_forward);
  544. m_pose.knee_r.setZ(m_pose.knee_r.z() + step_forward * 0.5F);
  545. }
  546. place_hand_at(false, hand_r_target);
  547. place_hand_at(true, hand_l_target);
  548. }
  549. void HumanoidPoseController::grasp_two_handed(const QVector3D &grip_center,
  550. float hand_separation) {
  551. hand_separation = std::clamp(hand_separation, 0.1F, 0.8F);
  552. QVector3D const right_axis = compute_right_axis();
  553. QVector3D const right_hand_pos =
  554. grip_center + right_axis * (hand_separation * 0.5F);
  555. QVector3D const left_hand_pos =
  556. grip_center - right_axis * (hand_separation * 0.5F);
  557. place_hand_at(false, right_hand_pos);
  558. place_hand_at(true, left_hand_pos);
  559. }
  560. void HumanoidPoseController::spear_thrust(float attack_phase) {
  561. using HP = HumanProportions;
  562. attack_phase = std::clamp(attack_phase, 0.0F, 1.0F);
  563. QVector3D const guard_pos(0.26F, HP::SHOULDER_Y + 0.08F, 0.28F);
  564. QVector3D const chamber_pos(0.32F, HP::SHOULDER_Y + 0.12F, 0.02F);
  565. QVector3D const thrust_pos(0.28F, HP::SHOULDER_Y + 0.05F, 0.95F);
  566. QVector3D const extended_pos(0.25F, HP::SHOULDER_Y + 0.02F, 1.05F);
  567. QVector3D const recover_pos(0.28F, HP::SHOULDER_Y + 0.06F, 0.45F);
  568. QVector3D hand_r_target;
  569. QVector3D hand_l_target;
  570. float forward_lean = 0.0F;
  571. float torso_twist = 0.0F;
  572. float shoulder_drop = 0.0F;
  573. float step_forward = 0.0F;
  574. float hip_rotation = 0.0F;
  575. auto ease_in_out_cubic = [](float t) {
  576. return t < 0.5F ? 4.0F * t * t * t
  577. : 1.0F - std::pow(-2.0F * t + 2.0F, 3.0F) / 2.0F;
  578. };
  579. auto smoothstep = [](float t) { return t * t * (3.0F - 2.0F * t); };
  580. auto ease_out = [](float t) { return 1.0F - (1.0F - t) * (1.0F - t); };
  581. if (attack_phase < 0.18F) {
  582. float const t = ease_in_out_cubic(attack_phase / 0.18F);
  583. hand_r_target = guard_pos * (1.0F - t) + chamber_pos * t;
  584. hand_l_target = QVector3D(-0.08F, HP::SHOULDER_Y - 0.04F,
  585. 0.22F * (1.0F - t) + 0.06F * t);
  586. torso_twist = -0.06F * t;
  587. hip_rotation = -0.04F * t;
  588. forward_lean = -0.03F * t;
  589. } else if (attack_phase < 0.28F) {
  590. float const t = (attack_phase - 0.18F) / 0.10F;
  591. hand_r_target = chamber_pos;
  592. hand_l_target = QVector3D(-0.08F, HP::SHOULDER_Y - 0.04F, 0.06F);
  593. torso_twist = -0.06F;
  594. hip_rotation = -0.04F;
  595. forward_lean = -0.03F - 0.02F * t;
  596. } else if (attack_phase < 0.48F) {
  597. float t = (attack_phase - 0.28F) / 0.20F;
  598. float power_t = t * t * t;
  599. hand_r_target = chamber_pos * (1.0F - power_t) + thrust_pos * power_t;
  600. hand_l_target = QVector3D(-0.08F + 0.06F * power_t,
  601. HP::SHOULDER_Y - 0.04F + 0.02F * power_t,
  602. 0.06F + 0.50F * power_t);
  603. torso_twist = -0.06F + 0.14F * power_t;
  604. hip_rotation = -0.04F + 0.10F * power_t;
  605. forward_lean = -0.05F + 0.18F * power_t;
  606. shoulder_drop = 0.05F * power_t;
  607. step_forward = 0.10F * power_t;
  608. } else if (attack_phase < 0.60F) {
  609. float const t = smoothstep((attack_phase - 0.48F) / 0.12F);
  610. hand_r_target = thrust_pos * (1.0F - t) + extended_pos * t;
  611. hand_l_target =
  612. QVector3D(-0.02F, HP::SHOULDER_Y - 0.02F, 0.56F + 0.10F * t);
  613. torso_twist = 0.08F;
  614. hip_rotation = 0.06F;
  615. forward_lean = 0.13F + 0.05F * t;
  616. shoulder_drop = 0.05F + 0.02F * t;
  617. step_forward = 0.10F + 0.04F * t;
  618. } else if (attack_phase < 0.78F) {
  619. float const t = ease_in_out_cubic((attack_phase - 0.60F) / 0.18F);
  620. hand_r_target = extended_pos * (1.0F - t) + recover_pos * t;
  621. hand_l_target = QVector3D(-0.02F * (1.0F - t) - 0.08F * t,
  622. HP::SHOULDER_Y - 0.02F * (1.0F - t) - 0.05F * t,
  623. 0.66F * (1.0F - t) + 0.38F * t);
  624. torso_twist = 0.08F * (1.0F - t);
  625. hip_rotation = 0.06F * (1.0F - t);
  626. forward_lean = 0.18F * (1.0F - t) + 0.04F * t;
  627. shoulder_drop = 0.07F * (1.0F - t);
  628. step_forward = 0.14F * (1.0F - t * 0.5F);
  629. } else {
  630. float const t = ease_out((attack_phase - 0.78F) / 0.22F);
  631. hand_r_target = recover_pos * (1.0F - t) + guard_pos * t;
  632. hand_l_target =
  633. QVector3D(-0.08F, HP::SHOULDER_Y - 0.05F * (1.0F - t) - 0.02F * t,
  634. 0.38F * (1.0F - t) + 0.22F * t);
  635. forward_lean = 0.04F * (1.0F - t);
  636. step_forward = 0.07F * (1.0F - t);
  637. }
  638. if (std::abs(torso_twist) > 0.001F) {
  639. float const twist = torso_twist * 0.0F;
  640. m_pose.shoulder_r.setZ(m_pose.shoulder_r.z() + twist);
  641. m_pose.shoulder_l.setZ(m_pose.shoulder_l.z() - twist * 0.4F);
  642. }
  643. if (std::abs(hip_rotation) > 0.001F) {
  644. m_pose.pelvis_pos.setZ(m_pose.pelvis_pos.z() + hip_rotation * 0.5F);
  645. }
  646. if (std::abs(forward_lean) > 0.001F) {
  647. m_pose.shoulder_l.setZ(m_pose.shoulder_l.z() + forward_lean);
  648. m_pose.shoulder_r.setZ(m_pose.shoulder_r.z() + forward_lean);
  649. m_pose.neck_base.setZ(m_pose.neck_base.z() + forward_lean * 0.85F);
  650. m_pose.head_pos.setZ(m_pose.head_pos.z() + forward_lean * 0.7F);
  651. }
  652. if (shoulder_drop > 0.001F) {
  653. m_pose.shoulder_r.setY(m_pose.shoulder_r.y() - shoulder_drop);
  654. m_pose.shoulder_l.setY(m_pose.shoulder_l.y() - shoulder_drop * 0.3F);
  655. }
  656. if (step_forward > 0.001F) {
  657. m_pose.foot_r.setZ(m_pose.foot_r.z() + step_forward);
  658. m_pose.knee_r.setZ(m_pose.knee_r.z() + step_forward * 0.6F);
  659. m_pose.foot_l.setZ(m_pose.foot_l.z() - step_forward * 0.15F);
  660. }
  661. float const thrust_extent =
  662. std::clamp((attack_phase - 0.18F) / 0.60F, 0.0F, 1.0F);
  663. float const along_offset = -0.08F + 0.04F * thrust_extent;
  664. float const y_drop = 0.08F + 0.03F * thrust_extent;
  665. hand_l_target = compute_offhand_spear_grip(
  666. m_pose, m_anim_ctx, hand_r_target, false, along_offset, y_drop, -0.06F);
  667. place_hand_at(false, hand_r_target);
  668. place_hand_at(true, hand_l_target);
  669. }
  670. void HumanoidPoseController::spear_thrust_from_hold(float attack_phase,
  671. float hold_depth) {
  672. using HP = HumanProportions;
  673. attack_phase = std::clamp(attack_phase, 0.0F, 1.0F);
  674. hold_depth = std::clamp(hold_depth, 0.0F, 1.0F);
  675. float const height_offset = -hold_depth * 0.35F;
  676. QVector3D const guard_pos(0.22F, HP::SHOULDER_Y + height_offset + 0.05F,
  677. 0.32F);
  678. QVector3D const chamber_pos(0.28F, HP::SHOULDER_Y + height_offset + 0.10F,
  679. 0.08F);
  680. QVector3D const thrust_pos(0.24F, HP::SHOULDER_Y + height_offset - 0.08F,
  681. 0.90F);
  682. QVector3D const extended_pos(0.22F, HP::SHOULDER_Y + height_offset - 0.12F,
  683. 1.00F);
  684. QVector3D const recover_pos(0.24F, HP::SHOULDER_Y + height_offset + 0.02F,
  685. 0.48F);
  686. QVector3D hand_r_target;
  687. QVector3D hand_l_target;
  688. float forward_lean = 0.0F;
  689. float torso_twist = 0.0F;
  690. float shoulder_extension = 0.0F;
  691. auto smoothstep = [](float t) { return t * t * (3.0F - 2.0F * t); };
  692. auto ease_out = [](float t) { return 1.0F - (1.0F - t) * (1.0F - t); };
  693. auto ease_in = [](float t) { return t * t; };
  694. if (attack_phase < 0.15F) {
  695. float const t = ease_in(attack_phase / 0.15F);
  696. hand_r_target = guard_pos * (1.0F - t) + chamber_pos * t;
  697. hand_l_target = QVector3D(-0.06F, HP::SHOULDER_Y + height_offset - 0.03F,
  698. 0.28F * (1.0F - t) + 0.10F * t);
  699. torso_twist = -0.04F * t;
  700. } else if (attack_phase < 0.22F) {
  701. hand_r_target = chamber_pos;
  702. hand_l_target =
  703. QVector3D(-0.06F, HP::SHOULDER_Y + height_offset - 0.03F, 0.10F);
  704. torso_twist = -0.04F;
  705. } else if (attack_phase < 0.42F) {
  706. float t = (attack_phase - 0.22F) / 0.20F;
  707. float power_t = t * t * t;
  708. hand_r_target = chamber_pos * (1.0F - power_t) + thrust_pos * power_t;
  709. hand_l_target =
  710. QVector3D(-0.06F + 0.05F * power_t,
  711. HP::SHOULDER_Y + height_offset - 0.03F + 0.01F * power_t,
  712. 0.10F + 0.48F * power_t);
  713. torso_twist = -0.04F + 0.10F * power_t;
  714. forward_lean = 0.12F * power_t;
  715. shoulder_extension = 0.06F * power_t;
  716. } else if (attack_phase < 0.55F) {
  717. float const t = smoothstep((attack_phase - 0.42F) / 0.13F);
  718. hand_r_target = thrust_pos * (1.0F - t) + extended_pos * t;
  719. hand_l_target = QVector3D(-0.01F, HP::SHOULDER_Y + height_offset - 0.02F,
  720. 0.58F + 0.08F * t);
  721. torso_twist = 0.06F;
  722. forward_lean = 0.12F + 0.04F * t;
  723. shoulder_extension = 0.06F + 0.03F * t;
  724. } else if (attack_phase < 0.75F) {
  725. float const t = smoothstep((attack_phase - 0.55F) / 0.20F);
  726. hand_r_target = extended_pos * (1.0F - t) + recover_pos * t;
  727. hand_l_target = QVector3D(-0.01F * (1.0F - t) - 0.05F * t,
  728. HP::SHOULDER_Y + height_offset -
  729. 0.02F * (1.0F - t) - 0.04F * t,
  730. 0.66F * (1.0F - t) + 0.40F * t);
  731. torso_twist = 0.06F * (1.0F - t);
  732. forward_lean = 0.16F * (1.0F - t) + 0.03F * t;
  733. shoulder_extension = 0.09F * (1.0F - t);
  734. } else {
  735. float const t = ease_out((attack_phase - 0.75F) / 0.25F);
  736. hand_r_target = recover_pos * (1.0F - t) + guard_pos * t;
  737. hand_l_target = QVector3D(-0.05F - 0.01F * t,
  738. HP::SHOULDER_Y + height_offset -
  739. 0.04F * (1.0F - t) - 0.03F * t,
  740. 0.40F * (1.0F - t) + 0.28F * t);
  741. forward_lean = 0.03F * (1.0F - t);
  742. }
  743. if (std::abs(torso_twist) > 0.001F) {
  744. float const twist = torso_twist * 0.0F;
  745. m_pose.shoulder_r.setZ(m_pose.shoulder_r.z() + twist);
  746. m_pose.shoulder_l.setZ(m_pose.shoulder_l.z() - twist * 0.3F);
  747. }
  748. if (forward_lean > 0.001F) {
  749. m_pose.shoulder_l.setZ(m_pose.shoulder_l.z() + forward_lean);
  750. m_pose.shoulder_r.setZ(m_pose.shoulder_r.z() + forward_lean);
  751. m_pose.neck_base.setZ(m_pose.neck_base.z() + forward_lean * 0.9F);
  752. m_pose.head_pos.setZ(m_pose.head_pos.z() + forward_lean * 0.75F);
  753. }
  754. if (shoulder_extension > 0.001F) {
  755. m_pose.shoulder_r.setZ(m_pose.shoulder_r.z() + shoulder_extension);
  756. m_pose.shoulder_r.setY(m_pose.shoulder_r.y() - shoulder_extension * 0.3F);
  757. }
  758. float const thrust_extent =
  759. std::clamp((attack_phase - 0.15F) / 0.55F, 0.0F, 1.0F);
  760. float const along_offset = -0.06F + 0.03F * thrust_extent;
  761. float const y_drop = 0.06F + 0.02F * thrust_extent;
  762. hand_l_target = compute_offhand_spear_grip(
  763. m_pose, m_anim_ctx, hand_r_target, false, along_offset, y_drop, -0.05F);
  764. place_hand_at(false, hand_r_target);
  765. place_hand_at(true, hand_l_target);
  766. }
  767. void HumanoidPoseController::sword_slash(float attack_phase) {
  768. using HP = HumanProportions;
  769. attack_phase = std::clamp(attack_phase, 0.0F, 1.0F);
  770. QVector3D const rest_pos(0.20F, HP::SHOULDER_Y + 0.05F, 0.15F);
  771. QVector3D const chamber_pos(0.28F, HP::SHOULDER_Y + 0.20F, 0.02F);
  772. QVector3D const apex_pos(0.30F, HP::SHOULDER_Y + 0.25F, 0.08F);
  773. QVector3D const strike_pos(0.18F, HP::SHOULDER_Y - 0.15F, 0.62F);
  774. QVector3D const followthrough_pos(0.05F, HP::WAIST_Y + 0.10F, 0.50F);
  775. QVector3D const recover_pos(0.22F, HP::SHOULDER_Y + 0.02F, 0.22F);
  776. QVector3D hand_r_target;
  777. QVector3D hand_l_target;
  778. float torso_twist = 0.0F;
  779. float forward_lean = 0.0F;
  780. float shoulder_rotation = 0.0F;
  781. float weight_shift = 0.0F;
  782. auto smoothstep = [](float t) { return t * t * (3.0F - 2.0F * t); };
  783. auto ease_out = [](float t) { return 1.0F - (1.0F - t) * (1.0F - t); };
  784. auto ease_in = [](float t) { return t * t; };
  785. if (attack_phase < 0.15F) {
  786. float t = attack_phase / 0.15F;
  787. float ease_t = ease_in(t);
  788. hand_r_target = rest_pos * (1.0F - ease_t) + chamber_pos * ease_t;
  789. hand_l_target =
  790. QVector3D(-0.20F, HP::SHOULDER_Y - 0.02F, 0.15F + 0.02F * t);
  791. torso_twist = -0.05F * ease_t;
  792. shoulder_rotation = 0.03F * ease_t;
  793. } else if (attack_phase < 0.28F) {
  794. float t = (attack_phase - 0.15F) / 0.13F;
  795. float ease_t = smoothstep(t);
  796. hand_r_target = chamber_pos * (1.0F - ease_t) + apex_pos * ease_t;
  797. hand_l_target = QVector3D(-0.20F, HP::SHOULDER_Y - 0.04F, 0.17F);
  798. torso_twist = -0.05F;
  799. shoulder_rotation = 0.03F + 0.02F * ease_t;
  800. weight_shift = -0.02F * ease_t;
  801. } else if (attack_phase < 0.48F) {
  802. float t = (attack_phase - 0.28F) / 0.20F;
  803. float power_t = t * t * t;
  804. hand_r_target = apex_pos * (1.0F - power_t) + strike_pos * power_t;
  805. hand_l_target = QVector3D(-0.20F + 0.08F * power_t,
  806. HP::SHOULDER_Y - 0.04F - 0.06F * power_t,
  807. 0.17F + 0.22F * power_t);
  808. torso_twist = -0.05F + 0.14F * power_t;
  809. forward_lean = 0.10F * power_t;
  810. shoulder_rotation = 0.05F - 0.08F * power_t;
  811. weight_shift = -0.02F + 0.08F * power_t;
  812. } else if (attack_phase < 0.62F) {
  813. float t = (attack_phase - 0.48F) / 0.14F;
  814. float ease_t = smoothstep(t);
  815. hand_r_target = strike_pos * (1.0F - ease_t) + followthrough_pos * ease_t;
  816. hand_l_target = QVector3D(-0.12F, HP::SHOULDER_Y - 0.10F, 0.39F);
  817. torso_twist = 0.09F - 0.03F * t;
  818. forward_lean = 0.10F - 0.02F * t;
  819. weight_shift = 0.06F;
  820. } else {
  821. float t = (attack_phase - 0.62F) / 0.38F;
  822. float ease_t = ease_out(t);
  823. hand_r_target = followthrough_pos * (1.0F - ease_t) +
  824. recover_pos * 0.5F * ease_t + rest_pos * 0.5F * ease_t;
  825. hand_l_target = QVector3D(-0.12F - 0.08F * ease_t,
  826. HP::SHOULDER_Y - 0.10F * (1.0F - ease_t),
  827. 0.39F * (1.0F - ease_t) + 0.15F * ease_t);
  828. torso_twist = 0.06F * (1.0F - ease_t);
  829. forward_lean = 0.08F * (1.0F - ease_t);
  830. weight_shift = 0.06F * (1.0F - ease_t);
  831. }
  832. if (std::abs(torso_twist) > 0.001F) {
  833. float const twist = torso_twist * 0.0F;
  834. m_pose.shoulder_r.setZ(m_pose.shoulder_r.z() + twist);
  835. m_pose.shoulder_l.setZ(m_pose.shoulder_l.z() - twist * 0.6F);
  836. }
  837. if (std::abs(shoulder_rotation) > 0.001F) {
  838. m_pose.shoulder_r.setY(m_pose.shoulder_r.y() - shoulder_rotation);
  839. m_pose.shoulder_l.setY(m_pose.shoulder_l.y() + shoulder_rotation * 0.4F);
  840. }
  841. if (forward_lean > 0.001F) {
  842. m_pose.shoulder_l.setZ(m_pose.shoulder_l.z() + forward_lean);
  843. m_pose.shoulder_r.setZ(m_pose.shoulder_r.z() + forward_lean);
  844. m_pose.neck_base.setZ(m_pose.neck_base.z() + forward_lean * 0.7F);
  845. m_pose.head_pos.setZ(m_pose.head_pos.z() + forward_lean * 0.5F);
  846. m_pose.pelvis_pos.setZ(m_pose.pelvis_pos.z() + forward_lean * 0.3F);
  847. }
  848. if (std::abs(weight_shift) > 0.001F) {
  849. m_pose.foot_r.setZ(m_pose.foot_r.z() + weight_shift);
  850. m_pose.knee_r.setZ(m_pose.knee_r.z() + weight_shift * 0.6F);
  851. }
  852. place_hand_at(false, hand_r_target);
  853. place_hand_at(true, hand_l_target);
  854. }
  855. void HumanoidPoseController::mount_on_horse(float saddle_height) {
  856. float const offset_y = saddle_height - m_pose.pelvis_pos.y();
  857. m_pose.pelvis_pos.setY(saddle_height);
  858. }
  859. void HumanoidPoseController::hold_sword_and_shield() {
  860. using HP = HumanProportions;
  861. QVector3D const sword_hand_pos(0.30F, HP::SHOULDER_Y - 0.02F, 0.35F);
  862. QVector3D const shield_hand_pos(-0.22F, HP::SHOULDER_Y, 0.18F);
  863. place_hand_at(false, sword_hand_pos);
  864. place_hand_at(true, shield_hand_pos);
  865. }
  866. void HumanoidPoseController::look_at(const QVector3D &target) {
  867. QVector3D const head_to_target = target - m_pose.head_pos;
  868. if (head_to_target.lengthSquared() < 1e-6F) {
  869. return;
  870. }
  871. QVector3D const direction = head_to_target.normalized();
  872. float const max_head_turn = 0.03F;
  873. QVector3D const head_offset = direction * max_head_turn;
  874. m_pose.head_pos += QVector3D(head_offset.x(), 0.0F, head_offset.z());
  875. float const neck_follow = 0.5F;
  876. m_pose.neck_base += QVector3D(head_offset.x() * neck_follow, 0.0F,
  877. head_offset.z() * neck_follow);
  878. }
  879. void HumanoidPoseController::hit_flinch(float intensity) {
  880. intensity = std::clamp(intensity, 0.0F, 1.0F);
  881. if (intensity < 0.01F) {
  882. return;
  883. }
  884. float const flinch_back = intensity * 0.06F;
  885. float const flinch_down = intensity * 0.04F;
  886. float const shoulder_drop = intensity * 0.03F;
  887. m_pose.head_pos.setZ(m_pose.head_pos.z() - flinch_back);
  888. m_pose.head_pos.setY(m_pose.head_pos.y() - flinch_down * 0.5F);
  889. m_pose.neck_base.setZ(m_pose.neck_base.z() - flinch_back * 0.8F);
  890. m_pose.shoulder_l.setY(m_pose.shoulder_l.y() - shoulder_drop);
  891. m_pose.shoulder_r.setY(m_pose.shoulder_r.y() - shoulder_drop);
  892. m_pose.shoulder_l.setZ(m_pose.shoulder_l.z() - flinch_back * 0.6F);
  893. m_pose.shoulder_r.setZ(m_pose.shoulder_r.z() - flinch_back * 0.6F);
  894. m_pose.pelvis_pos.setY(m_pose.pelvis_pos.y() - flinch_down * 0.3F);
  895. }
  896. void HumanoidPoseController::sword_slash_variant(float attack_phase,
  897. std::uint8_t variant) {
  898. using HP = HumanProportions;
  899. attack_phase = std::clamp(attack_phase, 0.0F, 1.0F);
  900. constexpr float k_strike_right_to_left = 1.0F;
  901. constexpr float k_strike_left_to_right = -1.0F;
  902. QVector3D rest_pos(0.20F, HP::SHOULDER_Y + 0.05F, 0.15F);
  903. QVector3D chamber_pos(0.28F, HP::SHOULDER_Y + 0.20F, 0.02F);
  904. QVector3D apex_pos(0.30F, HP::SHOULDER_Y + 0.25F, 0.08F);
  905. QVector3D strike_pos(0.18F, HP::SHOULDER_Y - 0.15F, 0.62F);
  906. QVector3D followthrough_pos(0.05F, HP::WAIST_Y + 0.10F, 0.50F);
  907. float strike_direction = k_strike_right_to_left;
  908. switch (variant % 3) {
  909. case 1:
  910. chamber_pos = QVector3D(-0.10F, HP::SHOULDER_Y + 0.22F, 0.04F);
  911. apex_pos = QVector3D(-0.08F, HP::SHOULDER_Y + 0.28F, 0.10F);
  912. strike_pos = QVector3D(0.32F, HP::SHOULDER_Y - 0.12F, 0.58F);
  913. followthrough_pos = QVector3D(0.40F, HP::WAIST_Y + 0.08F, 0.48F);
  914. strike_direction = k_strike_left_to_right;
  915. break;
  916. case 2:
  917. chamber_pos = QVector3D(0.35F, HP::SHOULDER_Y + 0.10F, 0.0F);
  918. apex_pos = QVector3D(0.38F, HP::SHOULDER_Y + 0.08F, 0.06F);
  919. strike_pos = QVector3D(0.05F, HP::SHOULDER_Y - 0.05F, 0.65F);
  920. followthrough_pos = QVector3D(-0.10F, HP::SHOULDER_Y - 0.10F, 0.55F);
  921. break;
  922. default:
  923. break;
  924. }
  925. QVector3D hand_r_target;
  926. QVector3D hand_l_target;
  927. float torso_twist = 0.0F;
  928. float forward_lean = 0.0F;
  929. float shoulder_rotation = 0.0F;
  930. float weight_shift = 0.0F;
  931. auto smoothstep = [](float t) { return t * t * (3.0F - 2.0F * t); };
  932. auto ease_out = [](float t) { return 1.0F - (1.0F - t) * (1.0F - t); };
  933. if (attack_phase < 0.15F) {
  934. float t = attack_phase / 0.15F;
  935. float ease_t = t * t;
  936. hand_r_target = rest_pos * (1.0F - ease_t) + chamber_pos * ease_t;
  937. hand_l_target = QVector3D(-0.20F, HP::SHOULDER_Y - 0.02F, 0.15F);
  938. torso_twist = strike_direction * (-0.05F * ease_t);
  939. shoulder_rotation = 0.03F * ease_t;
  940. } else if (attack_phase < 0.28F) {
  941. float t = (attack_phase - 0.15F) / 0.13F;
  942. float ease_t = smoothstep(t);
  943. hand_r_target = chamber_pos * (1.0F - ease_t) + apex_pos * ease_t;
  944. hand_l_target = QVector3D(-0.20F, HP::SHOULDER_Y - 0.04F, 0.17F);
  945. torso_twist = strike_direction * (-0.05F);
  946. shoulder_rotation = 0.03F + 0.02F * ease_t;
  947. weight_shift = -0.02F * ease_t;
  948. } else if (attack_phase < 0.48F) {
  949. float t = (attack_phase - 0.28F) / 0.20F;
  950. float power_t = t * t * t;
  951. hand_r_target = apex_pos * (1.0F - power_t) + strike_pos * power_t;
  952. hand_l_target = QVector3D(-0.20F + 0.08F * power_t,
  953. HP::SHOULDER_Y - 0.04F - 0.06F * power_t,
  954. 0.17F + 0.22F * power_t);
  955. torso_twist = strike_direction * (-0.05F + 0.14F * power_t);
  956. forward_lean = 0.10F * power_t;
  957. shoulder_rotation = 0.05F - 0.08F * power_t;
  958. weight_shift = -0.02F + 0.08F * power_t;
  959. } else if (attack_phase < 0.62F) {
  960. float t = (attack_phase - 0.48F) / 0.14F;
  961. float ease_t = smoothstep(t);
  962. hand_r_target = strike_pos * (1.0F - ease_t) + followthrough_pos * ease_t;
  963. hand_l_target = QVector3D(-0.12F, HP::SHOULDER_Y - 0.10F, 0.39F);
  964. torso_twist = strike_direction * (0.09F - 0.03F * t);
  965. forward_lean = 0.10F - 0.02F * t;
  966. weight_shift = 0.06F;
  967. } else {
  968. float t = (attack_phase - 0.62F) / 0.38F;
  969. float ease_t = ease_out(t);
  970. hand_r_target = followthrough_pos * (1.0F - ease_t) + rest_pos * ease_t;
  971. hand_l_target = QVector3D(-0.12F - 0.08F * ease_t,
  972. HP::SHOULDER_Y - 0.10F * (1.0F - ease_t),
  973. 0.39F * (1.0F - ease_t) + 0.15F * ease_t);
  974. torso_twist = 0.06F * strike_direction * (1.0F - ease_t);
  975. forward_lean = 0.08F * (1.0F - ease_t);
  976. weight_shift = 0.06F * (1.0F - ease_t);
  977. }
  978. if (std::abs(torso_twist) > 0.001F) {
  979. float const twist = torso_twist * 0.0F;
  980. m_pose.shoulder_r.setZ(m_pose.shoulder_r.z() + twist);
  981. m_pose.shoulder_l.setZ(m_pose.shoulder_l.z() - twist * 0.6F);
  982. }
  983. if (std::abs(shoulder_rotation) > 0.001F) {
  984. m_pose.shoulder_r.setY(m_pose.shoulder_r.y() - shoulder_rotation);
  985. m_pose.shoulder_l.setY(m_pose.shoulder_l.y() + shoulder_rotation * 0.4F);
  986. }
  987. if (forward_lean > 0.001F) {
  988. m_pose.shoulder_l.setZ(m_pose.shoulder_l.z() + forward_lean);
  989. m_pose.shoulder_r.setZ(m_pose.shoulder_r.z() + forward_lean);
  990. m_pose.neck_base.setZ(m_pose.neck_base.z() + forward_lean * 0.7F);
  991. m_pose.head_pos.setZ(m_pose.head_pos.z() + forward_lean * 0.5F);
  992. }
  993. if (std::abs(weight_shift) > 0.001F) {
  994. m_pose.foot_r.setZ(m_pose.foot_r.z() + weight_shift);
  995. m_pose.knee_r.setZ(m_pose.knee_r.z() + weight_shift * 0.6F);
  996. }
  997. place_hand_at(false, hand_r_target);
  998. place_hand_at(true, hand_l_target);
  999. }
  1000. void HumanoidPoseController::spear_thrust_variant(float attack_phase,
  1001. std::uint8_t variant) {
  1002. using HP = HumanProportions;
  1003. attack_phase = std::clamp(attack_phase, 0.0F, 1.0F);
  1004. constexpr float k_thrust_high = 1.0F;
  1005. constexpr float k_thrust_middle = 0.0F;
  1006. constexpr float k_thrust_low = -1.0F;
  1007. QVector3D guard_pos(0.26F, HP::SHOULDER_Y + 0.08F, 0.28F);
  1008. QVector3D chamber_pos(0.32F, HP::SHOULDER_Y + 0.12F, 0.02F);
  1009. QVector3D thrust_pos(0.28F, HP::SHOULDER_Y + 0.05F, 0.95F);
  1010. QVector3D extended_pos(0.25F, HP::SHOULDER_Y + 0.02F, 1.05F);
  1011. QVector3D recover_pos(0.28F, HP::SHOULDER_Y + 0.06F, 0.45F);
  1012. float thrust_height = k_thrust_middle;
  1013. float crouch_amount = 0.0F;
  1014. switch (variant % 3) {
  1015. case 1:
  1016. chamber_pos = QVector3D(0.30F, HP::SHOULDER_Y + 0.18F, 0.0F);
  1017. thrust_pos = QVector3D(0.28F, HP::WAIST_Y + 0.15F, 0.98F);
  1018. extended_pos = QVector3D(0.25F, HP::WAIST_Y + 0.10F, 1.08F);
  1019. recover_pos = QVector3D(0.28F, HP::SHOULDER_Y - 0.05F, 0.42F);
  1020. thrust_height = k_thrust_low;
  1021. crouch_amount = 0.08F;
  1022. break;
  1023. case 2:
  1024. chamber_pos = QVector3D(0.35F, HP::SHOULDER_Y + 0.05F, 0.08F);
  1025. thrust_pos = QVector3D(0.30F, HP::SHOULDER_Y + 0.12F, 0.92F);
  1026. extended_pos = QVector3D(0.28F, HP::SHOULDER_Y + 0.15F, 1.02F);
  1027. thrust_height = k_thrust_high;
  1028. break;
  1029. default:
  1030. break;
  1031. }
  1032. QVector3D hand_r_target;
  1033. QVector3D hand_l_target;
  1034. float forward_lean = 0.0F;
  1035. float torso_twist = 0.0F;
  1036. float shoulder_drop = 0.0F;
  1037. float step_forward = 0.0F;
  1038. float hip_rotation = 0.0F;
  1039. float crouch_factor = 0.0F;
  1040. auto ease_in_out_cubic = [](float t) {
  1041. return t < 0.5F ? 4.0F * t * t * t
  1042. : 1.0F - std::pow(-2.0F * t + 2.0F, 3.0F) / 2.0F;
  1043. };
  1044. auto smoothstep = [](float t) { return t * t * (3.0F - 2.0F * t); };
  1045. auto ease_out = [](float t) { return 1.0F - (1.0F - t) * (1.0F - t); };
  1046. if (attack_phase < 0.18F) {
  1047. float const t = ease_in_out_cubic(attack_phase / 0.18F);
  1048. hand_r_target = guard_pos * (1.0F - t) + chamber_pos * t;
  1049. hand_l_target = QVector3D(-0.08F, HP::SHOULDER_Y - 0.04F,
  1050. 0.22F * (1.0F - t) + 0.06F * t);
  1051. torso_twist = -0.06F * t;
  1052. hip_rotation = -0.04F * t;
  1053. forward_lean = -0.03F * t;
  1054. crouch_factor = crouch_amount * t;
  1055. } else if (attack_phase < 0.28F) {
  1056. hand_r_target = chamber_pos;
  1057. hand_l_target = QVector3D(-0.08F, HP::SHOULDER_Y - 0.04F, 0.06F);
  1058. torso_twist = -0.06F;
  1059. hip_rotation = -0.04F;
  1060. forward_lean = -0.03F;
  1061. crouch_factor = crouch_amount;
  1062. } else if (attack_phase < 0.48F) {
  1063. float t = (attack_phase - 0.28F) / 0.20F;
  1064. float power_t = t * t * t;
  1065. hand_r_target = chamber_pos * (1.0F - power_t) + thrust_pos * power_t;
  1066. hand_l_target = QVector3D(-0.08F + 0.06F * power_t,
  1067. HP::SHOULDER_Y - 0.04F + 0.02F * power_t,
  1068. 0.06F + 0.50F * power_t);
  1069. torso_twist = -0.06F + 0.14F * power_t;
  1070. hip_rotation = -0.04F + 0.10F * power_t;
  1071. forward_lean = -0.05F + 0.20F * power_t;
  1072. shoulder_drop = 0.05F * power_t;
  1073. step_forward = 0.12F * power_t;
  1074. crouch_factor = crouch_amount * (1.0F - power_t * 0.3F);
  1075. if (thrust_height < 0) {
  1076. crouch_factor += 0.06F * power_t;
  1077. } else if (thrust_height > 0) {
  1078. crouch_factor -= 0.03F * power_t;
  1079. }
  1080. } else if (attack_phase < 0.60F) {
  1081. float const t = smoothstep((attack_phase - 0.48F) / 0.12F);
  1082. hand_r_target = thrust_pos * (1.0F - t) + extended_pos * t;
  1083. hand_l_target =
  1084. QVector3D(-0.02F, HP::SHOULDER_Y - 0.02F, 0.56F + 0.10F * t);
  1085. torso_twist = 0.08F;
  1086. hip_rotation = 0.06F;
  1087. forward_lean = 0.15F + 0.05F * t;
  1088. shoulder_drop = 0.05F + 0.02F * t;
  1089. step_forward = 0.12F + 0.04F * t;
  1090. crouch_factor = crouch_amount * 0.7F;
  1091. } else if (attack_phase < 0.78F) {
  1092. float const t = ease_in_out_cubic((attack_phase - 0.60F) / 0.18F);
  1093. hand_r_target = extended_pos * (1.0F - t) + recover_pos * t;
  1094. hand_l_target = QVector3D(-0.02F * (1.0F - t) - 0.08F * t,
  1095. HP::SHOULDER_Y - 0.02F * (1.0F - t) - 0.05F * t,
  1096. 0.66F * (1.0F - t) + 0.38F * t);
  1097. torso_twist = 0.08F * (1.0F - t);
  1098. hip_rotation = 0.06F * (1.0F - t);
  1099. forward_lean = 0.20F * (1.0F - t) + 0.04F * t;
  1100. shoulder_drop = 0.07F * (1.0F - t);
  1101. step_forward = 0.16F * (1.0F - t * 0.5F);
  1102. crouch_factor = crouch_amount * 0.7F * (1.0F - t);
  1103. } else {
  1104. float const t = ease_out((attack_phase - 0.78F) / 0.22F);
  1105. hand_r_target = recover_pos * (1.0F - t) + guard_pos * t;
  1106. hand_l_target =
  1107. QVector3D(-0.08F, HP::SHOULDER_Y - 0.05F * (1.0F - t) - 0.02F * t,
  1108. 0.38F * (1.0F - t) + 0.22F * t);
  1109. forward_lean = 0.04F * (1.0F - t);
  1110. step_forward = 0.08F * (1.0F - t);
  1111. }
  1112. if (std::abs(torso_twist) > 0.001F) {
  1113. float const twist = torso_twist * 0.0F;
  1114. m_pose.shoulder_r.setZ(m_pose.shoulder_r.z() + twist);
  1115. m_pose.shoulder_l.setZ(m_pose.shoulder_l.z() - twist * 0.4F);
  1116. }
  1117. if (std::abs(hip_rotation) > 0.001F) {
  1118. m_pose.pelvis_pos.setZ(m_pose.pelvis_pos.z() + hip_rotation * 0.5F);
  1119. }
  1120. if (std::abs(forward_lean) > 0.001F) {
  1121. m_pose.shoulder_l.setZ(m_pose.shoulder_l.z() + forward_lean);
  1122. m_pose.shoulder_r.setZ(m_pose.shoulder_r.z() + forward_lean);
  1123. m_pose.neck_base.setZ(m_pose.neck_base.z() + forward_lean * 0.85F);
  1124. m_pose.head_pos.setZ(m_pose.head_pos.z() + forward_lean * 0.7F);
  1125. }
  1126. if (shoulder_drop > 0.001F) {
  1127. m_pose.shoulder_r.setY(m_pose.shoulder_r.y() - shoulder_drop);
  1128. m_pose.shoulder_l.setY(m_pose.shoulder_l.y() - shoulder_drop * 0.3F);
  1129. }
  1130. if (step_forward > 0.001F) {
  1131. m_pose.foot_r.setZ(m_pose.foot_r.z() + step_forward);
  1132. m_pose.knee_r.setZ(m_pose.knee_r.z() + step_forward * 0.6F);
  1133. m_pose.foot_l.setZ(m_pose.foot_l.z() - step_forward * 0.15F);
  1134. }
  1135. if (crouch_factor > 0.001F) {
  1136. m_pose.pelvis_pos.setY(m_pose.pelvis_pos.y() - crouch_factor);
  1137. m_pose.shoulder_l.setY(m_pose.shoulder_l.y() - crouch_factor * 0.6F);
  1138. m_pose.shoulder_r.setY(m_pose.shoulder_r.y() - crouch_factor * 0.6F);
  1139. m_pose.neck_base.setY(m_pose.neck_base.y() - crouch_factor * 0.5F);
  1140. m_pose.head_pos.setY(m_pose.head_pos.y() - crouch_factor * 0.4F);
  1141. }
  1142. place_hand_at(false, hand_r_target);
  1143. place_hand_at(true, hand_l_target);
  1144. }
  1145. void HumanoidPoseController::tilt_torso(float side_tilt, float forward_tilt) {
  1146. QVector3D const right = m_anim_ctx.heading_right();
  1147. QVector3D const forward = m_anim_ctx.heading_forward();
  1148. QVector3D const offset = right * side_tilt + forward * forward_tilt;
  1149. m_pose.shoulder_l += offset;
  1150. m_pose.shoulder_r += offset;
  1151. m_pose.neck_base += offset * 1.2F;
  1152. m_pose.head_pos += offset * 1.5F;
  1153. m_pose.body_frames.torso.origin += offset;
  1154. m_pose.body_frames.head.origin += offset * 1.5F;
  1155. }
  1156. } // namespace Render::GL