Unravel Engine C++ Reference
Loading...
Searching...
No Matches
seq_ease.cpp
Go to the documentation of this file.
1#include "seq_ease.h"
2
3#include "seq_math.h"
4
5#include <cmath>
6
7namespace
8{
9constexpr float pi = 3.14159265358979323846f;
10constexpr float half_pi = pi * 0.5f;
11
12float linearInterpolation(float a)
13{
14 return a;
15}
16
17float quadraticEaseIn(float a)
18{
19 return a * a;
20}
21
22float quadraticEaseOut(float a)
23{
24 return -(a * (a - static_cast<float>(2)));
25}
26
27float quadraticEaseInOut(float a)
28{
29 if(a < static_cast<float>(0.5))
30 {
31 return static_cast<float>(2) * a * a;
32 }
33
34 return (-static_cast<float>(2) * a * a) + (4 * a) - 1.0f;
35}
36
37float cubicEaseIn(float a)
38{
39 return a * a * a;
40}
41
42float cubicEaseOut(float a)
43{
44 float const f = a - 1.0f;
45 return f * f * f + 1.0f;
46}
47
48float cubicEaseInOut(float a)
49{
50 if(a < static_cast<float>(0.5))
51 {
52 return static_cast<float>(4) * a * a * a;
53 }
54
55 float const f = ((static_cast<float>(2) * a) - static_cast<float>(2));
56 return static_cast<float>(0.5) * f * f * f + 1.0f;
57}
58
59float quarticEaseIn(float a)
60{
61 return a * a * a * a;
62}
63
64float quarticEaseOut(float a)
65{
66 float const f = (a - 1.0f);
67 return f * f * f * (1.0f - a) + 1.0f;
68}
69
70float quarticEaseInOut(float a)
71{
72 if(a < static_cast<float>(0.5))
73 {
74 return static_cast<float>(8) * a * a * a * a;
75 }
76
77 float const f = (a - 1.0f);
78 return -static_cast<float>(8) * f * f * f * f + 1.0f;
79}
80
81float quinticEaseIn(float a)
82{
83 return a * a * a * a * a;
84}
85
86float quinticEaseOut(float a)
87{
88 float const f = (a - 1.0f);
89 return f * f * f * f * f + 1.0f;
90}
91
92float quinticEaseInOut(float a)
93{
94 if(a < static_cast<float>(0.5))
95 {
96 return static_cast<float>(16) * a * a * a * a * a;
97 }
98
99 float const f = ((static_cast<float>(2) * a) - static_cast<float>(2));
100 return static_cast<float>(0.5) * f * f * f * f * f + 1.0f;
101}
102
103float sineEaseIn(float a)
104{
105 return std::sin((a - 1.0f) * half_pi) + 1.0f;
106}
107
108float sineEaseOut(float a)
109{
110 return std::sin(a * half_pi);
111}
112
113float sineEaseInOut(float a)
114{
115 return static_cast<float>(0.5) * (1.0f - std::cos(a * static_cast<float>(pi)));
116}
117
118float circularEaseIn(float a)
119{
120 return 1.0f - std::sqrt(1.0f - (a * a));
121}
122
123float circularEaseOut(float a)
124{
125 return std::sqrt((static_cast<float>(2) - a) * a);
126}
127
128float circularEaseInOut(float a)
129{
130 if(a < static_cast<float>(0.5))
131 {
132 return static_cast<float>(0.5) * (1.0f - std::sqrt(1.0f - static_cast<float>(4) * (a * a)));
133 }
134
135 return static_cast<float>(0.5) *
136 (std::sqrt(-((static_cast<float>(2) * a) - static_cast<float>(3)) * ((static_cast<float>(2) * a) - 1.0f)) +
137 1.0f);
138}
139
140float exponentialEaseIn(float a)
141{
142 if(a <= 0.0f)
143 {
144 return a;
145 }
146
147 float const Complementary = a - 1.0f;
148 float const Two = 2.0f;
149
150 return std::pow(Two, Complementary * static_cast<float>(10));
151}
152
153float exponentialEaseOut(float a)
154{
155 if(a >= 1.0f)
156 {
157 return a;
158 }
159
160 return 1.0f - std::pow(static_cast<float>(2), -static_cast<float>(10) * a);
161}
162
163float exponentialEaseInOut(float a)
164{
165 if(a < static_cast<float>(0.5))
166 {
167 return static_cast<float>(0.5) *
168 std::pow(static_cast<float>(2), (static_cast<float>(20) * a) - static_cast<float>(10));
169 }
170
171 return -static_cast<float>(0.5) *
172 std::pow(static_cast<float>(2), (-static_cast<float>(20) * a) + static_cast<float>(10)) +
173 1.0f;
174}
175
176float elasticEaseIn(float a)
177{
178 return std::sin(static_cast<float>(13) * half_pi * a) *
179 std::pow(static_cast<float>(2), static_cast<float>(10) * (a - 1.0f));
180}
181
182float elasticEaseOut(float a)
183{
184 return std::sin(-static_cast<float>(13) * half_pi * (a + 1.0f)) *
185 std::pow(static_cast<float>(2), -static_cast<float>(10) * a) +
186 1.0f;
187}
188
189float elasticEaseInOut(float a)
190{
191 if(a < static_cast<float>(0.5))
192 {
193 return static_cast<float>(0.5) * std::sin(static_cast<float>(13) * half_pi * (static_cast<float>(2) * a)) *
194 std::pow(static_cast<float>(2), static_cast<float>(10) * ((static_cast<float>(2) * a) - 1.0f));
195 }
196
197 return static_cast<float>(0.5) *
198 (std::sin(-static_cast<float>(13) * half_pi * ((static_cast<float>(2) * a - 1.0f) + 1.0f)) *
199 std::pow(static_cast<float>(2), -static_cast<float>(10) * (static_cast<float>(2) * a - 1.0f)) +
200 static_cast<float>(2));
201}
202
203float backEaseIn(float a, float const& o)
204{
205 float z = ((o + 1.0f) * a) - o;
206 return (a * a * z);
207}
208
209float backEaseOut(float a, float const& o)
210{
211 float n = a - 1.0f;
212 float z = ((o + 1.0f) * n) + o;
213 return (n * n * z) + 1.0f;
214}
215
216float backEaseInOut(float a, float const& o)
217{
218 float s = o * static_cast<float>(1.525);
219 float x = 0.5f;
220 float n = a / static_cast<float>(0.5);
221
222 if(n < static_cast<float>(1))
223 {
224 float z = ((s + static_cast<float>(1)) * n) - s;
225 float m = n * n * z;
226 return x * m;
227 }
228
229 n -= static_cast<float>(2);
230 float z = ((s + static_cast<float>(1)) * n) + s;
231 float m = (n * n * z) + static_cast<float>(2);
232 return x * m;
233}
234
235float backEaseIn(float a)
236{
237 return backEaseIn(a, static_cast<float>(1.70158));
238}
239
240float backEaseOut(float a)
241{
242 return backEaseOut(a, static_cast<float>(1.70158));
243}
244
245float backEaseInOut(float a)
246{
247 return backEaseInOut(a, static_cast<float>(1.70158));
248}
249
250float bounceEaseOut(float a)
251{
252 if(a < static_cast<float>(4.0 / 11.0))
253 {
254 return (static_cast<float>(121) * a * a) / static_cast<float>(16);
255 }
256 if(a < static_cast<float>(8.0 / 11.0))
257 {
258 return (static_cast<float>(363.0 / 40.0) * a * a) - (static_cast<float>(99.0 / 10.0) * a) +
259 static_cast<float>(17.0 / 5.0);
260 }
261 if(a < static_cast<float>(9.0 / 10.0))
262 {
263 return (static_cast<float>(4356.0 / 361.0) * a * a) - (static_cast<float>(35442.0 / 1805.0) * a) +
264 static_cast<float>(16061.0 / 1805.0);
265 }
266 return (static_cast<float>(54.0 / 5.0) * a * a) - (static_cast<float>(513.0 / 25.0) * a) +
267 static_cast<float>(268.0 / 25.0);
268}
269
270float bounceEaseIn(float a)
271{
272 return 1.0f - bounceEaseOut(1.0f - a);
273}
274
275float bounceEaseInOut(float a)
276{
277 if(a < static_cast<float>(0.5))
278 {
279 return static_cast<float>(0.5) * (1.0f - bounceEaseOut(a * static_cast<float>(2)));
280 }
281
282 return static_cast<float>(0.5) * bounceEaseOut(a * static_cast<float>(2) - 1.0f) + static_cast<float>(0.5);
283}
284
285} // end of anonymous namespace
286
287namespace seq
288{
289namespace ease
290{
291
292float linear(float progress)
293{
294 return linearInterpolation(progress);
295}
296
298float smooth_start(float progress)
299{
300 return sineEaseIn(progress);
301}
302
304float smooth_stop(float progress)
305{
306 return sineEaseOut(progress);
307}
308
310float smooth_start_stop(float progress)
311{
312 return sineEaseInOut(progress);
313}
314
316float smooth_start2(float progress)
317{
318 return quadraticEaseIn(progress);
319}
320
322float smooth_stop2(float progress)
323{
324 return quadraticEaseOut(progress);
325}
326
330float smooth_start_stop2(float progress)
331{
332 return quadraticEaseInOut(progress);
333}
334
336float smooth_start3(float progress)
337{
338 return cubicEaseIn(progress);
339}
340
342float smooth_stop3(float progress)
343{
344 return cubicEaseOut(progress);
345}
346
350float smooth_start_stop3(float progress)
351{
352 return cubicEaseInOut(progress);
353}
354
356float smooth_start4(float progress)
357{
358 return quarticEaseIn(progress);
359}
360
362float smooth_stop4(float progress)
363{
364 return quarticEaseOut(progress);
365}
366
370float smooth_start_stop4(float progress)
371{
372 return quarticEaseInOut(progress);
373}
374
376float smooth_start5(float progress)
377{
378 return quinticEaseIn(progress);
379}
380
382float smooth_stop5(float progress)
383{
384 return quinticEaseOut(progress);
385}
386
390float smooth_start_stop5(float progress)
391{
392 return quinticEaseInOut(progress);
393}
394
396float smooth_start6(float progress)
397{
398 return exponentialEaseIn(progress);
399}
400
402float smooth_stop6(float progress)
403{
404 return exponentialEaseOut(progress);
405}
406
410float smooth_start_stop6(float progress)
411{
412 return exponentialEaseInOut(progress);
413}
414
416float circular_start(float progress)
417{
418 return circularEaseIn(progress);
419}
420
422float circular_stop(float progress)
423{
424 return circularEaseOut(progress);
425}
426
430float circular_start_stop(float progress)
431{
432 return circularEaseInOut(progress);
433}
434
436float elastic_start(float progress)
437{
438 return elasticEaseIn(progress);
439}
440
442float elastic_stop(float progress)
443{
444 return elasticEaseOut(progress);
445}
446
450float elastic_start_stop(float progress)
451{
452 return elasticEaseInOut(progress);
453}
454
455float back_start(float progress)
456{
457 return backEaseIn(progress);
458}
459
460float back_stop(float progress)
461{
462 return backEaseOut(progress);
463}
464
465float back_start_stop(float progress)
466{
467 return backEaseInOut(progress);
468}
469
470float bounce_start(float progress)
471{
472 return bounceEaseIn(progress);
473}
474
475float bounce_stop(float progress)
476{
477 return bounceEaseOut(progress);
478}
479
480float bounce_start_stop(float progress)
481{
482 return bounceEaseInOut(progress);
483}
484
485float arch(float progress)
486{
487 return progress * (1.0f - progress) * 4.0f;
488}
489
490float arch_smooth_step(float progress)
491{
492 return reverse_scale(scale(arch(progress), progress), progress) * 4.0f;
493}
494
495float arch_smooth_start_stop(float progress)
496{
497 return arch_smooth_start(progress) * arch_smooth_stop(progress);
498}
499
500float arch_smooth_start(float progress)
501{
502 return progress * progress * (1.0f - progress) * 8.0f;
503}
504
505float arch_smooth_stop(float progress)
506{
507 const auto remaining = 1.0f - progress;
508 return progress * remaining * remaining * 8.0f;
509}
510
511std::function<float(float)> create_back_start(float overshoot)
512{
513 return [overshoot](float a)
514 {
515 return backEaseIn(a, overshoot);
516 };
517}
518
519std::function<float(float)> create_back_stop(float overshoot)
520{
521 return [overshoot](float a)
522 {
523 return backEaseOut(a, overshoot);
524 };
525}
526
527std::function<float(float)> create_back_stop_stop(float overshoot)
528{
529 return [overshoot](float a)
530 {
531 return backEaseInOut(a, overshoot);
532 };
533}
534
535const std::vector<std::pair<std::string, std::function<float(float)>>>& get_ease_list()
536{
537 static const std::vector<std::pair<std::string, std::function<float(float)>>> list = {
538 {"linear", linear},
539
540 {"smooth_start", smooth_start},
541 {"smooth_start2", smooth_start2},
542 {"smooth_start3", smooth_start3},
543 {"smooth_start4", smooth_start4},
544 {"smooth_start5", smooth_start5},
545 {"smooth_start6", smooth_start6},
546
547 {"smooth_stop", smooth_stop},
548 {"smooth_stop2", smooth_stop2},
549 {"smooth_stop3", smooth_stop3},
550 {"smooth_stop4", smooth_stop4},
551 {"smooth_stop5", smooth_stop5},
552 {"smooth_stop6", smooth_stop6},
553
554 {"smooth_start_stop", smooth_start_stop},
555 {"smooth_start_stop2", smooth_start_stop2},
556 {"smooth_start_stop3", smooth_start_stop3},
557 {"smooth_start_stop4", smooth_start_stop4},
558 {"smooth_start_stop5", smooth_start_stop5},
559 {"smooth_start_stop6", smooth_start_stop6},
560
561 {"circular_start", circular_start},
562 {"circular_stop", circular_stop},
563 {"circular_start_stop", circular_start_stop},
564
565 {"elastic_start", elastic_start},
566 {"elastic_stop", elastic_stop},
567 {"elastic_start_stop", elastic_start_stop},
568
569 {"back_start", back_start},
570 {"back_stop", back_stop},
571 {"back_start_stop", back_start_stop},
572
573 {"bounce_start", bounce_start},
574 {"bounce_stop", bounce_stop},
575 {"bounce_start_stop", bounce_start_stop},
576
577 {"arch", arch},
578 {"arch_smooth_step", arch_smooth_step},
579 {"arch_smooth_start_stop", arch_smooth_start_stop},
580 {"arch_smooth_start", arch_smooth_start},
581 {"arch_smooth_stop", arch_smooth_stop},
582 };
583 return list;
584}
585
586} // namespace ease
587} // namespace seq
entt::handle a
float scale
Definition hub.cpp:25
float arch_smooth_start(float progress)
Definition seq_ease.cpp:500
float bounce_start_stop(float progress)
Definition seq_ease.cpp:480
float elastic_start(float progress)
Modelled after the damped sine wave y = sin(13pi/2*x)*pow(2, 10 * (x - 1))
Definition seq_ease.cpp:436
float smooth_stop3(float progress)
Modelled after the cubic y = (x - 1)^3 + 1.
Definition seq_ease.cpp:342
float smooth_stop4(float progress)
Modelled after the quartic y = 1 - (x - 1)^4.
Definition seq_ease.cpp:362
float circular_stop(float progress)
Modelled after shifted quadrant II of unit circle.
Definition seq_ease.cpp:422
float arch(float progress)
Definition seq_ease.cpp:485
float smooth_stop2(float progress)
Modelled after the parabola y = -x^2 + 2x.
Definition seq_ease.cpp:322
float smooth_start6(float progress)
Modelled after the exponential function y = 2^(10(x - 1))
Definition seq_ease.cpp:396
float smooth_start2(float progress)
Modelled after the parabola y = x^2.
Definition seq_ease.cpp:316
std::function< float(float)> create_back_stop(float overshoot)
Definition seq_ease.cpp:519
float smooth_start_stop3(float progress)
Definition seq_ease.cpp:350
float circular_start(float progress)
Modelled after shifted quadrant IV of unit circle.
Definition seq_ease.cpp:416
float smooth_stop(float progress)
Modelled after quarter-cycle of sine wave (different phase)
Definition seq_ease.cpp:304
float linear(float progress)
Definition seq_ease.cpp:292
float arch_smooth_step(float progress)
Definition seq_ease.cpp:490
float smooth_start_stop4(float progress)
Definition seq_ease.cpp:370
float smooth_stop5(float progress)
Modelled after the quintic y = (x - 1)^5 + 1.
Definition seq_ease.cpp:382
float smooth_stop6(float progress)
Modelled after the exponential function y = -2^(-10x) + 1.
Definition seq_ease.cpp:402
float elastic_stop(float progress)
Modelled after the damped sine wave y = sin(-13pi/2*(x + 1))*pow(2, -10x) + 1.
Definition seq_ease.cpp:442
std::function< float(float)> create_back_start(float overshoot)
Definition seq_ease.cpp:511
float circular_start_stop(float progress)
Definition seq_ease.cpp:430
float smooth_start_stop2(float progress)
Definition seq_ease.cpp:330
float arch_smooth_stop(float progress)
Definition seq_ease.cpp:505
float bounce_stop(float progress)
Definition seq_ease.cpp:475
float back_stop(float progress)
Definition seq_ease.cpp:460
float smooth_start_stop5(float progress)
Definition seq_ease.cpp:390
float smooth_start_stop(float progress)
Modelled after half sine wave.
Definition seq_ease.cpp:310
float arch_smooth_start_stop(float progress)
Definition seq_ease.cpp:495
float smooth_start_stop6(float progress)
Definition seq_ease.cpp:410
float bounce_start(float progress)
Definition seq_ease.cpp:470
std::function< float(float)> create_back_stop_stop(float overshoot)
Definition seq_ease.cpp:527
float smooth_start(float progress)
Modelled after quarter-cycle of sine wave.
Definition seq_ease.cpp:298
float smooth_start3(float progress)
Modelled after the cubic y = x^3.
Definition seq_ease.cpp:336
float smooth_start5(float progress)
Modelled after the quintic y = x^5.
Definition seq_ease.cpp:376
float back_start_stop(float progress)
Definition seq_ease.cpp:465
float back_start(float progress)
Definition seq_ease.cpp:455
float smooth_start4(float progress)
Modelled after the quartic x^4.
Definition seq_ease.cpp:356
const std::vector< std::pair< std::string, std::function< float(float)> > > & get_ease_list()
Definition seq_ease.cpp:535
float elastic_start_stop(float progress)
Definition seq_ease.cpp:450
Provides a sequence-based action management system for controlling and scheduling actions.
auto reverse_scale(float a, float t) -> float
Scales a value in reverse by a factor.
Definition seq_math.cpp:35
float x
float z