WalkmeshMovement.cpp 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. /*
  2. * Copyright (C) 2022 The V-Gears Team
  3. *
  4. * This file is part of V-Gears
  5. *
  6. * V-Gears is free software: you can redistribute it and/or modify it under
  7. * terms of the GNU General Public License as published by the Free Software
  8. * Foundation, version 3.0 (GPLv3) of the License.
  9. *
  10. * V-Gears is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. */
  15. #include <boost/test/unit_test.hpp>
  16. #include "engine/entity/WalkmeshMovement.h"
  17. BOOST_AUTO_TEST_CASE(TestWalkmeshMovementScriptedWalksAreNotStoppedByTheWalkmeshEdge){
  18. // The player and the characters that wander are stopped.
  19. BOOST_CHECK(WalkmeshMovement::EdgeBlocks(true, false));
  20. BOOST_CHECK(WalkmeshMovement::EdgeBlocks(false, false));
  21. // Even if a script moves the player, the player stays on the walkmesh.
  22. BOOST_CHECK(WalkmeshMovement::EdgeBlocks(true, true));
  23. // A character a script walks to a position is not.
  24. BOOST_CHECK(!WalkmeshMovement::EdgeBlocks(false, true));
  25. }
  26. BOOST_AUTO_TEST_CASE(TestWalkmeshMovementScriptedSteps){
  27. BOOST_CHECK_CLOSE(WalkmeshMovement::ScriptedStep(0.5f, 0.0f, 0.1f), 0.1f, 0.01f);
  28. BOOST_CHECK_CLOSE(WalkmeshMovement::ScriptedStep(0.05f, 0.0f, 0.1f), 0.05f, 0.01f);
  29. BOOST_CHECK_CLOSE(WalkmeshMovement::ScriptedStep(0.55f, 0.5f, 0.1f), 0.05f, 0.01f);
  30. BOOST_CHECK_EQUAL(WalkmeshMovement::ScriptedStep(0.4f, 0.5f, 0.1f), 0.0f);
  31. BOOST_CHECK_EQUAL(WalkmeshMovement::ScriptedStep(1.0f, 0.0f, 0.0f), 0.0f);
  32. for (float frame_time : {1.0f / 120.0f, 1.0f / 60.0f, 1.0f / 30.0f}){
  33. float distance = 2.0f;
  34. for (int step = 0; step < 1000 && distance > 0.0001f; ++step){
  35. distance -= WalkmeshMovement::ScriptedStep(distance, 0.0f, 6.0f * frame_time);
  36. }
  37. BOOST_CHECK_SMALL(distance, 0.0001f);
  38. }
  39. }
  40. BOOST_AUTO_TEST_CASE(TestWalkmeshMovementHeightOfAPointOfATriangle){
  41. // A slope that goes up one unit for each two units along X.
  42. const Ogre::Vector3 a(0.0f, 0.0f, 0.0f);
  43. const Ogre::Vector3 b(2.0f, 0.0f, 1.0f);
  44. const Ogre::Vector3 c(0.0f, 2.0f, 0.0f);
  45. BOOST_CHECK_CLOSE(
  46. WalkmeshMovement::PointElevation(Ogre::Vector2(1.0f, 0.5f), a, b, c), 0.5f, 0.01f
  47. );
  48. BOOST_CHECK_CLOSE(
  49. WalkmeshMovement::PointElevation(Ogre::Vector2(2.0f, 0.0f), a, b, c), 1.0f, 0.01f
  50. );
  51. }
  52. BOOST_AUTO_TEST_CASE(TestWalkmeshMovementSideOfALine){
  53. const Ogre::Vector2 start(0.0f, 0.0f);
  54. const Ogre::Vector2 end(1.0f, 0.0f);
  55. BOOST_CHECK(WalkmeshMovement::SideOfVector(Ogre::Vector2(0.5f, 1.0f), start, end) > 0.0f);
  56. BOOST_CHECK(WalkmeshMovement::SideOfVector(Ogre::Vector2(0.5f, -1.0f), start, end) < 0.0f);
  57. BOOST_CHECK_EQUAL(
  58. WalkmeshMovement::SideOfVector(Ogre::Vector2(3.0f, 0.0f), start, end), 0.0f
  59. );
  60. }
  61. /**
  62. * Steers a step that goes along X.
  63. *
  64. * @return The angle in degrees that the step has been turned, or 1000 if the entity is blocked.
  65. */
  66. static float SteerAngle(
  67. const bool is_player, const bool slip, const WalkmeshMovement::Obstacles& obstacles
  68. ){
  69. Ogre::Vector2 direction(1.0f, 0.0f);
  70. if (!WalkmeshMovement::Steer(is_player, slip, obstacles, direction)) return 1000.0f;
  71. return Ogre::Math::ATan2(direction.y, direction.x).valueDegrees();
  72. }
  73. BOOST_AUTO_TEST_CASE(TestWalkmeshMovementThePlayerSlidesAlongWalls){
  74. WalkmeshMovement::Obstacles left;
  75. left.left_wall = true;
  76. BOOST_CHECK_CLOSE(SteerAngle(true, true, left), -11.25f, 0.01f);
  77. WalkmeshMovement::Obstacles right;
  78. right.right_wall = true;
  79. BOOST_CHECK_CLOSE(SteerAngle(true, true, right), 11.25f, 0.01f);
  80. // Not with SLIP off, nor in a corner.
  81. BOOST_CHECK_EQUAL(SteerAngle(true, false, left), 1000.0f);
  82. WalkmeshMovement::Obstacles corner = left;
  83. corner.right_wall = true;
  84. BOOST_CHECK_EQUAL(SteerAngle(true, true, corner), 1000.0f);
  85. // The player doesn't go around other entities.
  86. WalkmeshMovement::Obstacles somebody = left;
  87. somebody.ahead_entity = true;
  88. BOOST_CHECK_EQUAL(SteerAngle(true, true, somebody), 1000.0f);
  89. }
  90. BOOST_AUTO_TEST_CASE(TestWalkmeshMovementCharactersGoAroundObstacles){
  91. WalkmeshMovement::Obstacles wall_ahead;
  92. wall_ahead.ahead_wall = true;
  93. BOOST_CHECK_CLOSE(SteerAngle(false, true, wall_ahead), -7.0f, 0.01f);
  94. // Somebody ahead: it turns to one side. Somebody to the left or right too.
  95. WalkmeshMovement::Obstacles ahead;
  96. ahead.ahead_entity = true;
  97. BOOST_CHECK_CLOSE(SteerAngle(false, true, ahead), -11.25f, 0.01f);
  98. WalkmeshMovement::Obstacles left;
  99. left.left_entity = true;
  100. BOOST_CHECK_CLOSE(SteerAngle(false, true, left), -11.25f, 0.01f);
  101. WalkmeshMovement::Obstacles right;
  102. right.right_entity = true;
  103. BOOST_CHECK_CLOSE(SteerAngle(false, true, right), 11.25f, 0.01f);
  104. // Walls on both sides block it.
  105. WalkmeshMovement::Obstacles corner;
  106. corner.left_wall = true;
  107. corner.right_wall = true;
  108. BOOST_CHECK_EQUAL(SteerAngle(false, true, corner), 1000.0f);
  109. }