mounted_pose_controller_test.cpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425
  1. #include "render/horse/rig.h"
  2. #include "render/humanoid/humanoid_specs.h"
  3. #include "render/humanoid/mounted_pose_controller.h"
  4. #include "render/humanoid/rig.h"
  5. #include <QVector3D>
  6. #include <cmath>
  7. #include <gtest/gtest.h>
  8. using namespace Render::GL;
  9. class MountedPoseControllerTest : public ::testing::Test {
  10. protected:
  11. void SetUp() override {
  12. using HP = HumanProportions;
  13. // Initialize a default pose with basic standing configuration
  14. pose = HumanoidPose{};
  15. float const head_center_y = HP::HEAD_CENTER_Y;
  16. float const half_shoulder = 0.5F * HP::SHOULDER_WIDTH;
  17. pose.head_pos = QVector3D(0.0F, head_center_y, 0.0F);
  18. pose.head_r = HP::HEAD_RADIUS;
  19. pose.neck_base = QVector3D(0.0F, HP::NECK_BASE_Y, 0.0F);
  20. pose.shoulder_l = QVector3D(-half_shoulder, HP::SHOULDER_Y, 0.0F);
  21. pose.shoulder_r = QVector3D(half_shoulder, HP::SHOULDER_Y, 0.0F);
  22. pose.pelvis_pos = QVector3D(0.0F, HP::WAIST_Y, 0.0F);
  23. pose.hand_l = QVector3D(-0.05F, HP::SHOULDER_Y + 0.05F, 0.55F);
  24. pose.hand_r = QVector3D(0.15F, HP::SHOULDER_Y + 0.15F, 0.20F);
  25. pose.elbow_l = QVector3D(-0.15F, HP::SHOULDER_Y - 0.15F, 0.25F);
  26. pose.elbow_r = QVector3D(0.25F, HP::SHOULDER_Y - 0.10F, 0.10F);
  27. pose.knee_l = QVector3D(-0.10F, HP::KNEE_Y, 0.05F);
  28. pose.knee_r = QVector3D(0.10F, HP::KNEE_Y, -0.05F);
  29. pose.foot_l = QVector3D(-0.14F, 0.022F, 0.06F);
  30. pose.foot_r = QVector3D(0.14F, 0.022F, -0.06F);
  31. pose.foot_y_offset = 0.022F;
  32. // Initialize animation context with default idle state
  33. anim_ctx = HumanoidAnimationContext{};
  34. anim_ctx.inputs.time = 0.0F;
  35. anim_ctx.inputs.is_moving = false;
  36. anim_ctx.inputs.is_attacking = false;
  37. anim_ctx.variation = VariationParams::fromSeed(12345);
  38. anim_ctx.gait.state = HumanoidMotionState::Idle;
  39. // Initialize a typical horse mount frame
  40. mount = MountedAttachmentFrame{};
  41. mount.saddle_center = QVector3D(0.0F, 1.20F, 0.0F);
  42. mount.seat_position = QVector3D(0.0F, 1.25F, 0.0F);
  43. mount.seat_forward = QVector3D(0.0F, 0.0F, 1.0F);
  44. mount.seat_right = QVector3D(1.0F, 0.0F, 0.0F);
  45. mount.seat_up = QVector3D(0.0F, 1.0F, 0.0F);
  46. mount.ground_offset = QVector3D(0.0F, 0.0F, 0.0F);
  47. mount.stirrup_attach_left = QVector3D(-0.35F, 1.05F, 0.15F);
  48. mount.stirrup_attach_right = QVector3D(0.35F, 1.05F, 0.15F);
  49. mount.stirrup_bottom_left = QVector3D(-0.40F, 0.75F, 0.20F);
  50. mount.stirrup_bottom_right = QVector3D(0.40F, 0.75F, 0.20F);
  51. mount.rein_bit_left = QVector3D(-0.12F, 1.48F, 0.95F);
  52. mount.rein_bit_right = QVector3D(0.12F, 1.48F, 0.95F);
  53. mount.bridle_base = QVector3D(0.0F, 1.50F, 0.85F);
  54. }
  55. HumanoidPose pose;
  56. HumanoidAnimationContext anim_ctx;
  57. MountedAttachmentFrame mount;
  58. // Helper to check if a position is approximately equal
  59. bool approxEqual(const QVector3D &a, const QVector3D &b,
  60. float epsilon = 0.01F) {
  61. return std::abs(a.x() - b.x()) < epsilon &&
  62. std::abs(a.y() - b.y()) < epsilon &&
  63. std::abs(a.z() - b.z()) < epsilon;
  64. }
  65. };
  66. TEST_F(MountedPoseControllerTest, ConstructorInitializesCorrectly) {
  67. MountedPoseController controller(pose, anim_ctx);
  68. // Constructor should not modify the pose
  69. EXPECT_FLOAT_EQ(pose.pelvis_pos.y(), HumanProportions::WAIST_Y);
  70. }
  71. TEST_F(MountedPoseControllerTest, MountOnHorsePositionsPelvisOnSaddle) {
  72. MountedPoseController controller(pose, anim_ctx);
  73. controller.mountOnHorse(mount);
  74. // Pelvis should be at seat position
  75. EXPECT_TRUE(approxEqual(pose.pelvis_pos, mount.seat_position));
  76. }
  77. TEST_F(MountedPoseControllerTest, MountOnHorsePlacesFeetInStirrups) {
  78. MountedPoseController controller(pose, anim_ctx);
  79. controller.mountOnHorse(mount);
  80. // Feet should be in stirrups
  81. EXPECT_TRUE(approxEqual(pose.foot_l, mount.stirrup_bottom_left));
  82. EXPECT_TRUE(approxEqual(pose.foot_r, mount.stirrup_bottom_right));
  83. }
  84. TEST_F(MountedPoseControllerTest, MountOnHorseLiftsUpperBody) {
  85. MountedPoseController controller(pose, anim_ctx);
  86. float const original_shoulder_y = pose.shoulder_l.y();
  87. controller.mountOnHorse(mount);
  88. // Shoulders should be lifted when mounted
  89. EXPECT_GT(pose.shoulder_l.y(), original_shoulder_y);
  90. EXPECT_GT(pose.shoulder_r.y(), original_shoulder_y);
  91. }
  92. TEST_F(MountedPoseControllerTest, DismountRestoresStandingPosition) {
  93. MountedPoseController controller(pose, anim_ctx);
  94. controller.mountOnHorse(mount);
  95. controller.dismount();
  96. // Pelvis should be back at standing height
  97. EXPECT_NEAR(pose.pelvis_pos.y(), HumanProportions::WAIST_Y, 0.01F);
  98. }
  99. TEST_F(MountedPoseControllerTest, RidingIdleSetsHandsToRestPosition) {
  100. MountedPoseController controller(pose, anim_ctx);
  101. controller.ridingIdle(mount);
  102. // Hands should be in a resting position near pelvis
  103. EXPECT_LT(pose.hand_l.y(), mount.seat_position.y());
  104. EXPECT_LT(pose.hand_r.y(), mount.seat_position.y());
  105. }
  106. TEST_F(MountedPoseControllerTest, RidingLeaningForwardMovesTorso) {
  107. MountedPoseController controller(pose, anim_ctx);
  108. controller.ridingIdle(mount);
  109. QVector3D const original_shoulder_z = pose.shoulder_l;
  110. controller.ridingLeaning(mount, 1.0F, 0.0F); // Full forward lean
  111. // Shoulders should move forward
  112. EXPECT_GT(pose.shoulder_l.z(), original_shoulder_z.z());
  113. EXPECT_GT(pose.shoulder_r.z(), original_shoulder_z.z());
  114. }
  115. TEST_F(MountedPoseControllerTest, RidingLeaningSidewaysMovesTorso) {
  116. MountedPoseController controller(pose, anim_ctx);
  117. controller.ridingIdle(mount);
  118. QVector3D const original_shoulder_x = pose.shoulder_l;
  119. controller.ridingLeaning(mount, 0.0F, 1.0F); // Full right lean
  120. // Shoulders should move to the right
  121. EXPECT_GT(pose.shoulder_r.x(), original_shoulder_x.x());
  122. }
  123. TEST_F(MountedPoseControllerTest, RidingLeaningClampsInputs) {
  124. MountedPoseController controller(pose, anim_ctx);
  125. // Should not crash with out-of-range inputs
  126. EXPECT_NO_THROW(controller.ridingLeaning(mount, 2.0F, -2.0F));
  127. }
  128. TEST_F(MountedPoseControllerTest, RidingChargingLeansForward) {
  129. MountedPoseController controller(pose, anim_ctx);
  130. controller.ridingIdle(mount);
  131. QVector3D const original_shoulder = pose.shoulder_l;
  132. controller.ridingCharging(mount, 1.0F);
  133. // Should lean forward when charging
  134. EXPECT_GT(pose.shoulder_l.z(), original_shoulder.z());
  135. EXPECT_LT(pose.shoulder_l.y(), original_shoulder.y()); // Crouch
  136. }
  137. TEST_F(MountedPoseControllerTest, RidingReiningPullsHandsBack) {
  138. MountedPoseController controller(pose, anim_ctx);
  139. controller.ridingIdle(mount);
  140. float const idle_left_z = pose.hand_l.z();
  141. float const idle_right_z = pose.hand_r.z();
  142. controller.ridingReining(mount, 1.0F, 1.0F);
  143. // Hands should be pulled back when reining
  144. EXPECT_LT(pose.hand_l.z(), idle_left_z);
  145. EXPECT_LT(pose.hand_r.z(), idle_right_z);
  146. }
  147. TEST_F(MountedPoseControllerTest, RidingReiningLeansTorsoBack) {
  148. MountedPoseController controller(pose, anim_ctx);
  149. controller.ridingIdle(mount);
  150. QVector3D const original_shoulder = pose.shoulder_l;
  151. controller.ridingReining(mount, 1.0F, 1.0F);
  152. // Should lean back when reining hard
  153. EXPECT_LT(pose.shoulder_l.z(), original_shoulder.z());
  154. }
  155. TEST_F(MountedPoseControllerTest, RidingMeleeStrikeAnimatesCorrectly) {
  156. MountedPoseController controller(pose, anim_ctx);
  157. // Test windup phase
  158. controller.ridingMeleeStrike(mount, 0.15F);
  159. float const windup_y = pose.hand_r.y();
  160. // Test strike phase
  161. controller.ridingMeleeStrike(mount, 0.40F);
  162. float const strike_y = pose.hand_r.y();
  163. // Hand should be lower during strike than windup
  164. EXPECT_LT(strike_y, windup_y);
  165. }
  166. TEST_F(MountedPoseControllerTest, RidingSpearThrustAnimatesCorrectly) {
  167. MountedPoseController controller(pose, anim_ctx);
  168. // Test guard phase
  169. controller.ridingSpearThrust(mount, 0.10F);
  170. float const guard_z = pose.hand_r.z();
  171. // Test thrust phase
  172. controller.ridingSpearThrust(mount, 0.35F);
  173. float const thrust_z = pose.hand_r.z();
  174. // Hand should move forward during thrust
  175. EXPECT_GT(thrust_z, guard_z);
  176. }
  177. TEST_F(MountedPoseControllerTest, RidingBowShotAnimatesCorrectly) {
  178. MountedPoseController controller(pose, anim_ctx);
  179. // Test initial draw
  180. controller.ridingBowShot(mount, 0.10F);
  181. QVector3D const draw_start = pose.hand_r;
  182. // Test full draw
  183. controller.ridingBowShot(mount, 0.40F);
  184. QVector3D const draw_end = pose.hand_r;
  185. // Right hand should move back when drawing
  186. float const dist_moved = (draw_end - draw_start).length();
  187. EXPECT_GT(dist_moved, 0.05F);
  188. }
  189. TEST_F(MountedPoseControllerTest, RidingShieldDefenseRaisesHand) {
  190. MountedPoseController controller(pose, anim_ctx);
  191. controller.ridingShieldDefense(mount, false);
  192. float const lowered_y = pose.hand_l.y();
  193. controller.ridingShieldDefense(mount, true);
  194. float const raised_y = pose.hand_l.y();
  195. // Shield should be higher when raised
  196. EXPECT_GT(raised_y, lowered_y);
  197. }
  198. TEST_F(MountedPoseControllerTest, HoldReinsPositionsHandsCorrectly) {
  199. MountedPoseController controller(pose, anim_ctx);
  200. controller.mountOnHorse(mount);
  201. controller.holdReins(mount, 0.5F, 0.5F, 0.3F, 0.3F);
  202. // Hands should stay near the saddle area with a slight forward bias
  203. EXPECT_LT(std::abs(pose.hand_l.x()), mount.seat_position.x() + 0.30F);
  204. EXPECT_LT(std::abs(pose.hand_r.x()), mount.seat_position.x() + 0.30F);
  205. EXPECT_LT(pose.hand_l.y(), mount.seat_position.y());
  206. EXPECT_LT(pose.hand_r.y(), mount.seat_position.y());
  207. }
  208. TEST_F(MountedPoseControllerTest, HoldReinsSlackAffectsHandPosition) {
  209. MountedPoseController controller(pose, anim_ctx);
  210. controller.mountOnHorse(mount);
  211. controller.holdReins(mount, 0.0F, 0.0F, 1.0F, 1.0F);
  212. QVector3D const tight_left = pose.hand_l;
  213. controller.holdReins(mount, 1.0F, 1.0F, 0.0F, 0.0F);
  214. QVector3D const slack_left = pose.hand_l;
  215. // Slack reins should lower hands
  216. EXPECT_LT(slack_left.y(), tight_left.y());
  217. }
  218. TEST_F(MountedPoseControllerTest, HoldSpearOverhandRaisesHand) {
  219. MountedPoseController controller(pose, anim_ctx);
  220. controller.holdSpearMounted(mount, SpearGrip::OVERHAND);
  221. // Right hand should be high for overhead grip
  222. EXPECT_GT(pose.hand_r.y(), mount.seat_position.y() + 0.40F);
  223. }
  224. TEST_F(MountedPoseControllerTest, HoldSpearCouchedLowersHand) {
  225. MountedPoseController controller(pose, anim_ctx);
  226. controller.holdSpearMounted(mount, SpearGrip::COUCHED);
  227. // Right hand should be low for couched grip
  228. EXPECT_LT(pose.hand_r.y(), mount.seat_position.y() + 0.20F);
  229. }
  230. TEST_F(MountedPoseControllerTest, HoldSpearTwoHandedUsesBothHands) {
  231. MountedPoseController controller(pose, anim_ctx);
  232. controller.holdSpearMounted(mount, SpearGrip::TWO_HANDED);
  233. // Both hands should be on spear shaft
  234. float const hand_separation = (pose.hand_r - pose.hand_l).length();
  235. EXPECT_GT(hand_separation, 0.15F);
  236. EXPECT_LT(hand_separation, 0.35F);
  237. }
  238. TEST_F(MountedPoseControllerTest, HoldBowMountedPositionsHandsCorrectly) {
  239. MountedPoseController controller(pose, anim_ctx);
  240. controller.holdBowMounted(mount);
  241. // Left hand should hold bow forward
  242. EXPECT_GT(pose.hand_l.z(), mount.seat_position.z());
  243. // Right hand should be near bow for arrow nocking
  244. float const hand_separation = (pose.hand_r - pose.hand_l).length();
  245. EXPECT_LT(hand_separation, 0.25F);
  246. }
  247. TEST_F(MountedPoseControllerTest, KneePositionValidForMountedRiding) {
  248. MountedPoseController controller(pose, anim_ctx);
  249. controller.mountOnHorse(mount);
  250. // Knees should be between pelvis and feet
  251. EXPECT_LT(pose.knee_l.y(), pose.pelvis_pos.y());
  252. EXPECT_GT(pose.knee_l.y(), pose.foot_l.y());
  253. EXPECT_LT(pose.knee_r.y(), pose.pelvis_pos.y());
  254. EXPECT_GT(pose.knee_r.y(), pose.foot_r.y());
  255. }
  256. TEST_F(MountedPoseControllerTest, ElbowPositionValidForAllActions) {
  257. MountedPoseController controller(pose, anim_ctx);
  258. controller.ridingIdle(mount);
  259. // Elbows should be between shoulders and hands
  260. float const left_shoulder_elbow = (pose.elbow_l - pose.shoulder_l).length();
  261. float const left_elbow_hand = (pose.hand_l - pose.elbow_l).length();
  262. EXPECT_GT(left_shoulder_elbow, 0.05F);
  263. EXPECT_GT(left_elbow_hand, 0.05F);
  264. EXPECT_LT(left_shoulder_elbow, 0.50F);
  265. EXPECT_LT(left_elbow_hand, 0.50F);
  266. }
  267. TEST_F(MountedPoseControllerTest, AllMethodsHandleEdgeCases) {
  268. MountedPoseController controller(pose, anim_ctx);
  269. // Should not crash with various inputs
  270. EXPECT_NO_THROW(controller.mountOnHorse(mount));
  271. EXPECT_NO_THROW(controller.dismount());
  272. EXPECT_NO_THROW(controller.ridingIdle(mount));
  273. EXPECT_NO_THROW(controller.ridingLeaning(mount, 0.0F, 0.0F));
  274. EXPECT_NO_THROW(controller.ridingCharging(mount, 0.0F));
  275. EXPECT_NO_THROW(controller.ridingReining(mount, 0.0F, 0.0F));
  276. EXPECT_NO_THROW(controller.ridingMeleeStrike(mount, 0.5F));
  277. EXPECT_NO_THROW(controller.ridingSpearThrust(mount, 0.5F));
  278. EXPECT_NO_THROW(controller.ridingBowShot(mount, 0.5F));
  279. EXPECT_NO_THROW(controller.ridingShieldDefense(mount, true));
  280. EXPECT_NO_THROW(controller.holdReins(mount, 0.5F, 0.5F, 0.4F, 0.4F));
  281. EXPECT_NO_THROW(controller.holdSpearMounted(mount, SpearGrip::OVERHAND));
  282. EXPECT_NO_THROW(controller.holdBowMounted(mount));
  283. }
  284. TEST_F(MountedPoseControllerTest, AttackPhaseClamping) {
  285. MountedPoseController controller(pose, anim_ctx);
  286. // Test clamping of attack phase > 1.0
  287. EXPECT_NO_THROW(controller.ridingMeleeStrike(mount, 1.5F));
  288. EXPECT_NO_THROW(controller.ridingSpearThrust(mount, 2.0F));
  289. EXPECT_NO_THROW(controller.ridingBowShot(mount, -0.5F));
  290. }
  291. TEST_F(MountedPoseControllerTest, RidingChargingIntensityClamping) {
  292. MountedPoseController controller(pose, anim_ctx);
  293. controller.ridingCharging(mount, 1.5F);
  294. QVector3D const max_lean = pose.shoulder_l;
  295. // Reset
  296. SetUp();
  297. MountedPoseController controller2(pose, anim_ctx);
  298. controller2.ridingCharging(mount, 1.0F);
  299. // Should be same as clamped 1.5F
  300. EXPECT_TRUE(approxEqual(pose.shoulder_l, max_lean));
  301. }
  302. TEST_F(MountedPoseControllerTest, FullRidingSequence) {
  303. MountedPoseController controller(pose, anim_ctx);
  304. // Simulate a full riding sequence
  305. controller.mountOnHorse(mount);
  306. EXPECT_TRUE(approxEqual(pose.pelvis_pos, mount.seat_position));
  307. controller.ridingIdle(mount);
  308. QVector3D const idle_hands = pose.hand_l;
  309. controller.holdReins(mount, 0.5F, 0.5F, 0.3F, 0.3F);
  310. controller.ridingCharging(mount, 1.0F);
  311. controller.ridingSpearThrust(mount, 0.35F);
  312. // Verify animation in progress
  313. EXPECT_GT(pose.hand_r.z(), mount.seat_position.z());
  314. controller.ridingIdle(mount);
  315. controller.dismount();
  316. // Should be back near standing position
  317. EXPECT_NEAR(pose.pelvis_pos.y(), HumanProportions::WAIST_Y, 0.01F);
  318. }