Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
godotengine
GitHub Repository: godotengine/godot
Path: blob/master/tests/scene/test_animation.h
10277 views
1
/**************************************************************************/
2
/* test_animation.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/resources/animation.h"
34
35
#include "tests/test_macros.h"
36
37
namespace TestAnimation {
38
39
TEST_CASE("[Animation] Empty animation getters") {
40
const Ref<Animation> animation = memnew(Animation);
41
42
CHECK(animation->get_length() == doctest::Approx(real_t(1.0)));
43
CHECK(animation->get_step() == doctest::Approx(real_t(1.0 / 30)));
44
}
45
46
TEST_CASE("[Animation] Create value track") {
47
// This creates an animation that makes the node "Enemy" move to the right by
48
// 100 pixels in 0.5 seconds.
49
Ref<Animation> animation = memnew(Animation);
50
const int track_index = animation->add_track(Animation::TYPE_VALUE);
51
CHECK(track_index == 0);
52
animation->track_set_path(track_index, NodePath("Enemy:position:x"));
53
animation->track_insert_key(track_index, 0.0, 0);
54
animation->track_insert_key(track_index, 0.5, 100);
55
56
CHECK(animation->get_track_count() == 1);
57
CHECK(!animation->track_is_compressed(0));
58
CHECK(int(animation->track_get_key_value(0, 0)) == 0);
59
CHECK(int(animation->track_get_key_value(0, 1)) == 100);
60
61
CHECK(animation->value_track_interpolate(0, -0.2) == doctest::Approx(0.0));
62
CHECK(animation->value_track_interpolate(0, 0.0) == doctest::Approx(0.0));
63
CHECK(animation->value_track_interpolate(0, 0.2) == doctest::Approx(40.0));
64
CHECK(animation->value_track_interpolate(0, 0.4) == doctest::Approx(80.0));
65
CHECK(animation->value_track_interpolate(0, 0.5) == doctest::Approx(100.0));
66
CHECK(animation->value_track_interpolate(0, 0.6) == doctest::Approx(100.0));
67
68
CHECK(animation->track_get_key_transition(0, 0) == doctest::Approx(real_t(1.0)));
69
CHECK(animation->track_get_key_transition(0, 1) == doctest::Approx(real_t(1.0)));
70
71
ERR_PRINT_OFF;
72
// Nonexistent keys.
73
CHECK(animation->track_get_key_value(0, 2).is_null());
74
CHECK(animation->track_get_key_value(0, -1).is_null());
75
CHECK(animation->track_get_key_transition(0, 2) == doctest::Approx(real_t(-1.0)));
76
// Nonexistent track (and keys).
77
CHECK(animation->track_get_key_value(1, 0).is_null());
78
CHECK(animation->track_get_key_value(1, 1).is_null());
79
CHECK(animation->track_get_key_value(1, 2).is_null());
80
CHECK(animation->track_get_key_value(1, -1).is_null());
81
CHECK(animation->track_get_key_transition(1, 0) == doctest::Approx(real_t(-1.0)));
82
83
// This is a value track, so the methods below should return errors.
84
CHECK(animation->try_position_track_interpolate(0, 0.0, nullptr) == ERR_INVALID_PARAMETER);
85
CHECK(animation->try_rotation_track_interpolate(0, 0.0, nullptr) == ERR_INVALID_PARAMETER);
86
CHECK(animation->try_scale_track_interpolate(0, 0.0, nullptr) == ERR_INVALID_PARAMETER);
87
CHECK(animation->bezier_track_interpolate(0, 0.0) == doctest::Approx(0.0));
88
CHECK(animation->try_blend_shape_track_interpolate(0, 0.0, nullptr) == ERR_INVALID_PARAMETER);
89
ERR_PRINT_ON;
90
}
91
92
TEST_CASE("[Animation] Create 3D position track") {
93
Ref<Animation> animation = memnew(Animation);
94
const int track_index = animation->add_track(Animation::TYPE_POSITION_3D);
95
animation->track_set_path(track_index, NodePath("Enemy:position"));
96
animation->position_track_insert_key(track_index, 0.0, Vector3(0, 1, 2));
97
animation->position_track_insert_key(track_index, 0.5, Vector3(3.5, 4, 5));
98
99
CHECK(animation->get_track_count() == 1);
100
CHECK(!animation->track_is_compressed(0));
101
CHECK(Vector3(animation->track_get_key_value(0, 0)).is_equal_approx(Vector3(0, 1, 2)));
102
CHECK(Vector3(animation->track_get_key_value(0, 1)).is_equal_approx(Vector3(3.5, 4, 5)));
103
104
Vector3 r_interpolation;
105
106
CHECK(animation->try_position_track_interpolate(0, -0.2, &r_interpolation) == OK);
107
CHECK(r_interpolation.is_equal_approx(Vector3(0, 1, 2)));
108
109
CHECK(animation->try_position_track_interpolate(0, 0.0, &r_interpolation) == OK);
110
CHECK(r_interpolation.is_equal_approx(Vector3(0, 1, 2)));
111
112
CHECK(animation->try_position_track_interpolate(0, 0.2, &r_interpolation) == OK);
113
CHECK(r_interpolation.is_equal_approx(Vector3(1.4, 2.2, 3.2)));
114
115
CHECK(animation->try_position_track_interpolate(0, 0.4, &r_interpolation) == OK);
116
CHECK(r_interpolation.is_equal_approx(Vector3(2.8, 3.4, 4.4)));
117
118
CHECK(animation->try_position_track_interpolate(0, 0.5, &r_interpolation) == OK);
119
CHECK(r_interpolation.is_equal_approx(Vector3(3.5, 4, 5)));
120
121
CHECK(animation->try_position_track_interpolate(0, 0.6, &r_interpolation) == OK);
122
CHECK(r_interpolation.is_equal_approx(Vector3(3.5, 4, 5)));
123
124
// 3D position tracks always use linear interpolation for performance reasons.
125
CHECK(animation->track_get_key_transition(0, 0) == doctest::Approx(real_t(1.0)));
126
CHECK(animation->track_get_key_transition(0, 1) == doctest::Approx(real_t(1.0)));
127
128
// This is a 3D position track, so the methods below should return errors.
129
ERR_PRINT_OFF;
130
CHECK(animation->value_track_interpolate(0, 0.0).is_null());
131
CHECK(animation->try_rotation_track_interpolate(0, 0.0, nullptr) == ERR_INVALID_PARAMETER);
132
CHECK(animation->try_scale_track_interpolate(0, 0.0, nullptr) == ERR_INVALID_PARAMETER);
133
CHECK(animation->bezier_track_interpolate(0, 0.0) == doctest::Approx(0.0));
134
CHECK(animation->try_blend_shape_track_interpolate(0, 0.0, nullptr) == ERR_INVALID_PARAMETER);
135
ERR_PRINT_ON;
136
}
137
138
TEST_CASE("[Animation] Create 3D rotation track") {
139
Ref<Animation> animation = memnew(Animation);
140
const int track_index = animation->add_track(Animation::TYPE_ROTATION_3D);
141
animation->track_set_path(track_index, NodePath("Enemy:rotation"));
142
animation->rotation_track_insert_key(track_index, 0.0, Quaternion::from_euler(Vector3(0, 1, 2)));
143
animation->rotation_track_insert_key(track_index, 0.5, Quaternion::from_euler(Vector3(3.5, 4, 5)));
144
145
CHECK(animation->get_track_count() == 1);
146
CHECK(!animation->track_is_compressed(0));
147
CHECK(Quaternion(animation->track_get_key_value(0, 0)).is_equal_approx(Quaternion::from_euler(Vector3(0, 1, 2))));
148
CHECK(Quaternion(animation->track_get_key_value(0, 1)).is_equal_approx(Quaternion::from_euler(Vector3(3.5, 4, 5))));
149
150
Quaternion r_interpolation;
151
152
CHECK(animation->try_rotation_track_interpolate(0, -0.2, &r_interpolation) == OK);
153
CHECK(r_interpolation.is_equal_approx(Quaternion(0.403423, 0.259035, 0.73846, 0.47416)));
154
155
CHECK(animation->try_rotation_track_interpolate(0, 0.0, &r_interpolation) == OK);
156
CHECK(r_interpolation.is_equal_approx(Quaternion(0.403423, 0.259035, 0.73846, 0.47416)));
157
158
CHECK(animation->try_rotation_track_interpolate(0, 0.2, &r_interpolation) == OK);
159
CHECK(r_interpolation.is_equal_approx(Quaternion(0.336182, 0.30704, 0.751515, 0.477425)));
160
161
CHECK(animation->try_rotation_track_interpolate(0, 0.4, &r_interpolation) == OK);
162
CHECK(r_interpolation.is_equal_approx(Quaternion(0.266585, 0.352893, 0.759303, 0.477344)));
163
164
CHECK(animation->try_rotation_track_interpolate(0, 0.5, &r_interpolation) == OK);
165
CHECK(r_interpolation.is_equal_approx(Quaternion(0.231055, 0.374912, 0.761204, 0.476048)));
166
167
CHECK(animation->try_rotation_track_interpolate(0, 0.6, &r_interpolation) == OK);
168
CHECK(r_interpolation.is_equal_approx(Quaternion(0.231055, 0.374912, 0.761204, 0.476048)));
169
170
// 3D rotation tracks always use linear interpolation for performance reasons.
171
CHECK(animation->track_get_key_transition(0, 0) == doctest::Approx(real_t(1.0)));
172
CHECK(animation->track_get_key_transition(0, 1) == doctest::Approx(real_t(1.0)));
173
174
// This is a 3D rotation track, so the methods below should return errors.
175
ERR_PRINT_OFF;
176
CHECK(animation->value_track_interpolate(0, 0.0).is_null());
177
CHECK(animation->try_position_track_interpolate(0, 0.0, nullptr) == ERR_INVALID_PARAMETER);
178
CHECK(animation->try_scale_track_interpolate(0, 0.0, nullptr) == ERR_INVALID_PARAMETER);
179
CHECK(animation->bezier_track_interpolate(0, 0.0) == doctest::Approx(real_t(0.0)));
180
CHECK(animation->try_blend_shape_track_interpolate(0, 0.0, nullptr) == ERR_INVALID_PARAMETER);
181
ERR_PRINT_ON;
182
}
183
184
TEST_CASE("[Animation] Create 3D scale track") {
185
Ref<Animation> animation = memnew(Animation);
186
const int track_index = animation->add_track(Animation::TYPE_SCALE_3D);
187
animation->track_set_path(track_index, NodePath("Enemy:scale"));
188
animation->scale_track_insert_key(track_index, 0.0, Vector3(0, 1, 2));
189
animation->scale_track_insert_key(track_index, 0.5, Vector3(3.5, 4, 5));
190
191
CHECK(animation->get_track_count() == 1);
192
CHECK(!animation->track_is_compressed(0));
193
CHECK(Vector3(animation->track_get_key_value(0, 0)).is_equal_approx(Vector3(0, 1, 2)));
194
CHECK(Vector3(animation->track_get_key_value(0, 1)).is_equal_approx(Vector3(3.5, 4, 5)));
195
196
Vector3 r_interpolation;
197
198
CHECK(animation->try_scale_track_interpolate(0, -0.2, &r_interpolation) == OK);
199
CHECK(r_interpolation.is_equal_approx(Vector3(0, 1, 2)));
200
201
CHECK(animation->try_scale_track_interpolate(0, 0.0, &r_interpolation) == OK);
202
CHECK(r_interpolation.is_equal_approx(Vector3(0, 1, 2)));
203
204
CHECK(animation->try_scale_track_interpolate(0, 0.2, &r_interpolation) == OK);
205
CHECK(r_interpolation.is_equal_approx(Vector3(1.4, 2.2, 3.2)));
206
207
CHECK(animation->try_scale_track_interpolate(0, 0.4, &r_interpolation) == OK);
208
CHECK(r_interpolation.is_equal_approx(Vector3(2.8, 3.4, 4.4)));
209
210
CHECK(animation->try_scale_track_interpolate(0, 0.5, &r_interpolation) == OK);
211
CHECK(r_interpolation.is_equal_approx(Vector3(3.5, 4, 5)));
212
213
CHECK(animation->try_scale_track_interpolate(0, 0.6, &r_interpolation) == OK);
214
CHECK(r_interpolation.is_equal_approx(Vector3(3.5, 4, 5)));
215
216
// 3D scale tracks always use linear interpolation for performance reasons.
217
CHECK(animation->track_get_key_transition(0, 0) == doctest::Approx(1.0));
218
CHECK(animation->track_get_key_transition(0, 1) == doctest::Approx(1.0));
219
220
// This is a 3D scale track, so the methods below should return errors.
221
ERR_PRINT_OFF;
222
CHECK(animation->value_track_interpolate(0, 0.0).is_null());
223
CHECK(animation->try_position_track_interpolate(0, 0.0, nullptr) == ERR_INVALID_PARAMETER);
224
CHECK(animation->try_rotation_track_interpolate(0, 0.0, nullptr) == ERR_INVALID_PARAMETER);
225
CHECK(animation->bezier_track_interpolate(0, 0.0) == doctest::Approx(0.0));
226
CHECK(animation->try_blend_shape_track_interpolate(0, 0.0, nullptr) == ERR_INVALID_PARAMETER);
227
ERR_PRINT_ON;
228
}
229
230
TEST_CASE("[Animation] Create blend shape track") {
231
Ref<Animation> animation = memnew(Animation);
232
const int track_index = animation->add_track(Animation::TYPE_BLEND_SHAPE);
233
animation->track_set_path(track_index, NodePath("Enemy:scale"));
234
// Negative values for blend shapes should work as expected.
235
animation->blend_shape_track_insert_key(track_index, 0.0, -1.0);
236
animation->blend_shape_track_insert_key(track_index, 0.5, 1.0);
237
238
CHECK(animation->get_track_count() == 1);
239
CHECK(!animation->track_is_compressed(0));
240
241
float r_blend = 0.0f;
242
243
CHECK(animation->blend_shape_track_get_key(0, 0, &r_blend) == OK);
244
CHECK(r_blend == doctest::Approx(-1.0f));
245
246
CHECK(animation->blend_shape_track_get_key(0, 1, &r_blend) == OK);
247
CHECK(r_blend == doctest::Approx(1.0f));
248
249
CHECK(animation->try_blend_shape_track_interpolate(0, -0.2, &r_blend) == OK);
250
CHECK(r_blend == doctest::Approx(-1.0f));
251
252
CHECK(animation->try_blend_shape_track_interpolate(0, 0.0, &r_blend) == OK);
253
CHECK(r_blend == doctest::Approx(-1.0f));
254
255
CHECK(animation->try_blend_shape_track_interpolate(0, 0.2, &r_blend) == OK);
256
CHECK(r_blend == doctest::Approx(-0.2f));
257
258
CHECK(animation->try_blend_shape_track_interpolate(0, 0.4, &r_blend) == OK);
259
CHECK(r_blend == doctest::Approx(0.6f));
260
261
CHECK(animation->try_blend_shape_track_interpolate(0, 0.5, &r_blend) == OK);
262
CHECK(r_blend == doctest::Approx(1.0f));
263
264
CHECK(animation->try_blend_shape_track_interpolate(0, 0.6, &r_blend) == OK);
265
CHECK(r_blend == doctest::Approx(1.0f));
266
267
// Blend shape tracks always use linear interpolation for performance reasons.
268
CHECK(animation->track_get_key_transition(0, 0) == doctest::Approx(real_t(1.0)));
269
CHECK(animation->track_get_key_transition(0, 1) == doctest::Approx(real_t(1.0)));
270
271
// This is a blend shape track, so the methods below should return errors.
272
ERR_PRINT_OFF;
273
CHECK(animation->value_track_interpolate(0, 0.0).is_null());
274
CHECK(animation->try_position_track_interpolate(0, 0.0, nullptr) == ERR_INVALID_PARAMETER);
275
CHECK(animation->try_rotation_track_interpolate(0, 0.0, nullptr) == ERR_INVALID_PARAMETER);
276
CHECK(animation->try_scale_track_interpolate(0, 0.0, nullptr) == ERR_INVALID_PARAMETER);
277
CHECK(animation->bezier_track_interpolate(0, 0.0) == doctest::Approx(0.0));
278
ERR_PRINT_ON;
279
}
280
281
TEST_CASE("[Animation] Create Bezier track") {
282
Ref<Animation> animation = memnew(Animation);
283
const int track_index = animation->add_track(Animation::TYPE_BEZIER);
284
animation->track_set_path(track_index, NodePath("Enemy:scale"));
285
animation->bezier_track_insert_key(track_index, 0.0, -1.0, Vector2(-1, -1), Vector2(1, 1));
286
animation->bezier_track_insert_key(track_index, 0.5, 1.0, Vector2(0, 1), Vector2(1, 0.5));
287
288
CHECK(animation->get_track_count() == 1);
289
CHECK(!animation->track_is_compressed(0));
290
291
CHECK(animation->bezier_track_get_key_value(0, 0) == doctest::Approx(real_t(-1.0)));
292
CHECK(animation->bezier_track_get_key_value(0, 1) == doctest::Approx(real_t(1.0)));
293
294
CHECK(animation->bezier_track_interpolate(0, -0.2) == doctest::Approx(real_t(-1.0)));
295
CHECK(animation->bezier_track_interpolate(0, 0.0) == doctest::Approx(real_t(-1.0)));
296
CHECK(animation->bezier_track_interpolate(0, 0.2) == doctest::Approx(real_t(-0.76057207584381)));
297
CHECK(animation->bezier_track_interpolate(0, 0.4) == doctest::Approx(real_t(-0.39975279569626)));
298
CHECK(animation->bezier_track_interpolate(0, 0.5) == doctest::Approx(real_t(1.0)));
299
CHECK(animation->bezier_track_interpolate(0, 0.6) == doctest::Approx(real_t(1.0)));
300
301
// This is a bezier track, so the methods below should return errors.
302
ERR_PRINT_OFF;
303
CHECK(animation->value_track_interpolate(0, 0.0).is_null());
304
CHECK(animation->try_position_track_interpolate(0, 0.0, nullptr) == ERR_INVALID_PARAMETER);
305
CHECK(animation->try_rotation_track_interpolate(0, 0.0, nullptr) == ERR_INVALID_PARAMETER);
306
CHECK(animation->try_scale_track_interpolate(0, 0.0, nullptr) == ERR_INVALID_PARAMETER);
307
CHECK(animation->try_blend_shape_track_interpolate(0, 0.0, nullptr) == ERR_INVALID_PARAMETER);
308
ERR_PRINT_ON;
309
}
310
311
} // namespace TestAnimation
312
313