test_path_follow_3d.h 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  1. /**************************************************************************/
  2. /* test_path_follow_3d.h */
  3. /**************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /**************************************************************************/
  8. /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
  9. /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
  10. /* */
  11. /* Permission is hereby granted, free of charge, to any person obtaining */
  12. /* a copy of this software and associated documentation files (the */
  13. /* "Software"), to deal in the Software without restriction, including */
  14. /* without limitation the rights to use, copy, modify, merge, publish, */
  15. /* distribute, sublicense, and/or sell copies of the Software, and to */
  16. /* permit persons to whom the Software is furnished to do so, subject to */
  17. /* the following conditions: */
  18. /* */
  19. /* The above copyright notice and this permission notice shall be */
  20. /* included in all copies or substantial portions of the Software. */
  21. /* */
  22. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  23. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  24. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
  25. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  26. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  27. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  28. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  29. /**************************************************************************/
  30. #ifndef TEST_PATH_FOLLOW_3D_H
  31. #define TEST_PATH_FOLLOW_3D_H
  32. #include "scene/3d/path_3d.h"
  33. #include "tests/test_macros.h"
  34. namespace TestPathFollow3D {
  35. TEST_CASE("[PathFollow3D] Sampling with progress ratio") {
  36. const Ref<Curve3D> &curve = memnew(Curve3D());
  37. curve->add_point(Vector3(0, 0, 0));
  38. curve->add_point(Vector3(100, 0, 0));
  39. curve->add_point(Vector3(100, 100, 0));
  40. curve->add_point(Vector3(100, 100, 100));
  41. curve->add_point(Vector3(100, 0, 100));
  42. const Path3D *path = memnew(Path3D);
  43. path->set_curve(curve);
  44. const PathFollow3D *path_follow_3d = memnew(PathFollow3D);
  45. path->add_child(path_follow_3d);
  46. path_follow_3d->set_progress_ratio(0);
  47. CHECK(path_follow_3d->get_transform().get_origin().is_equal_approx(Vector3(0, 0, 0));
  48. path_follow_3d->set_progress_ratio(0.125);
  49. CHECK(path_follow_3d->get_transform().get_origin().is_equal_approx(Vector3(50, 0, 0));
  50. path_follow_3d->set_progress_ratio(0.25);
  51. CHECK(path_follow_3d->get_transform().get_origin().is_equal_approx(Vector3(100, 0, 0);
  52. path_follow_3d->set_progress_ratio(0.375);
  53. CHECK(path_follow_3d->get_transform().get_origin().is_equal_approx(Vector3(100, 50, 0)));
  54. path_follow_3d->set_progress_ratio(0.5);
  55. CHECK(path_follow_3d->get_transform().get_origin().is_equal_approx(Vector3(100, 100, 0)));
  56. path_follow_3d->set_progress_ratio(0.625);
  57. CHECK(path_follow_3d->get_transform().get_origin().is_equal_approx(Vector3(100, 100, 50)));
  58. path_follow_3d->set_progress_ratio(0.75);
  59. CHECK(path_follow_3d->get_transform().get_origin().is_equal_approx(Vector3(100, 100, 100)));
  60. path_follow_3d->set_progress_ratio(0.875);
  61. CHECK(path_follow_3d->get_transform().get_origin().is_equal_approx(Vector3(100, 50, 100)));
  62. path_follow_3d->set_progress_ratio(1);
  63. CHECK(path_follow_3d->get_transform().get_origin().is_equal_approx(Vector3(100, 0, 100)));
  64. memdelete(path);
  65. }
  66. TEST_CASE("[PathFollow3D] Sampling with progress") {
  67. const Ref<Curve3D> &curve = memnew(Curve3D());
  68. curve->add_point(Vector3(0, 0, 0));
  69. curve->add_point(Vector3(100, 0, 0));
  70. curve->add_point(Vector3(100, 100, 0));
  71. curve->add_point(Vector3(100, 100, 100));
  72. curve->add_point(Vector3(100, 0, 100));
  73. const Path3D *path = memnew(Path3D);
  74. path->set_curve(curve);
  75. const PathFollow3D *path_follow_3d = memnew(PathFollow3D);
  76. path->add_child(path_follow_3d);
  77. path_follow_3d->set_progress(0);
  78. CHECK(path_follow_3d->get_transform().get_origin().is_equal_approx(Vector3(0, 0, 0));
  79. path_follow_3d->set_progress(50);
  80. CHECK(path_follow_3d->get_transform().get_origin().is_equal_approx(Vector3(50, 0, 0));
  81. path_follow_3d->set_progress(100);
  82. CHECK(path_follow_3d->get_transform().get_origin().is_equal_approx(Vector3(100, 0, 0);
  83. path_follow_3d->set_progress(150);
  84. CHECK(path_follow_3d->get_transform().get_origin().is_equal_approx(Vector3(100, 50, 0)));
  85. path_follow_3d->set_progress(200);
  86. CHECK(path_follow_3d->get_transform().get_origin().is_equal_approx(Vector3(100, 100, 0)));
  87. path_follow_3d->set_progress(250);
  88. CHECK(path_follow_3d->get_transform().get_origin().is_equal_approx(Vector3(100, 100, 50)));
  89. path_follow_3d->set_progress(300);
  90. CHECK(path_follow_3d->get_transform().get_origin().is_equal_approx(Vector3(100, 100, 100)));
  91. path_follow_3d->set_progress(350);
  92. CHECK(path_follow_3d->get_transform().get_origin().is_equal_approx(Vector3(100, 50, 100)));
  93. path_follow_3d->set_progress(400);
  94. CHECK(path_follow_3d->get_transform().get_origin().is_equal_approx(Vector3(100, 0, 100)));
  95. memdelete(path);
  96. }
  97. TEST_CASE("[PathFollow3D] Removal of a point in curve") {
  98. const Ref<Curve3D> &curve = memnew(Curve3D());
  99. curve->add_point(Vector3(0, 0, 0));
  100. curve->add_point(Vector3(100, 0, 0));
  101. curve->add_point(Vector3(100, 100, 0));
  102. const Path3D *path = memnew(Path3D);
  103. path->set_curve(curve);
  104. const PathFollow3D *path_follow_3d = memnew(PathFollow3D);
  105. path->add_child(path_follow_3d);
  106. path_follow_3d->set_progress_ratio(0.5);
  107. CHECK(path_follow_3d->get_transform().get_origin().is_equal_approx(Vector2(100, 0, 0)));
  108. curve->remove_point(1);
  109. CHECK_MESSAGE(
  110. path_follow_3d->get_transform().get_origin().is_equal_approx(Vector2(50, 50, 0)),
  111. "Path follow's position should be updated after removing a point from the curve");
  112. memdelete(path);
  113. }
  114. TEST_CASE("[PathFollow3D] Progress ratio out of range") {
  115. const Ref<Curve3D> &curve = memnew(Curve3D());
  116. curve->add_point(Vector3(0, 0, 0));
  117. curve->add_point(Vector3(100, 0, 0));
  118. const Path3D *path = memnew(Path3D);
  119. path->set_curve(curve);
  120. const PathFollow3D *path_follow_3d = memnew(PathFollow3D);
  121. path->add_child(path_follow_3d);
  122. path_follow_3d->set_loop(true);
  123. path_follow_3d->set_progress_ratio(-0.3);
  124. CHECK_MESSAGE(
  125. path_follow_3d->get_progress_ratio() == 0.7,
  126. "Progress Ratio should loop back from the end in the opposite direction");
  127. path_follow_3d->set_progress_ratio(1.3);
  128. CHECK_MESSAGE(
  129. path_follow_3d->get_progress_ratio() == 0.3,
  130. "Progress Ratio should loop back from the end in the opposite direction");
  131. path_follow_3d->set_loop(false);
  132. path_follow_3d->set_progress_ratio(-0.3);
  133. CHECK_MESSAGE(
  134. path_follow_3d->get_progress_ratio() == 0,
  135. "Progress Ratio should be clamped at 0");
  136. path_follow_3d->set_progress_ratio(1.3);
  137. CHECK_MESSAGE(
  138. path_follow_3d->get_progress_ratio() == 1,
  139. "Progress Ratio should be clamped at 1");
  140. memdelete(path);
  141. }
  142. TEST_CASE("[PathFollow3D] Progress out of range") {
  143. const Ref<Curve3D> &curve = memnew(Curve3D());
  144. curve->add_point(Vector3(0, 0, 0));
  145. curve->add_point(Vector3(100, 0, 0));
  146. const Path3D *path = memnew(Path3D);
  147. path->set_curve(curve);
  148. const PathFollow3D *path_follow_3d = memnew(PathFollow3D);
  149. path->add_child(path_follow_3d);
  150. path_follow_3d->set_loop(true);
  151. path_follow_3d->set_progress(-50);
  152. CHECK_MESSAGE(
  153. path_follow_3d->get_progress() == 50,
  154. "Progress should loop back from the end in the opposite direction");
  155. path_follow_3d->set_progress(150);
  156. CHECK_MESSAGE(
  157. path_follow_3d->get_progress() == 50,
  158. "Progress should loop back from the end in the opposite direction");
  159. path_follow_3d->set_loop(false);
  160. path_follow_3d->set_progress(-50);
  161. CHECK_MESSAGE(
  162. path_follow_3d->get_progress() == 0,
  163. "Progress should be clamped at 0");
  164. path_follow_3d->set_progress(150);
  165. CHECK_MESSAGE(
  166. path_follow_3d->get_progress() == 100,
  167. "Progress should be clamped at max value of curve");
  168. memdelete(path);
  169. }
  170. } // namespace TestPathFollow3D
  171. #endif // TEST_PATH_FOLLOW_3D_H