Sound Bakery  v0.1.0
Open-source audio middleware for games
Loading...
Searching...
No Matches
easing.h
1#pragma once
2
3#include <cmath>
4
5namespace sbk::maths
6{
7 constexpr double PI = 3.1415926545;
8
9 double easeInSine(double t) { return sin(1.5707963 * t); }
10
11 double easeOutSine(double t) { return 1 + sin(1.5707963 * (--t)); }
12
13 double easeInOutSine(double t) { return 0.5 * (1 + sin(3.1415926 * (t - 0.5))); }
14
15 double easeInQuad(double t) { return t * t; }
16
17 double easeOutQuad(double t) { return t * (2 - t); }
18
19 double easeInOutQuad(double t) { return t < 0.5 ? 2 * t * t : t * (4 - 2 * t) - 1; }
20
21 double easeInCubic(double t) { return t * t * t; }
22
23 double easeOutCubic(double t) { return 1 + (--t) * t * t; }
24
25 double easeInOutCubic(double t) { return t < 0.5 ? 4 * t * t * t : 1 + (--t) * (2 * (--t)) * (2 * t); }
26
27 double easeInQuart(double t)
28 {
29 t *= t;
30 return t * t;
31 }
32
33 double easeOutQuart(double t)
34 {
35 t = (--t) * t;
36 return 1 - t * t;
37 }
38
39 double easeInOutQuart(double t)
40 {
41 if (t < 0.5)
42 {
43 t *= t;
44 return 8 * t * t;
45 }
46 else
47 {
48 t = (--t) * t;
49 return 1 - 8 * t * t;
50 }
51 }
52
53 double easeInQuint(double t)
54 {
55 double t2 = t * t;
56 return t * t2 * t2;
57 }
58
59 double easeOutQuint(double t)
60 {
61 double t2 = (--t) * t;
62 return 1 + t * t2 * t2;
63 }
64
65 double easeInOutQuint(double t)
66 {
67 double t2;
68 if (t < 0.5)
69 {
70 t2 = t * t;
71 return 16 * t * t2 * t2;
72 }
73 else
74 {
75 t2 = (--t) * t;
76 return 1 + 16 * t * t2 * t2;
77 }
78 }
79
80 double easeInExpo(double t) { return (pow(2, 8 * t) - 1) / 255; }
81
82 double easeOutExpo(double t) { return 1 - pow(2, -8 * t); }
83
84 double easeInOutExpo(double t)
85 {
86 if (t < 0.5)
87 {
88 return (pow(2, 16 * t) - 1) / 510;
89 }
90 else
91 {
92 return 1 - 0.5 * pow(2, -16 * (t - 0.5));
93 }
94 }
95
96 double easeInCirc(double t) { return 1 - sqrt(1 - t); }
97
98 double easeOutCirc(double t) { return sqrt(t); }
99
100 double easeInOutCirc(double t)
101 {
102 if (t < 0.5)
103 {
104 return (1 - sqrt(1 - 2 * t)) * 0.5;
105 }
106 else
107 {
108 return (1 + sqrt(2 * t - 1)) * 0.5;
109 }
110 }
111
112 double easeInBack(double t) { return t * t * (2.70158 * t - 1.70158); }
113
114 double easeOutBack(double t) { return 1 + (--t) * t * (2.70158 * t + 1.70158); }
115
116 double easeInOutBack(double t)
117 {
118 if (t < 0.5)
119 {
120 return t * t * (7 * t - 2.5) * 2;
121 }
122 else
123 {
124 return 1 + (--t) * t * 2 * (7 * t + 2.5);
125 }
126 }
127
128 double easeInElastic(double t)
129 {
130 double t2 = t * t;
131 return t2 * t2 * sin(t * PI * 4.5);
132 }
133
134 double easeOutElastic(double t)
135 {
136 double t2 = (t - 1) * (t - 1);
137 return 1 - t2 * t2 * cos(t * PI * 4.5);
138 }
139
140 double easeInOutElastic(double t)
141 {
142 double t2;
143 if (t < 0.45)
144 {
145 t2 = t * t;
146 return 8 * t2 * t2 * sin(t * PI * 9);
147 }
148 else if (t < 0.55)
149 {
150 return 0.5 + 0.75 * sin(t * PI * 4);
151 }
152 else
153 {
154 t2 = (t - 1) * (t - 1);
155 return 1 - 8 * t2 * t2 * sin(t * PI * 9);
156 }
157 }
158
159 double easeInBounce(double t) { return pow(2, 6 * (t - 1)) * abs(sin(t * PI * 3.5)); }
160
161 double easeOutBounce(double t) { return 1 - pow(2, -6 * t) * abs(cos(t * PI * 3.5)); }
162
163 double easeInOutBounce(double t)
164 {
165 if (t < 0.5)
166 {
167 return 8 * pow(2, 8 * (t - 1)) * abs(sin(t * PI * 7));
168 }
169 else
170 {
171 return 1 - 8 * pow(2, -8 * t) * abs(sin(t * PI * 7));
172 }
173 }
174} // namespace sbk::maths