Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
godotengine
GitHub Repository: godotengine/godot
Path: blob/master/scene/animation/easing_equations.h
9896 views
1
/**************************************************************************/
2
/* easing_equations.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 "core/math/math_funcs.h"
34
35
/*
36
* Derived from Robert Penner's easing equations: http://robertpenner.com/easing/
37
*
38
* Copyright (c) 2001 Robert Penner
39
*
40
* Permission is hereby granted, free of charge, to any person obtaining a copy
41
* of this software and associated documentation files (the "Software"), to deal
42
* in the Software without restriction, including without limitation the rights
43
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
44
* copies of the Software, and to permit persons to whom the Software is
45
* furnished to do so, subject to the following conditions:
46
*
47
* The above copyright notice and this permission notice shall be included in all
48
* copies or substantial portions of the Software.
49
*
50
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
51
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
52
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
53
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
54
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
55
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
56
* SOFTWARE.
57
*/
58
59
namespace Linear {
60
static real_t in(real_t t, real_t b, real_t c, real_t d) {
61
return c * t / d + b;
62
}
63
}; // namespace Linear
64
65
namespace Sine {
66
static real_t in(real_t t, real_t b, real_t c, real_t d) {
67
return -c * std::cos(t / d * (Math::PI / 2)) + c + b;
68
}
69
70
static real_t out(real_t t, real_t b, real_t c, real_t d) {
71
return c * std::sin(t / d * (Math::PI / 2)) + b;
72
}
73
74
static real_t in_out(real_t t, real_t b, real_t c, real_t d) {
75
return -c / 2 * (std::cos(Math::PI * t / d) - 1) + b;
76
}
77
78
static real_t out_in(real_t t, real_t b, real_t c, real_t d) {
79
if (t < d / 2) {
80
return out(t * 2, b, c / 2, d);
81
}
82
real_t h = c / 2;
83
return in(t * 2 - d, b + h, h, d);
84
}
85
}; // namespace Sine
86
87
namespace Quint {
88
static real_t in(real_t t, real_t b, real_t c, real_t d) {
89
return c * std::pow(t / d, 5) + b;
90
}
91
92
static real_t out(real_t t, real_t b, real_t c, real_t d) {
93
return c * (std::pow(t / d - 1, 5) + 1) + b;
94
}
95
96
static real_t in_out(real_t t, real_t b, real_t c, real_t d) {
97
t = t / d * 2;
98
99
if (t < 1) {
100
return c / 2 * std::pow(t, 5) + b;
101
}
102
return c / 2 * (std::pow(t - 2, 5) + 2) + b;
103
}
104
105
static real_t out_in(real_t t, real_t b, real_t c, real_t d) {
106
if (t < d / 2) {
107
return out(t * 2, b, c / 2, d);
108
}
109
real_t h = c / 2;
110
return in(t * 2 - d, b + h, h, d);
111
}
112
}; // namespace Quint
113
114
namespace Quart {
115
static real_t in(real_t t, real_t b, real_t c, real_t d) {
116
return c * std::pow(t / d, 4) + b;
117
}
118
119
static real_t out(real_t t, real_t b, real_t c, real_t d) {
120
return -c * (std::pow(t / d - 1, 4) - 1) + b;
121
}
122
123
static real_t in_out(real_t t, real_t b, real_t c, real_t d) {
124
t = t / d * 2;
125
126
if (t < 1) {
127
return c / 2 * std::pow(t, 4) + b;
128
}
129
return -c / 2 * (std::pow(t - 2, 4) - 2) + b;
130
}
131
132
static real_t out_in(real_t t, real_t b, real_t c, real_t d) {
133
if (t < d / 2) {
134
return out(t * 2, b, c / 2, d);
135
}
136
real_t h = c / 2;
137
return in(t * 2 - d, b + h, h, d);
138
}
139
}; // namespace Quart
140
141
namespace Quad {
142
static real_t in(real_t t, real_t b, real_t c, real_t d) {
143
return c * std::pow(t / d, 2) + b;
144
}
145
146
static real_t out(real_t t, real_t b, real_t c, real_t d) {
147
t /= d;
148
return -c * t * (t - 2) + b;
149
}
150
151
static real_t in_out(real_t t, real_t b, real_t c, real_t d) {
152
t = t / d * 2;
153
154
if (t < 1) {
155
return c / 2 * std::pow(t, 2) + b;
156
}
157
return -c / 2 * ((t - 1) * (t - 3) - 1) + b;
158
}
159
160
static real_t out_in(real_t t, real_t b, real_t c, real_t d) {
161
if (t < d / 2) {
162
return out(t * 2, b, c / 2, d);
163
}
164
real_t h = c / 2;
165
return in(t * 2 - d, b + h, h, d);
166
}
167
}; // namespace Quad
168
169
namespace Expo {
170
static real_t in(real_t t, real_t b, real_t c, real_t d) {
171
if (t == 0) {
172
return b;
173
}
174
return c * std::pow(2, 10 * (t / d - 1)) + b - c * 0.001;
175
}
176
177
static real_t out(real_t t, real_t b, real_t c, real_t d) {
178
if (t == d) {
179
return b + c;
180
}
181
return c * 1.001 * (-std::pow(2, -10 * t / d) + 1) + b;
182
}
183
184
static real_t in_out(real_t t, real_t b, real_t c, real_t d) {
185
if (t == 0) {
186
return b;
187
}
188
189
if (t == d) {
190
return b + c;
191
}
192
193
t = t / d * 2;
194
195
if (t < 1) {
196
return c / 2 * std::pow(2, 10 * (t - 1)) + b - c * 0.0005;
197
}
198
return c / 2 * 1.0005 * (-std::pow(2, -10 * (t - 1)) + 2) + b;
199
}
200
201
static real_t out_in(real_t t, real_t b, real_t c, real_t d) {
202
if (t < d / 2) {
203
return out(t * 2, b, c / 2, d);
204
}
205
real_t h = c / 2;
206
return in(t * 2 - d, b + h, h, d);
207
}
208
}; // namespace Expo
209
210
namespace Elastic {
211
static real_t in(real_t t, real_t b, real_t c, real_t d) {
212
if (t == 0) {
213
return b;
214
}
215
216
t /= d;
217
if (t == 1) {
218
return b + c;
219
}
220
221
t -= 1;
222
float p = d * 0.3f;
223
float a = c * std::pow(2, 10 * t);
224
float s = p / 4;
225
226
return -(a * std::sin((t * d - s) * (2 * Math::PI) / p)) + b;
227
}
228
229
static real_t out(real_t t, real_t b, real_t c, real_t d) {
230
if (t == 0) {
231
return b;
232
}
233
234
t /= d;
235
if (t == 1) {
236
return b + c;
237
}
238
239
float p = d * 0.3f;
240
float s = p / 4;
241
242
return (c * std::pow(2, -10 * t) * std::sin((t * d - s) * (2 * Math::PI) / p) + c + b);
243
}
244
245
static real_t in_out(real_t t, real_t b, real_t c, real_t d) {
246
if (t == 0) {
247
return b;
248
}
249
250
if ((t /= d / 2) == 2) {
251
return b + c;
252
}
253
254
float p = d * (0.3f * 1.5f);
255
float a = c;
256
float s = p / 4;
257
258
if (t < 1) {
259
t -= 1;
260
a *= std::pow(2, 10 * t);
261
return -0.5f * (a * std::sin((t * d - s) * (2 * Math::PI) / p)) + b;
262
}
263
264
t -= 1;
265
a *= std::pow(2, -10 * t);
266
return a * std::sin((t * d - s) * (2 * Math::PI) / p) * 0.5f + c + b;
267
}
268
269
static real_t out_in(real_t t, real_t b, real_t c, real_t d) {
270
if (t < d / 2) {
271
return out(t * 2, b, c / 2, d);
272
}
273
real_t h = c / 2;
274
return in(t * 2 - d, b + h, h, d);
275
}
276
}; // namespace Elastic
277
278
namespace Cubic {
279
static real_t in(real_t t, real_t b, real_t c, real_t d) {
280
t /= d;
281
return c * t * t * t + b;
282
}
283
284
static real_t out(real_t t, real_t b, real_t c, real_t d) {
285
t = t / d - 1;
286
return c * (t * t * t + 1) + b;
287
}
288
289
static real_t in_out(real_t t, real_t b, real_t c, real_t d) {
290
t /= d / 2;
291
if (t < 1) {
292
return c / 2 * t * t * t + b;
293
}
294
295
t -= 2;
296
return c / 2 * (t * t * t + 2) + b;
297
}
298
299
static real_t out_in(real_t t, real_t b, real_t c, real_t d) {
300
if (t < d / 2) {
301
return out(t * 2, b, c / 2, d);
302
}
303
real_t h = c / 2;
304
return in(t * 2 - d, b + h, h, d);
305
}
306
}; // namespace Cubic
307
308
namespace Circ {
309
static real_t in(real_t t, real_t b, real_t c, real_t d) {
310
t /= d;
311
return -c * (std::sqrt(1 - t * t) - 1) + b;
312
}
313
314
static real_t out(real_t t, real_t b, real_t c, real_t d) {
315
t = t / d - 1;
316
return c * std::sqrt(1 - t * t) + b;
317
}
318
319
static real_t in_out(real_t t, real_t b, real_t c, real_t d) {
320
t /= d / 2;
321
if (t < 1) {
322
return -c / 2 * (std::sqrt(1 - t * t) - 1) + b;
323
}
324
325
t -= 2;
326
return c / 2 * (std::sqrt(1 - t * t) + 1) + b;
327
}
328
329
static real_t out_in(real_t t, real_t b, real_t c, real_t d) {
330
if (t < d / 2) {
331
return out(t * 2, b, c / 2, d);
332
}
333
real_t h = c / 2;
334
return in(t * 2 - d, b + h, h, d);
335
}
336
}; // namespace Circ
337
338
namespace Bounce {
339
static real_t out(real_t t, real_t b, real_t c, real_t d) {
340
t /= d;
341
342
if (t < (1 / 2.75f)) {
343
return c * (7.5625f * t * t) + b;
344
}
345
346
if (t < (2 / 2.75f)) {
347
t -= 1.5f / 2.75f;
348
return c * (7.5625f * t * t + 0.75f) + b;
349
}
350
351
if (t < (2.5 / 2.75)) {
352
t -= 2.25f / 2.75f;
353
return c * (7.5625f * t * t + 0.9375f) + b;
354
}
355
356
t -= 2.625f / 2.75f;
357
return c * (7.5625f * t * t + 0.984375f) + b;
358
}
359
360
static real_t in(real_t t, real_t b, real_t c, real_t d) {
361
return c - out(d - t, 0, c, d) + b;
362
}
363
364
static real_t in_out(real_t t, real_t b, real_t c, real_t d) {
365
if (t < d / 2) {
366
return in(t * 2, b, c / 2, d);
367
}
368
real_t h = c / 2;
369
return out(t * 2 - d, b + h, h, d);
370
}
371
372
static real_t out_in(real_t t, real_t b, real_t c, real_t d) {
373
if (t < d / 2) {
374
return out(t * 2, b, c / 2, d);
375
}
376
real_t h = c / 2;
377
return in(t * 2 - d, b + h, h, d);
378
}
379
}; // namespace Bounce
380
381
namespace Back {
382
static real_t in(real_t t, real_t b, real_t c, real_t d) {
383
float s = 1.70158f;
384
t /= d;
385
386
return c * t * t * ((s + 1) * t - s) + b;
387
}
388
389
static real_t out(real_t t, real_t b, real_t c, real_t d) {
390
float s = 1.70158f;
391
t = t / d - 1;
392
393
return c * (t * t * ((s + 1) * t + s) + 1) + b;
394
}
395
396
static real_t in_out(real_t t, real_t b, real_t c, real_t d) {
397
float s = 1.70158f * 1.525f;
398
t /= d / 2;
399
400
if (t < 1) {
401
return c / 2 * (t * t * ((s + 1) * t - s)) + b;
402
}
403
404
t -= 2;
405
return c / 2 * (t * t * ((s + 1) * t + s) + 2) + b;
406
}
407
408
static real_t out_in(real_t t, real_t b, real_t c, real_t d) {
409
if (t < d / 2) {
410
return out(t * 2, b, c / 2, d);
411
}
412
real_t h = c / 2;
413
return in(t * 2 - d, b + h, h, d);
414
}
415
}; // namespace Back
416
417
namespace Spring {
418
static real_t out(real_t t, real_t b, real_t c, real_t d) {
419
t /= d;
420
real_t s = 1.0 - t;
421
t = (std::sin(t * Math::PI * (0.2 + 2.5 * t * t * t)) * std::pow(s, 2.2) + t) * (1.0 + (1.2 * s));
422
return c * t + b;
423
}
424
425
static real_t in(real_t t, real_t b, real_t c, real_t d) {
426
return c - out(d - t, 0, c, d) + b;
427
}
428
429
static real_t in_out(real_t t, real_t b, real_t c, real_t d) {
430
if (t < d / 2) {
431
return in(t * 2, b, c / 2, d);
432
}
433
real_t h = c / 2;
434
return out(t * 2 - d, b + h, h, d);
435
}
436
437
static real_t out_in(real_t t, real_t b, real_t c, real_t d) {
438
if (t < d / 2) {
439
return out(t * 2, b, c / 2, d);
440
}
441
real_t h = c / 2;
442
return in(t * 2 - d, b + h, h, d);
443
}
444
}; // namespace Spring
445
446