horse_animation_controller.cpp 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289
  1. #include "horse_animation_controller.h"
  2. #include "../humanoid/rig.h"
  3. #include <algorithm>
  4. #include <cmath>
  5. #include <numbers>
  6. namespace Render::GL {
  7. namespace {
  8. constexpr float k_pi = std::numbers::pi_v<float>;
  9. constexpr float k_idle_breathing_primary_freq = 0.4F;
  10. constexpr float k_idle_breathing_secondary_freq = 0.15F;
  11. constexpr float k_idle_breathing_primary_weight = 0.6F;
  12. constexpr float k_idle_breathing_secondary_weight = 0.4F;
  13. constexpr float k_idle_phase_speed = 0.15F;
  14. struct GaitParameters {
  15. float cycle_time;
  16. float front_leg_phase;
  17. float rear_leg_phase;
  18. float stride_swing;
  19. float stride_lift;
  20. float bob_amplitude;
  21. };
  22. constexpr GaitParameters getGaitParams(GaitType gait) {
  23. switch (gait) {
  24. case GaitType::IDLE:
  25. return {2.5F, 0.0F, 0.0F, 0.015F, 0.008F, 0.004F};
  26. case GaitType::WALK:
  27. return {1.1F, 0.50F, 0.0F, 0.42F, 0.18F, 0.016F};
  28. case GaitType::TROT:
  29. return {0.55F, 0.0F, 0.50F, 0.62F, 0.32F, 0.028F};
  30. case GaitType::CANTER:
  31. return {0.48F, 0.62F, 0.35F, 0.65F, 0.35F, 0.032F};
  32. case GaitType::GALLOP:
  33. return {0.38F, 0.50F, 0.15F, 0.85F, 0.48F, 0.042F};
  34. }
  35. return {2.5F, 0.0F, 0.0F, 0.015F, 0.008F, 0.004F};
  36. }
  37. } // namespace
  38. HorseAnimationController::HorseAnimationController(
  39. HorseProfile &profile, const AnimationInputs &anim,
  40. const HumanoidAnimationContext &rider_ctx)
  41. : m_profile(profile), m_anim(anim), m_rider_ctx(rider_ctx) {
  42. m_phase = 0.0F;
  43. m_bob = 0.0F;
  44. m_rein_slack = 0.05F;
  45. m_speed = 0.0F;
  46. m_turn_angle = 0.0F;
  47. m_banking = 0.0F;
  48. m_is_rearing = false;
  49. m_rear_height = 0.0F;
  50. m_is_kicking = false;
  51. m_kick_rear_legs = false;
  52. m_kick_power = 0.0F;
  53. m_is_bucking = false;
  54. m_buck_intensity = 0.0F;
  55. m_is_jumping = false;
  56. m_jump_height = 0.0F;
  57. m_jump_distance = 0.0F;
  58. m_target_gait = GaitType::IDLE;
  59. m_gait_transition_progress = 1.0F;
  60. m_transition_start_time = 0.0F;
  61. }
  62. void HorseAnimationController::set_gait(GaitType gait) {
  63. m_current_gait = gait;
  64. m_target_gait = gait;
  65. m_gait_transition_progress = 1.0F;
  66. switch (gait) {
  67. case GaitType::IDLE:
  68. m_speed = 0.0F;
  69. break;
  70. case GaitType::WALK:
  71. m_speed = 1.5F;
  72. break;
  73. case GaitType::TROT:
  74. m_speed = 4.0F;
  75. break;
  76. case GaitType::CANTER:
  77. m_speed = 6.5F;
  78. break;
  79. case GaitType::GALLOP:
  80. m_speed = 10.0F;
  81. break;
  82. }
  83. update_gait_parameters();
  84. }
  85. void HorseAnimationController::idle(float bob_intensity) {
  86. m_current_gait = GaitType::IDLE;
  87. m_speed = 0.0F;
  88. float const phase = std::fmod(m_anim.time * 0.25F, 1.0F);
  89. m_phase = phase;
  90. m_bob = std::sin(phase * 2.0F * k_pi) * m_profile.dims.idle_bob_amplitude *
  91. bob_intensity;
  92. update_gait_parameters();
  93. }
  94. void HorseAnimationController::accelerate(float speed_delta) {
  95. m_speed += speed_delta;
  96. m_speed = std::max(0.0F, m_speed);
  97. GaitType new_gait;
  98. if (m_speed < 0.5F) {
  99. new_gait = GaitType::IDLE;
  100. } else if (m_speed < 3.0F) {
  101. new_gait = GaitType::WALK;
  102. } else if (m_speed < 5.5F) {
  103. new_gait = GaitType::TROT;
  104. } else if (m_speed < 8.0F) {
  105. new_gait = GaitType::CANTER;
  106. } else {
  107. new_gait = GaitType::GALLOP;
  108. }
  109. if (m_current_gait != new_gait) {
  110. m_target_gait = new_gait;
  111. m_gait_transition_progress = 0.0F;
  112. m_transition_start_time = m_anim.time;
  113. }
  114. update_gait_parameters();
  115. }
  116. void HorseAnimationController::decelerate(float speed_delta) {
  117. accelerate(-speed_delta);
  118. }
  119. void HorseAnimationController::turn(float yaw_radians, float banking_amount) {
  120. m_turn_angle = yaw_radians;
  121. m_banking = std::clamp(banking_amount, -1.0F, 1.0F);
  122. }
  123. void HorseAnimationController::strafe_step(bool left, float distance) {
  124. float const direction = left ? -1.0F : 1.0F;
  125. m_phase = std::fmod(m_phase + direction * distance * 0.1F, 1.0F);
  126. }
  127. void HorseAnimationController::rear(float height_factor) {
  128. m_is_rearing = true;
  129. m_rear_height = std::clamp(height_factor, 0.0F, 1.0F);
  130. }
  131. void HorseAnimationController::kick(bool rear_legs, float power) {
  132. m_is_kicking = true;
  133. m_kick_rear_legs = rear_legs;
  134. m_kick_power = std::clamp(power, 0.0F, 1.0F);
  135. }
  136. void HorseAnimationController::buck(float intensity) {
  137. m_is_bucking = true;
  138. m_buck_intensity = std::clamp(intensity, 0.0F, 1.0F);
  139. }
  140. void HorseAnimationController::jump_obstacle(float height, float distance) {
  141. m_is_jumping = true;
  142. m_jump_height = std::max(0.0F, height);
  143. m_jump_distance = std::max(0.0F, distance);
  144. }
  145. auto HorseAnimationController::get_current_phase() const -> float {
  146. return m_phase;
  147. }
  148. auto HorseAnimationController::get_current_bob() const -> float {
  149. return m_bob;
  150. }
  151. auto HorseAnimationController::get_stride_cycle() const -> float {
  152. GaitParameters const params = getGaitParams(m_current_gait);
  153. return params.cycle_time;
  154. }
  155. void HorseAnimationController::update_gait_parameters() {
  156. constexpr float transition_duration = 0.3F;
  157. if (m_gait_transition_progress < 1.0F) {
  158. float const elapsed = m_anim.time - m_transition_start_time;
  159. m_gait_transition_progress = std::min(1.0F, elapsed / transition_duration);
  160. if (m_gait_transition_progress >= 1.0F) {
  161. m_current_gait = m_target_gait;
  162. }
  163. }
  164. GaitParameters const current_params = getGaitParams(m_current_gait);
  165. GaitParameters target_params = current_params;
  166. if (m_gait_transition_progress < 1.0F) {
  167. target_params = getGaitParams(m_target_gait);
  168. float const t = m_gait_transition_progress;
  169. float const ease_t = t * t * (3.0F - 2.0F * t);
  170. m_profile.gait.cycle_time =
  171. current_params.cycle_time +
  172. ease_t * (target_params.cycle_time - current_params.cycle_time);
  173. m_profile.gait.front_leg_phase = current_params.front_leg_phase +
  174. ease_t * (target_params.front_leg_phase -
  175. current_params.front_leg_phase);
  176. m_profile.gait.rear_leg_phase =
  177. current_params.rear_leg_phase +
  178. ease_t * (target_params.rear_leg_phase - current_params.rear_leg_phase);
  179. m_profile.gait.stride_swing =
  180. current_params.stride_swing +
  181. ease_t * (target_params.stride_swing - current_params.stride_swing);
  182. m_profile.gait.stride_lift =
  183. current_params.stride_lift +
  184. ease_t * (target_params.stride_lift - current_params.stride_lift);
  185. } else {
  186. m_profile.gait.cycle_time = current_params.cycle_time;
  187. m_profile.gait.front_leg_phase = current_params.front_leg_phase;
  188. m_profile.gait.rear_leg_phase = current_params.rear_leg_phase;
  189. m_profile.gait.stride_swing = current_params.stride_swing;
  190. m_profile.gait.stride_lift = current_params.stride_lift;
  191. }
  192. bool const is_moving = m_current_gait != GaitType::IDLE;
  193. if (is_moving) {
  194. if (m_rider_ctx.gait.cycle_time > 0.0001F) {
  195. m_phase = m_rider_ctx.gait.cycle_phase;
  196. } else {
  197. m_phase = std::fmod(m_anim.time / m_profile.gait.cycle_time, 1.0F);
  198. }
  199. float const rider_intensity = m_rider_ctx.locomotion_normalized_speed();
  200. float const bob_amp = m_profile.dims.idle_bob_amplitude +
  201. rider_intensity * (m_profile.dims.move_bob_amplitude -
  202. m_profile.dims.idle_bob_amplitude);
  203. float const primary_bob = std::sin(m_phase * 2.0F * k_pi);
  204. float const secondary_bob = std::sin(m_phase * 4.0F * k_pi) * 0.25F;
  205. float const tertiary_variation =
  206. std::sin(m_anim.time * 0.7F) * 0.08F + 1.0F;
  207. float bob_pattern = primary_bob;
  208. if (m_current_gait == GaitType::TROT) {
  209. bob_pattern = (primary_bob + secondary_bob * 0.5F);
  210. } else if (m_current_gait == GaitType::CANTER) {
  211. bob_pattern = primary_bob + std::sin(m_phase * 3.0F * k_pi) * 0.15F;
  212. } else if (m_current_gait == GaitType::GALLOP) {
  213. bob_pattern = primary_bob * 1.2F + secondary_bob * 0.3F;
  214. }
  215. m_bob = bob_pattern * bob_amp * tertiary_variation;
  216. } else {
  217. float const breathing =
  218. std::sin(m_anim.time * k_idle_breathing_primary_freq) *
  219. k_idle_breathing_primary_weight +
  220. std::sin(m_anim.time * k_idle_breathing_secondary_freq) *
  221. k_idle_breathing_secondary_weight;
  222. m_phase = std::fmod(m_anim.time * k_idle_phase_speed, 1.0F);
  223. m_bob = breathing * m_profile.dims.idle_bob_amplitude * 0.8F;
  224. }
  225. float rein_tension = m_rider_ctx.locomotion_normalized_speed();
  226. if (m_rider_ctx.gait.has_target) {
  227. rein_tension += 0.25F;
  228. }
  229. if (m_rider_ctx.is_attacking()) {
  230. rein_tension += 0.35F;
  231. }
  232. rein_tension = std::clamp(rein_tension, 0.0F, 1.0F);
  233. m_rein_slack = std::max(0.01F, m_rein_slack * (1.0F - rein_tension));
  234. }
  235. } // namespace Render::GL