pose_controller.cpp 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473
  1. #include "pose_controller.h"
  2. #include "humanoid_math.h"
  3. #include "spear_pose_utils.h"
  4. #include <QVector3D>
  5. #include <algorithm>
  6. #include <cmath>
  7. #include <numbers>
  8. namespace Render::GL {
  9. HumanoidPoseController::HumanoidPoseController(
  10. HumanoidPose &pose, const HumanoidAnimationContext &anim_ctx)
  11. : m_pose(pose), m_anim_ctx(anim_ctx) {}
  12. void HumanoidPoseController::standIdle() {}
  13. void HumanoidPoseController::kneel(float depth) {
  14. using HP = HumanProportions;
  15. depth = std::clamp(depth, 0.0F, 1.0F);
  16. if (depth < 1e-6F) {
  17. return;
  18. }
  19. float const kneel_offset = depth * 0.40F;
  20. float const pelvis_y = HP::WAIST_Y - kneel_offset;
  21. m_pose.pelvis_pos.setY(pelvis_y);
  22. float const stance_narrow = 0.11F;
  23. float const left_knee_y = HP::GROUND_Y + 0.07F * depth;
  24. float const left_knee_z = -0.06F * depth;
  25. m_pose.knee_l = QVector3D(-stance_narrow, left_knee_y, left_knee_z);
  26. m_pose.foot_l = QVector3D(-stance_narrow - 0.025F, HP::GROUND_Y,
  27. left_knee_z - HP::LOWER_LEG_LEN * 0.93F * depth);
  28. float const right_knee_y = pelvis_y - 0.12F;
  29. float const right_foot_z = 0.28F * depth;
  30. m_pose.knee_r = QVector3D(stance_narrow, right_knee_y, right_foot_z - 0.05F);
  31. m_pose.foot_r = QVector3D(stance_narrow, HP::GROUND_Y + m_pose.foot_y_offset,
  32. right_foot_z);
  33. float const upper_body_drop = kneel_offset;
  34. m_pose.shoulder_l.setY(m_pose.shoulder_l.y() - upper_body_drop);
  35. m_pose.shoulder_r.setY(m_pose.shoulder_r.y() - upper_body_drop);
  36. m_pose.neck_base.setY(m_pose.neck_base.y() - upper_body_drop);
  37. m_pose.head_pos.setY(m_pose.head_pos.y() - upper_body_drop);
  38. }
  39. void HumanoidPoseController::lean(const QVector3D &direction, float amount) {
  40. amount = std::clamp(amount, 0.0F, 1.0F);
  41. QVector3D dir = direction;
  42. if (dir.lengthSquared() > 1e-6F) {
  43. dir.normalize();
  44. } else {
  45. dir = QVector3D(0.0F, 0.0F, 1.0F);
  46. }
  47. float const lean_magnitude = 0.12F * amount;
  48. QVector3D const lean_offset = dir * lean_magnitude;
  49. m_pose.shoulder_l += lean_offset;
  50. m_pose.shoulder_r += lean_offset;
  51. m_pose.neck_base += lean_offset * 0.85F;
  52. m_pose.head_pos += lean_offset * 0.75F;
  53. }
  54. void HumanoidPoseController::placeHandAt(bool is_left,
  55. const QVector3D &target_position) {
  56. getHand(is_left) = target_position;
  57. const QVector3D &shoulder = getShoulder(is_left);
  58. const QVector3D outward_dir = computeOutwardDir(is_left);
  59. float const along_frac = is_left ? 0.45F : 0.48F;
  60. float const lateral_offset = is_left ? 0.15F : 0.12F;
  61. float const y_bias = is_left ? -0.08F : 0.02F;
  62. float const outward_sign = 1.0F;
  63. getElbow(is_left) =
  64. solveElbowIK(is_left, shoulder, target_position, outward_dir, along_frac,
  65. lateral_offset, y_bias, outward_sign);
  66. }
  67. auto HumanoidPoseController::solveElbowIK(
  68. bool, const QVector3D &shoulder, const QVector3D &hand,
  69. const QVector3D &outward_dir, float along_frac, float lateral_offset,
  70. float y_bias, float outward_sign) const -> QVector3D {
  71. return elbowBendTorso(shoulder, hand, outward_dir, along_frac, lateral_offset,
  72. y_bias, outward_sign);
  73. }
  74. auto HumanoidPoseController::solveKneeIK(
  75. bool is_left, const QVector3D &hip, const QVector3D &foot,
  76. float height_scale) const -> QVector3D {
  77. using HP = HumanProportions;
  78. QVector3D hip_to_foot = foot - hip;
  79. float const distance = hip_to_foot.length();
  80. if (distance < 1e-5F) {
  81. return hip;
  82. }
  83. float const upper_len = HP::UPPER_LEG_LEN * height_scale;
  84. float const lower_len = HP::LOWER_LEG_LEN * height_scale;
  85. float const reach = upper_len + lower_len;
  86. float const min_reach =
  87. std::max(std::abs(upper_len - lower_len) + 1e-4F, 1e-3F);
  88. float const max_reach = std::max(reach - 1e-4F, min_reach + 1e-4F);
  89. float const clamped_dist = std::clamp(distance, min_reach, max_reach);
  90. QVector3D const dir = hip_to_foot / distance;
  91. float cos_theta = (upper_len * upper_len + clamped_dist * clamped_dist -
  92. lower_len * lower_len) /
  93. (2.0F * upper_len * clamped_dist);
  94. cos_theta = std::clamp(cos_theta, -1.0F, 1.0F);
  95. float const sin_theta =
  96. std::sqrt(std::max(0.0F, 1.0F - cos_theta * cos_theta));
  97. QVector3D bend_pref =
  98. is_left ? QVector3D(-0.24F, 0.0F, 0.95F) : QVector3D(0.24F, 0.0F, 0.95F);
  99. bend_pref.normalize();
  100. QVector3D bend_axis = bend_pref - dir * QVector3D::dotProduct(dir, bend_pref);
  101. if (bend_axis.lengthSquared() < 1e-6F) {
  102. bend_axis = QVector3D::crossProduct(dir, QVector3D(0.0F, 1.0F, 0.0F));
  103. if (bend_axis.lengthSquared() < 1e-6F) {
  104. bend_axis = QVector3D::crossProduct(dir, QVector3D(1.0F, 0.0F, 0.0F));
  105. }
  106. }
  107. bend_axis.normalize();
  108. QVector3D knee =
  109. hip + dir * (cos_theta * upper_len) + bend_axis * (sin_theta * upper_len);
  110. float const knee_floor = HP::GROUND_Y + m_pose.foot_y_offset * 0.5F;
  111. if (knee.y() < knee_floor) {
  112. knee.setY(knee_floor);
  113. }
  114. if (knee.y() > hip.y()) {
  115. knee.setY(hip.y());
  116. }
  117. return knee;
  118. }
  119. auto HumanoidPoseController::getShoulder(bool is_left) const
  120. -> const QVector3D & {
  121. return is_left ? m_pose.shoulder_l : m_pose.shoulder_r;
  122. }
  123. auto HumanoidPoseController::getHand(bool is_left) -> QVector3D & {
  124. return is_left ? m_pose.hand_l : m_pose.hand_r;
  125. }
  126. auto HumanoidPoseController::getHand(bool is_left) const -> const QVector3D & {
  127. return is_left ? m_pose.hand_l : m_pose.hand_r;
  128. }
  129. auto HumanoidPoseController::getElbow(bool is_left) -> QVector3D & {
  130. return is_left ? m_pose.elbow_l : m_pose.elbow_r;
  131. }
  132. auto HumanoidPoseController::computeRightAxis() const -> QVector3D {
  133. QVector3D right_axis = m_pose.shoulder_r - m_pose.shoulder_l;
  134. right_axis.setY(0.0F);
  135. if (right_axis.lengthSquared() < 1e-8F) {
  136. right_axis = QVector3D(1.0F, 0.0F, 0.0F);
  137. }
  138. right_axis.normalize();
  139. return right_axis;
  140. }
  141. auto HumanoidPoseController::computeOutwardDir(bool is_left) const
  142. -> QVector3D {
  143. QVector3D const right_axis = computeRightAxis();
  144. return is_left ? -right_axis : right_axis;
  145. }
  146. auto HumanoidPoseController::get_shoulder_y(bool is_left) const -> float {
  147. return is_left ? m_pose.shoulder_l.y() : m_pose.shoulder_r.y();
  148. }
  149. auto HumanoidPoseController::get_pelvis_y() const -> float {
  150. return m_pose.pelvis_pos.y();
  151. }
  152. void HumanoidPoseController::aimBow(float draw_phase) {
  153. using HP = HumanProportions;
  154. draw_phase = std::clamp(draw_phase, 0.0F, 1.0F);
  155. QVector3D const aim_pos(0.18F, HP::SHOULDER_Y + 0.18F, 0.35F);
  156. QVector3D const draw_pos(0.22F, HP::SHOULDER_Y + 0.10F, -0.30F);
  157. QVector3D const release_pos(0.18F, HP::SHOULDER_Y + 0.20F, 0.10F);
  158. QVector3D hand_r_target;
  159. float shoulder_twist = 0.0F;
  160. float head_recoil = 0.0F;
  161. if (draw_phase < 0.20F) {
  162. float t = draw_phase / 0.20F;
  163. t = t * t;
  164. hand_r_target = aim_pos * (1.0F - t) + draw_pos * t;
  165. shoulder_twist = t * 0.08F;
  166. } else if (draw_phase < 0.50F) {
  167. hand_r_target = draw_pos;
  168. shoulder_twist = 0.08F;
  169. } else if (draw_phase < 0.58F) {
  170. float t = (draw_phase - 0.50F) / 0.08F;
  171. t = t * t * t;
  172. hand_r_target = draw_pos * (1.0F - t) + release_pos * t;
  173. shoulder_twist = 0.08F * (1.0F - t * 0.6F);
  174. head_recoil = t * 0.04F;
  175. } else {
  176. float t = (draw_phase - 0.58F) / 0.42F;
  177. t = 1.0F - (1.0F - t) * (1.0F - t);
  178. hand_r_target = release_pos * (1.0F - t) + aim_pos * t;
  179. shoulder_twist = 0.08F * 0.4F * (1.0F - t);
  180. head_recoil = 0.04F * (1.0F - t);
  181. }
  182. QVector3D const hand_l_target(0.0F - 0.05F, HP::SHOULDER_Y + 0.05F, 0.55F);
  183. placeHandAt(true, hand_l_target);
  184. placeHandAt(false, hand_r_target);
  185. if (shoulder_twist > 0.01F) {
  186. m_pose.shoulder_r.setY(m_pose.shoulder_r.y() + shoulder_twist);
  187. m_pose.shoulder_l.setY(m_pose.shoulder_l.y() - shoulder_twist * 0.5F);
  188. }
  189. if (head_recoil > 0.01F) {
  190. m_pose.head_pos.setZ(m_pose.head_pos.z() - head_recoil);
  191. }
  192. }
  193. void HumanoidPoseController::meleeStrike(float strike_phase) {
  194. using HP = HumanProportions;
  195. strike_phase = std::clamp(strike_phase, 0.0F, 1.0F);
  196. QVector3D const rest_pos(0.25F, HP::SHOULDER_Y, 0.10F);
  197. QVector3D const raised_pos(0.30F, HP::HEAD_TOP_Y + 0.2F, -0.05F);
  198. QVector3D const strike_pos(0.35F, HP::WAIST_Y, 0.45F);
  199. QVector3D hand_r_target;
  200. QVector3D hand_l_target;
  201. if (strike_phase < 0.25F) {
  202. float t = strike_phase / 0.25F;
  203. t = t * t;
  204. hand_r_target = rest_pos * (1.0F - t) + raised_pos * t;
  205. hand_l_target = QVector3D(-0.15F, HP::SHOULDER_Y - 0.1F * t, 0.20F);
  206. } else if (strike_phase < 0.35F) {
  207. hand_r_target = raised_pos;
  208. hand_l_target = QVector3D(-0.15F, HP::SHOULDER_Y - 0.1F, 0.20F);
  209. } else if (strike_phase < 0.55F) {
  210. float t = (strike_phase - 0.35F) / 0.2F;
  211. t = t * t * t;
  212. hand_r_target = raised_pos * (1.0F - t) + strike_pos * t;
  213. hand_l_target = QVector3D(-0.15F, HP::SHOULDER_Y - 0.1F * (1.0F - t * 0.5F),
  214. 0.20F + 0.15F * t);
  215. } else {
  216. float t = (strike_phase - 0.55F) / 0.45F;
  217. t = 1.0F - (1.0F - t) * (1.0F - t);
  218. hand_r_target = strike_pos * (1.0F - t) + rest_pos * t;
  219. hand_l_target = QVector3D(-0.15F, HP::SHOULDER_Y - 0.05F * (1.0F - t),
  220. 0.35F * (1.0F - t) + 0.20F * t);
  221. }
  222. placeHandAt(false, hand_r_target);
  223. placeHandAt(true, hand_l_target);
  224. }
  225. void HumanoidPoseController::graspTwoHanded(const QVector3D &grip_center,
  226. float hand_separation) {
  227. hand_separation = std::clamp(hand_separation, 0.1F, 0.8F);
  228. QVector3D const right_axis = computeRightAxis();
  229. QVector3D const right_hand_pos =
  230. grip_center + right_axis * (hand_separation * 0.5F);
  231. QVector3D const left_hand_pos =
  232. grip_center - right_axis * (hand_separation * 0.5F);
  233. placeHandAt(false, right_hand_pos);
  234. placeHandAt(true, left_hand_pos);
  235. }
  236. void HumanoidPoseController::spearThrust(float attack_phase) {
  237. using HP = HumanProportions;
  238. attack_phase = std::clamp(attack_phase, 0.0F, 1.0F);
  239. QVector3D const guard_pos(0.28F, HP::SHOULDER_Y + 0.05F, 0.25F);
  240. QVector3D const prepare_pos(0.35F, HP::SHOULDER_Y + 0.08F, 0.05F);
  241. QVector3D const thrust_pos(0.32F, HP::SHOULDER_Y + 0.10F, 0.90F);
  242. QVector3D const recover_pos(0.28F, HP::SHOULDER_Y + 0.06F, 0.40F);
  243. QVector3D hand_r_target;
  244. QVector3D hand_l_target;
  245. auto easeInOutCubic = [](float t) {
  246. return t < 0.5F ? 4.0F * t * t * t
  247. : 1.0F - std::pow(-2.0F * t + 2.0F, 3.0F) / 2.0F;
  248. };
  249. auto smoothstep = [](float edge0, float edge1, float x) {
  250. float t = std::clamp((x - edge0) / (edge1 - edge0), 0.0F, 1.0F);
  251. return t * t * (3.0F - 2.0F * t);
  252. };
  253. auto lerp = [](float a, float b, float t) { return a * (1.0F - t) + b * t; };
  254. if (attack_phase < 0.20F) {
  255. float const t = easeInOutCubic(attack_phase / 0.20F);
  256. hand_r_target = guard_pos * (1.0F - t) + prepare_pos * t;
  257. hand_l_target = QVector3D(-0.10F, HP::SHOULDER_Y - 0.05F,
  258. 0.20F * (1.0F - t) + 0.08F * t);
  259. } else if (attack_phase < 0.30F) {
  260. hand_r_target = prepare_pos;
  261. hand_l_target = QVector3D(-0.10F, HP::SHOULDER_Y - 0.05F, 0.08F);
  262. } else if (attack_phase < 0.50F) {
  263. float t = (attack_phase - 0.30F) / 0.20F;
  264. t = t * t * t;
  265. hand_r_target = prepare_pos * (1.0F - t) + thrust_pos * t;
  266. hand_l_target =
  267. QVector3D(-0.10F + 0.05F * t, HP::SHOULDER_Y - 0.05F + 0.03F * t,
  268. 0.08F + 0.45F * t);
  269. } else if (attack_phase < 0.70F) {
  270. float const t = easeInOutCubic((attack_phase - 0.50F) / 0.20F);
  271. hand_r_target = thrust_pos * (1.0F - t) + recover_pos * t;
  272. hand_l_target = QVector3D(-0.05F * (1.0F - t) - 0.10F * t,
  273. HP::SHOULDER_Y - 0.02F * (1.0F - t) - 0.06F * t,
  274. lerp(0.53F, 0.35F, t));
  275. } else {
  276. float const t = smoothstep(0.70F, 1.0F, attack_phase);
  277. hand_r_target = recover_pos * (1.0F - t) + guard_pos * t;
  278. hand_l_target =
  279. QVector3D(-0.10F - 0.02F * (1.0F - t),
  280. HP::SHOULDER_Y - 0.06F + 0.01F * t, lerp(0.35F, 0.25F, t));
  281. }
  282. float const thrust_extent =
  283. std::clamp((attack_phase - 0.20F) / 0.60F, 0.0F, 1.0F);
  284. float const along_offset = -0.06F + 0.02F * thrust_extent;
  285. float const y_drop = 0.10F + 0.02F * thrust_extent;
  286. hand_l_target = computeOffhandSpearGrip(m_pose, m_anim_ctx, hand_r_target,
  287. false, along_offset, y_drop, -0.08F);
  288. placeHandAt(false, hand_r_target);
  289. placeHandAt(true, hand_l_target);
  290. }
  291. void HumanoidPoseController::swordSlash(float attack_phase) {
  292. using HP = HumanProportions;
  293. attack_phase = std::clamp(attack_phase, 0.0F, 1.0F);
  294. QVector3D const rest_pos(0.20F, HP::SHOULDER_Y + 0.05F, 0.15F);
  295. QVector3D const prepare_pos(0.26F, HP::HEAD_TOP_Y + 0.18F, -0.06F);
  296. QVector3D const raised_pos(0.25F, HP::HEAD_TOP_Y + 0.22F, 0.02F);
  297. QVector3D const strike_pos(0.30F, HP::WAIST_Y - 0.05F, 0.50F);
  298. QVector3D const recover_pos(0.22F, HP::SHOULDER_Y + 0.02F, 0.22F);
  299. QVector3D hand_r_target;
  300. QVector3D hand_l_target;
  301. auto easeInOutCubic = [](float t) {
  302. return t < 0.5F ? 4.0F * t * t * t
  303. : 1.0F - std::pow(-2.0F * t + 2.0F, 3.0F) / 2.0F;
  304. };
  305. auto smoothstep = [](float edge0, float edge1, float x) {
  306. float t = std::clamp((x - edge0) / (edge1 - edge0), 0.0F, 1.0F);
  307. return t * t * (3.0F - 2.0F * t);
  308. };
  309. auto lerp = [](float a, float b, float t) { return a * (1.0F - t) + b * t; };
  310. if (attack_phase < 0.18F) {
  311. float const t = easeInOutCubic(attack_phase / 0.18F);
  312. hand_r_target = rest_pos * (1.0F - t) + prepare_pos * t;
  313. hand_l_target =
  314. QVector3D(-0.21F, HP::SHOULDER_Y - 0.02F - 0.03F * t, 0.15F);
  315. } else if (attack_phase < 0.32F) {
  316. float const t = easeInOutCubic((attack_phase - 0.18F) / 0.14F);
  317. hand_r_target = prepare_pos * (1.0F - t) + raised_pos * t;
  318. hand_l_target = QVector3D(-0.21F, HP::SHOULDER_Y - 0.05F, 0.17F);
  319. } else if (attack_phase < 0.52F) {
  320. float t = (attack_phase - 0.32F) / 0.20F;
  321. t = t * t * t;
  322. hand_r_target = raised_pos * (1.0F - t) + strike_pos * t;
  323. hand_l_target = QVector3D(
  324. -0.21F, HP::SHOULDER_Y - 0.03F * (1.0F - 0.5F * t), 0.17F + 0.20F * t);
  325. } else if (attack_phase < 0.72F) {
  326. float const t = easeInOutCubic((attack_phase - 0.52F) / 0.20F);
  327. hand_r_target = strike_pos * (1.0F - t) + recover_pos * t;
  328. hand_l_target = QVector3D(-0.20F, HP::SHOULDER_Y - 0.015F * (1.0F - t),
  329. lerp(0.37F, 0.20F, t));
  330. } else {
  331. float const t = smoothstep(0.72F, 1.0F, attack_phase);
  332. hand_r_target = recover_pos * (1.0F - t) + rest_pos * t;
  333. hand_l_target = QVector3D(-0.20F - 0.02F * (1.0F - t), HP::SHOULDER_Y,
  334. lerp(0.20F, 0.15F, t));
  335. }
  336. placeHandAt(false, hand_r_target);
  337. placeHandAt(true, hand_l_target);
  338. }
  339. void HumanoidPoseController::mountOnHorse(float saddle_height) {
  340. float const offset_y = saddle_height - m_pose.pelvis_pos.y();
  341. m_pose.pelvis_pos.setY(saddle_height);
  342. }
  343. void HumanoidPoseController::hold_sword_and_shield() {
  344. using HP = HumanProportions;
  345. QVector3D const sword_hand_pos(0.30F, HP::SHOULDER_Y - 0.02F, 0.35F);
  346. QVector3D const shield_hand_pos(-0.22F, HP::SHOULDER_Y, 0.18F);
  347. placeHandAt(false, sword_hand_pos);
  348. placeHandAt(true, shield_hand_pos);
  349. }
  350. void HumanoidPoseController::look_at(const QVector3D &target) {
  351. QVector3D const head_to_target = target - m_pose.head_pos;
  352. if (head_to_target.lengthSquared() < 1e-6F) {
  353. return;
  354. }
  355. QVector3D const direction = head_to_target.normalized();
  356. float const max_head_turn = 0.03F;
  357. QVector3D const head_offset = direction * max_head_turn;
  358. m_pose.head_pos += QVector3D(head_offset.x(), 0.0F, head_offset.z());
  359. float const neck_follow = 0.5F;
  360. m_pose.neck_base += QVector3D(head_offset.x() * neck_follow, 0.0F,
  361. head_offset.z() * neck_follow);
  362. }
  363. } // namespace Render::GL