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