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