test_node_2d.h 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  1. /**************************************************************************/
  2. /* test_node_2d.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_NODE_2D_H
  31. #define TEST_NODE_2D_H
  32. #include "scene/2d/node_2d.h"
  33. #include "scene/main/window.h"
  34. #include "tests/test_macros.h"
  35. namespace TestNode2D {
  36. TEST_CASE("[SceneTree][Node2D]") {
  37. SUBCASE("[Node2D][Global Transform] Global Transform should be accessible while not in SceneTree.") { // GH-79453
  38. Node2D *test_node = memnew(Node2D);
  39. test_node->set_name("node");
  40. Node2D *test_child = memnew(Node2D);
  41. test_child->set_name("child");
  42. test_node->add_child(test_child);
  43. test_node->set_global_position(Point2(1, 1));
  44. CHECK_EQ(test_node->get_global_position(), Point2(1, 1));
  45. CHECK_EQ(test_child->get_global_position(), Point2(1, 1));
  46. test_node->set_global_position(Point2(2, 2));
  47. CHECK_EQ(test_node->get_global_position(), Point2(2, 2));
  48. test_node->set_global_transform(Transform2D(0, Point2(3, 3)));
  49. CHECK_EQ(test_node->get_global_position(), Point2(3, 3));
  50. memdelete(test_child);
  51. memdelete(test_node);
  52. }
  53. SUBCASE("[Node2D][Global Transform] Global Transform should be correct after inserting node from detached tree into SceneTree.") { // GH-86841
  54. Node2D *main = memnew(Node2D);
  55. Node2D *outer = memnew(Node2D);
  56. Node2D *inner = memnew(Node2D);
  57. SceneTree::get_singleton()->get_root()->add_child(main);
  58. main->set_position(Point2(100, 100));
  59. outer->set_position(Point2(10, 0));
  60. inner->set_position(Point2(0, 10));
  61. outer->add_child(inner);
  62. // `inner` is still detached.
  63. CHECK_EQ(inner->get_global_position(), Point2(10, 10));
  64. main->add_child(outer);
  65. // `inner` is in scene tree.
  66. CHECK_EQ(inner->get_global_position(), Point2(110, 110));
  67. main->remove_child(outer);
  68. // `inner` is detached again.
  69. CHECK_EQ(inner->get_global_position(), Point2(10, 10));
  70. memdelete(inner);
  71. memdelete(outer);
  72. memdelete(main);
  73. }
  74. }
  75. TEST_CASE("[SceneTree][Node2D] Utility methods") {
  76. Node2D *test_node1 = memnew(Node2D);
  77. Node2D *test_node2 = memnew(Node2D);
  78. Node2D *test_node3 = memnew(Node2D);
  79. Node2D *test_sibling = memnew(Node2D);
  80. SceneTree::get_singleton()->get_root()->add_child(test_node1);
  81. test_node1->set_position(Point2(100, 100));
  82. test_node1->set_rotation(Math::deg_to_rad(30.0));
  83. test_node1->set_scale(Size2(1, 1));
  84. test_node1->add_child(test_node2);
  85. test_node2->set_position(Point2(10, 0));
  86. test_node2->set_rotation(Math::deg_to_rad(60.0));
  87. test_node2->set_scale(Size2(1, 1));
  88. test_node2->add_child(test_node3);
  89. test_node2->add_child(test_sibling);
  90. test_node3->set_position(Point2(0, 10));
  91. test_node3->set_rotation(Math::deg_to_rad(0.0));
  92. test_node3->set_scale(Size2(2, 2));
  93. test_sibling->set_position(Point2(5, 10));
  94. test_sibling->set_rotation(Math::deg_to_rad(90.0));
  95. test_sibling->set_scale(Size2(2, 1));
  96. SUBCASE("[Node2D] look_at") {
  97. test_node3->look_at(Vector2(1, 1));
  98. CHECK(test_node3->get_global_position().is_equal_approx(Point2(98.66026, 105)));
  99. CHECK(Math::is_equal_approx(test_node3->get_global_rotation(), real_t(-2.32477)));
  100. CHECK(test_node3->get_global_scale().is_equal_approx(Vector2(2, 2)));
  101. CHECK(test_node3->get_position().is_equal_approx(Vector2(0, 10)));
  102. CHECK(Math::is_equal_approx(test_node3->get_rotation(), real_t(2.38762)));
  103. CHECK(test_node3->get_scale().is_equal_approx(Vector2(2, 2)));
  104. test_node3->look_at(Vector2(0, 10));
  105. CHECK(test_node3->get_global_position().is_equal_approx(Vector2(98.66026, 105)));
  106. CHECK(Math::is_equal_approx(test_node3->get_global_rotation(), real_t(-2.37509)));
  107. CHECK(test_node3->get_global_scale().is_equal_approx(Vector2(2, 2)));
  108. CHECK(test_node3->get_position().is_equal_approx(Vector2(0, 10)));
  109. CHECK(Math::is_equal_approx(test_node3->get_rotation(), real_t(2.3373)));
  110. CHECK(test_node3->get_scale().is_equal_approx(Vector2(2, 2)));
  111. // Don't do anything if look_at own position.
  112. test_node3->look_at(test_node3->get_global_position());
  113. CHECK(test_node3->get_global_position().is_equal_approx(Vector2(98.66026, 105)));
  114. CHECK(Math::is_equal_approx(test_node3->get_global_rotation(), real_t(-2.37509)));
  115. CHECK(test_node3->get_global_scale().is_equal_approx(Vector2(2, 2)));
  116. CHECK(test_node3->get_position().is_equal_approx(Vector2(0, 10)));
  117. CHECK(Math::is_equal_approx(test_node3->get_rotation(), real_t(2.3373)));
  118. CHECK(test_node3->get_scale().is_equal_approx(Vector2(2, 2)));
  119. // Revert any rotation caused by look_at, must run after look_at tests
  120. test_node3->set_rotation(Math::deg_to_rad(0.0));
  121. }
  122. SUBCASE("[Node2D] get_angle_to") {
  123. CHECK(Math::is_equal_approx(test_node3->get_angle_to(Vector2(1, 1)), real_t(2.38762)));
  124. CHECK(Math::is_equal_approx(test_node3->get_angle_to(Vector2(0, 10)), real_t(2.3373)));
  125. CHECK(Math::is_equal_approx(test_node3->get_angle_to(Vector2(2, -5)), real_t(2.42065)));
  126. CHECK(Math::is_equal_approx(test_node3->get_angle_to(Vector2(-2, 5)), real_t(2.3529)));
  127. // Return 0 when get_angle_to own position.
  128. CHECK(Math::is_equal_approx(test_node3->get_angle_to(test_node3->get_global_position()), real_t(0)));
  129. }
  130. SUBCASE("[Node2D] to_local") {
  131. Point2 node3_local = test_node3->to_local(Point2(1, 2));
  132. CHECK(node3_local.is_equal_approx(Point2(-51.5, 48.83013)));
  133. node3_local = test_node3->to_local(Point2(-2, 1));
  134. CHECK(node3_local.is_equal_approx(Point2(-52, 50.33013)));
  135. node3_local = test_node3->to_local(Point2(0, 0));
  136. CHECK(node3_local.is_equal_approx(Point2(-52.5, 49.33013)));
  137. node3_local = test_node3->to_local(test_node3->get_global_position());
  138. CHECK(node3_local.is_equal_approx(Point2(0, 0)));
  139. }
  140. SUBCASE("[Node2D] to_global") {
  141. Point2 node3_global = test_node3->to_global(Point2(1, 2));
  142. CHECK(node3_global.is_equal_approx(Point2(94.66026, 107)));
  143. node3_global = test_node3->to_global(Point2(-2, 1));
  144. CHECK(node3_global.is_equal_approx(Point2(96.66026, 101)));
  145. node3_global = test_node3->to_global(Point2(0, 0));
  146. CHECK(node3_global.is_equal_approx(test_node3->get_global_position()));
  147. }
  148. SUBCASE("[Node2D] get_relative_transform_to_parent") {
  149. Transform2D relative_xform = test_node3->get_relative_transform_to_parent(test_node3);
  150. CHECK(relative_xform.is_equal_approx(Transform2D()));
  151. relative_xform = test_node3->get_relative_transform_to_parent(test_node2);
  152. CHECK(relative_xform.get_origin().is_equal_approx(Vector2(0, 10)));
  153. CHECK(Math::is_equal_approx(relative_xform.get_rotation(), real_t(0)));
  154. CHECK(relative_xform.get_scale().is_equal_approx(Vector2(2, 2)));
  155. relative_xform = test_node3->get_relative_transform_to_parent(test_node1);
  156. CHECK(relative_xform.get_origin().is_equal_approx(Vector2(1.339746, 5)));
  157. CHECK(Math::is_equal_approx(relative_xform.get_rotation(), real_t(1.0472)));
  158. CHECK(relative_xform.get_scale().is_equal_approx(Vector2(2, 2)));
  159. ERR_PRINT_OFF;
  160. // In case of a sibling all transforms until the root are accumulated.
  161. Transform2D xform = test_node3->get_relative_transform_to_parent(test_sibling);
  162. Transform2D return_xform = test_node1->get_global_transform().inverse() * test_node3->get_global_transform();
  163. CHECK(xform.is_equal_approx(return_xform));
  164. ERR_PRINT_ON;
  165. }
  166. memdelete(test_sibling);
  167. memdelete(test_node3);
  168. memdelete(test_node2);
  169. memdelete(test_node1);
  170. }
  171. } // namespace TestNode2D
  172. #endif // TEST_NODE_2D_H