ASW Lib
A.D.S. Games SDL Wrapper Library. A library targeted at Allegro4 users who want to switch to SDL3 and use modern c++.
Loading...
Searching...
No Matches
easing.cpp
Go to the documentation of this file.
2
3#include <cmath>
4#include <numbers>
5
6// Linear
7float asw::easing::linear(float t)
8{
9 return t;
10}
11
12// Smoothstep
14{
15 return t * t * (3.0F - (2.0F * t));
16}
17
18// Quadratic
20{
21 return t * t;
22}
23
25{
26 return t * (2.0F - t);
27}
28
30{
31 return t < 0.5F ? 2.0F * t * t : -1.0F + ((4.0F - (2.0F * t)) * t);
32}
33
34// Cubic
36{
37 return t * t * t;
38}
39
41{
42 const float u = t - 1.0F;
43 return (u * u * u) + 1.0F;
44}
45
47{
48 return t < 0.5F ? 4.0F * t * t * t
49 : ((t - 1.0F) * (2.0F * t - 2.0F) * (2.0F * t - 2.0F)) + 1.0F;
50}
51
52// Sine
54{
55 return 1.0F - std::cos(t * std::numbers::pi_v<float> * 0.5F);
56}
57
59{
60 return std::sin(t * std::numbers::pi_v<float> * 0.5F);
61}
62
64{
65 return 0.5F * (1.0F - std::cos(std::numbers::pi_v<float> * t));
66}
67
68// Exponential
70{
71 return t == 0.0F ? 0.0F : std::pow(2.0F, 10.0F * (t - 1.0F));
72}
73
75{
76 return t == 1.0F ? 1.0F : 1.0F - std::pow(2.0F, -10.0F * t);
77}
78
80{
81 if (t == 0.0F) {
82 return 0.0F;
83 }
84
85 if (t == 1.0F) {
86 return 1.0F;
87 }
88
89 if (t < 0.5F) {
90 return std::pow(2.0F, (20.0F * t) - 10.0F) * 0.5F;
91 }
92
93 return (2.0F - std::pow(2.0F, (-20.0F * t) + 10.0F)) * 0.5F;
94}
95
96// Elastic
98{
99 if (t == 0.0F || t == 1.0F) {
100 return t;
101 }
102
103 return -std::pow(2.0F, 10.0F * (t - 1.0F))
104 * std::sin((t - 1.075F) * (2.0F * std::numbers::pi_v<float>) / 0.3F);
105}
106
108{
109 if (t == 0.0F || t == 1.0F) {
110 return t;
111 }
112
113 return (std::pow(2.0F, -10.0F * t)
114 * std::sin((t - 0.075F) * (2.0F * std::numbers::pi_v<float>) / 0.3F))
115 + 1.0F;
116}
117
118// Bounce
120{
121 if (t < 1.0F / 2.75F) {
122 return 7.5625F * t * t;
123 }
124
125 if (t < 2.0F / 2.75F) {
126 t -= 1.5F / 2.75F;
127 return (7.5625F * t * t) + 0.75F;
128 }
129
130 if (t < 2.5F / 2.75F) {
131 t -= 2.25F / 2.75F;
132 return (7.5625F * t * t) + 0.9375F;
133 }
134
135 t -= 2.625F / 2.75F;
136
137 return (7.5625F * t * t) + 0.984375F;
138}
139
141{
142 return 1.0F - ease_out_bounce(1.0F - t);
143}
144
145// Back (overshoot)
147{
148 constexpr float s = 1.70158F;
149 return t * t * ((s + 1.0F) * t - s);
150}
151
153{
154 constexpr float s = 1.70158F;
155 const float u = t - 1.0F;
156 return (u * u * ((s + 1.0F) * u + s)) + 1.0F;
157}
158
159// Convenience
161{
162 switch (mode) {
164 return smoothstep(t);
166 return ease_out_quad(t);
168 default:
169 return linear(t);
170 }
171}
Easing functions for animations.
float ease_out_cubic(float t)
Definition easing.cpp:40
float smoothstep(float t)
Soft at both ends, 3t^2 - 2t^3.
Definition easing.cpp:13
float ease_out_quad(float t)
Definition easing.cpp:24
float ease_in_out_expo(float t)
Definition easing.cpp:79
float linear(float t)
Definition easing.cpp:7
float ease_in_elastic(float t)
Definition easing.cpp:97
float ease_out_bounce(float t)
Definition easing.cpp:119
float ease_in_out_quad(float t)
Definition easing.cpp:29
float ease_out_back(float t)
Definition easing.cpp:152
float ease_in_out_cubic(float t)
Definition easing.cpp:46
float ease_in_back(float t)
Definition easing.cpp:146
float ease_out_sine(float t)
Definition easing.cpp:58
float ease_in_cubic(float t)
Definition easing.cpp:35
float ease_in_quad(float t)
Definition easing.cpp:19
float falloff(float t, asw::Falloff mode)
Definition easing.cpp:160
float ease_in_sine(float t)
Definition easing.cpp:53
float ease_in_out_sine(float t)
Definition easing.cpp:63
float ease_out_elastic(float t)
Definition easing.cpp:107
float ease_out_expo(float t)
Definition easing.cpp:74
float ease_in_expo(float t)
Definition easing.cpp:69
float ease_in_bounce(float t)
Definition easing.cpp:140
Falloff
How a gradient or light fades from its centre to its edge.
Definition types.h:46
@ Linear
Even fade to the edge.
@ Quadratic
Bright core that drops off quickly, closer to a real light.
@ Smooth
Soft at the centre and at the edge.