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// Quadratic
14{
15 return t * t;
16}
17
19{
20 return t * (2.0F - t);
21}
22
24{
25 return t < 0.5F ? 2.0F * t * t : -1.0F + ((4.0F - (2.0F * t)) * t);
26}
27
28// Cubic
30{
31 return t * t * t;
32}
33
35{
36 const float u = t - 1.0F;
37 return (u * u * u) + 1.0F;
38}
39
41{
42 return t < 0.5F ? 4.0F * t * t * t
43 : ((t - 1.0F) * (2.0F * t - 2.0F) * (2.0F * t - 2.0F)) + 1.0F;
44}
45
46// Sine
48{
49 return 1.0F - std::cos(t * std::numbers::pi_v<float> * 0.5F);
50}
51
53{
54 return std::sin(t * std::numbers::pi_v<float> * 0.5F);
55}
56
58{
59 return 0.5F * (1.0F - std::cos(std::numbers::pi_v<float> * t));
60}
61
62// Exponential
64{
65 return t == 0.0F ? 0.0F : std::pow(2.0F, 10.0F * (t - 1.0F));
66}
67
69{
70 return t == 1.0F ? 1.0F : 1.0F - std::pow(2.0F, -10.0F * t);
71}
72
74{
75 if (t == 0.0F) {
76 return 0.0F;
77 }
78
79 if (t == 1.0F) {
80 return 1.0F;
81 }
82
83 if (t < 0.5F) {
84 return std::pow(2.0F, (20.0F * t) - 10.0F) * 0.5F;
85 }
86
87 return (2.0F - std::pow(2.0F, (-20.0F * t) + 10.0F)) * 0.5F;
88}
89
90// Elastic
92{
93 if (t == 0.0F || t == 1.0F) {
94 return t;
95 }
96
97 return -std::pow(2.0F, 10.0F * (t - 1.0F))
98 * std::sin((t - 1.075F) * (2.0F * std::numbers::pi_v<float>) / 0.3F);
99}
100
102{
103 if (t == 0.0F || t == 1.0F) {
104 return t;
105 }
106
107 return (std::pow(2.0F, -10.0F * t)
108 * std::sin((t - 0.075F) * (2.0F * std::numbers::pi_v<float>) / 0.3F))
109 + 1.0F;
110}
111
112// Bounce
114{
115 if (t < 1.0F / 2.75F) {
116 return 7.5625F * t * t;
117 }
118
119 if (t < 2.0F / 2.75F) {
120 t -= 1.5F / 2.75F;
121 return (7.5625F * t * t) + 0.75F;
122 }
123
124 if (t < 2.5F / 2.75F) {
125 t -= 2.25F / 2.75F;
126 return (7.5625F * t * t) + 0.9375F;
127 }
128
129 t -= 2.625F / 2.75F;
130
131 return (7.5625F * t * t) + 0.984375F;
132}
133
135{
136 return 1.0F - ease_out_bounce(1.0F - t);
137}
138
139// Back (overshoot)
141{
142 constexpr float s = 1.70158F;
143 return t * t * ((s + 1.0F) * t - s);
144}
145
147{
148 constexpr float s = 1.70158F;
149 const float u = t - 1.0F;
150 return (u * u * ((s + 1.0F) * u + s)) + 1.0F;
151}
Easing functions for animations.
float ease_out_cubic(float t)
Definition easing.cpp:34
float ease_out_quad(float t)
Definition easing.cpp:18
float ease_in_out_expo(float t)
Definition easing.cpp:73
float linear(float t)
Definition easing.cpp:7
float ease_in_elastic(float t)
Definition easing.cpp:91
float ease_out_bounce(float t)
Definition easing.cpp:113
float ease_in_out_quad(float t)
Definition easing.cpp:23
float ease_out_back(float t)
Definition easing.cpp:146
float ease_in_out_cubic(float t)
Definition easing.cpp:40
float ease_in_back(float t)
Definition easing.cpp:140
float ease_out_sine(float t)
Definition easing.cpp:52
float ease_in_cubic(float t)
Definition easing.cpp:29
float ease_in_quad(float t)
Definition easing.cpp:13
float ease_in_sine(float t)
Definition easing.cpp:47
float ease_in_out_sine(float t)
Definition easing.cpp:57
float ease_out_elastic(float t)
Definition easing.cpp:101
float ease_out_expo(float t)
Definition easing.cpp:68
float ease_in_expo(float t)
Definition easing.cpp:63
float ease_in_bounce(float t)
Definition easing.cpp:134