Estella 0.113.0
A fast 2D and 3D game engine powered by WebAssembly and ECS
Loading...
Searching...
No Matches
EasingFunctions.hpp
Go to the documentation of this file.
1// SPDX-License-Identifier: Apache-2.0
2// SPDX-FileCopyrightText: Copyright (c) 2024-present ESEngine Team
3#pragma once
4
5#include "../core/Types.hpp"
6#include "TweenData.hpp"
7
8#include <cmath>
9
11
12constexpr f32 ANIM_PI = 3.14159265358979323846f;
13
14inline f32 easeLinear(f32 t) {
15 return t;
16}
17
18inline f32 easeInQuad(f32 t) {
19 return t * t;
20}
21
22inline f32 easeOutQuad(f32 t) {
23 return t * (2.0f - t);
24}
25
27 return t < 0.5f ? 2.0f * t * t : -1.0f + (4.0f - 2.0f * t) * t;
28}
29
30inline f32 easeInCubic(f32 t) {
31 return t * t * t;
32}
33
34inline f32 easeOutCubic(f32 t) {
35 f32 t1 = t - 1.0f;
36 return t1 * t1 * t1 + 1.0f;
37}
38
40 return t < 0.5f
41 ? 4.0f * t * t * t
42 : (t - 1.0f) * (2.0f * t - 2.0f) * (2.0f * t - 2.0f) + 1.0f;
43}
44
45inline f32 easeInBack(f32 t) {
46 constexpr f32 C1 = 1.70158f;
47 constexpr f32 C3 = C1 + 1.0f;
48 return C3 * t * t * t - C1 * t * t;
49}
50
51inline f32 easeOutBack(f32 t) {
52 constexpr f32 C1 = 1.70158f;
53 constexpr f32 C3 = C1 + 1.0f;
54 f32 t1 = t - 1.0f;
55 return 1.0f + C3 * t1 * t1 * t1 + C1 * t1 * t1;
56}
57
59 constexpr f32 C1 = 1.70158f;
60 constexpr f32 C2 = C1 * 1.525f;
61 if (t < 0.5f) {
62 return (std::pow(2.0f * t, 2.0f) * ((C2 + 1.0f) * 2.0f * t - C2)) / 2.0f;
63 }
64 return (std::pow(2.0f * t - 2.0f, 2.0f) * ((C2 + 1.0f) * (t * 2.0f - 2.0f) + C2) + 2.0f) / 2.0f;
65}
66
68 if (t == 0.0f || t == 1.0f) {
69 return t;
70 }
71 return -std::pow(2.0f, 10.0f * t - 10.0f)
72 * std::sin((t * 10.0f - 10.75f) * (2.0f * ANIM_PI / 3.0f));
73}
74
76 if (t == 0.0f || t == 1.0f) {
77 return t;
78 }
79 return std::pow(2.0f, -10.0f * t)
80 * std::sin((t * 10.0f - 0.75f) * (2.0f * ANIM_PI / 3.0f)) + 1.0f;
81}
82
84 if (t == 0.0f || t == 1.0f) {
85 return t;
86 }
87 constexpr f32 C5 = (2.0f * ANIM_PI) / 4.5f;
88 if (t < 0.5f) {
89 return -(std::pow(2.0f, 20.0f * t - 10.0f) * std::sin((20.0f * t - 11.125f) * C5)) / 2.0f;
90 }
91 return (std::pow(2.0f, -20.0f * t + 10.0f) * std::sin((20.0f * t - 11.125f) * C5)) / 2.0f + 1.0f;
92}
93
95 constexpr f32 N1 = 7.5625f;
96 constexpr f32 D1 = 2.75f;
97 if (t < 1.0f / D1) {
98 return N1 * t * t;
99 } else if (t < 2.0f / D1) {
100 t -= 1.5f / D1;
101 return N1 * t * t + 0.75f;
102 } else if (t < 2.5f / D1) {
103 t -= 2.25f / D1;
104 return N1 * t * t + 0.9375f;
105 } else {
106 t -= 2.625f / D1;
107 return N1 * t * t + 0.984375f;
108 }
109}
110
111// Sine, quart, quint, expo and circ: the standard (Penner) curves. Each In/Out/InOut triple starts at 0 and ends at 1.
112
113inline f32 easeInSine(f32 t) { return 1.0f - std::cos((t * ANIM_PI) / 2.0f); }
114inline f32 easeOutSine(f32 t) { return std::sin((t * ANIM_PI) / 2.0f); }
115inline f32 easeInOutSine(f32 t) { return -(std::cos(ANIM_PI * t) - 1.0f) / 2.0f; }
116
117inline f32 easeInQuart(f32 t) { return t * t * t * t; }
118inline f32 easeOutQuart(f32 t) { f32 u = 1.0f - t; return 1.0f - u * u * u * u; }
120 if (t < 0.5f) return 8.0f * t * t * t * t;
121 f32 u = -2.0f * t + 2.0f;
122 return 1.0f - u * u * u * u / 2.0f;
123}
124
125inline f32 easeInQuint(f32 t) { return t * t * t * t * t; }
126inline f32 easeOutQuint(f32 t) { f32 u = 1.0f - t; return 1.0f - u * u * u * u * u; }
128 if (t < 0.5f) return 16.0f * t * t * t * t * t;
129 f32 u = -2.0f * t + 2.0f;
130 return 1.0f - u * u * u * u * u / 2.0f;
131}
132
133inline f32 easeInExpo(f32 t) { return t == 0.0f ? 0.0f : std::pow(2.0f, 10.0f * t - 10.0f); }
134inline f32 easeOutExpo(f32 t) { return t == 1.0f ? 1.0f : 1.0f - std::pow(2.0f, -10.0f * t); }
136 if (t == 0.0f || t == 1.0f) return t;
137 return t < 0.5f ? std::pow(2.0f, 20.0f * t - 10.0f) / 2.0f : (2.0f - std::pow(2.0f, -20.0f * t + 10.0f)) / 2.0f;
138}
139
140inline f32 easeInCirc(f32 t) { return 1.0f - std::sqrt(1.0f - t * t); }
141inline f32 easeOutCirc(f32 t) { f32 u = t - 1.0f; return std::sqrt(1.0f - u * u); }
143 if (t < 0.5f) return (1.0f - std::sqrt(1.0f - std::pow(2.0f * t, 2.0f))) / 2.0f;
144 return (std::sqrt(1.0f - std::pow(-2.0f * t + 2.0f, 2.0f)) + 1.0f) / 2.0f;
145}
146
147// Smoothstep is 3t^2 - 2t^3; smootherstep is 6t^5 - 15t^4 + 10t^3 (zero first AND second derivative at both ends).
148inline f32 smoothstep(f32 t) { return t <= 0.0f ? 0.0f : t >= 1.0f ? 1.0f : t * t * (3.0f - 2.0f * t); }
149inline f32 smootherstep(f32 t) { return t <= 0.0f ? 0.0f : t >= 1.0f ? 1.0f : t * t * t * (t * (t * 6.0f - 15.0f) + 10.0f); }
150
151inline f32 cubicBezier(f32 t, f32 p1x, f32 p1y, f32 p2x, f32 p2y) {
152 constexpr i32 MAX_ITERATIONS = 8;
153 constexpr f32 EPSILON = 1e-6f;
154 f32 x = t;
155 for (i32 i = 0; i < MAX_ITERATIONS; ++i) {
156 f32 ix = 1.0f - x;
157 f32 cx = 3.0f * p1x * ix * ix * x
158 + 3.0f * p2x * ix * x * x
159 + x * x * x - t;
160 if (std::abs(cx) < EPSILON) {
161 break;
162 }
163 f32 dx = 3.0f * p1x * (1.0f - 2.0f * x) * (1.0f - x)
164 + 6.0f * p2x * x * (1.0f - x)
165 - 3.0f * p2x * x * x
166 + 3.0f * x * x;
167 if (std::abs(dx) < EPSILON) {
168 break;
169 }
170 x -= cx / dx;
171 }
172 f32 ix = 1.0f - x;
173 return 3.0f * p1y * ix * ix * x
174 + 3.0f * p2y * ix * x * x
175 + x * x * x;
176}
177
178inline f32 easeInBounce(f32 t) { return 1.0f - easeOutBounce(1.0f - t); }
180 return t < 0.5f ? (1.0f - easeOutBounce(1.0f - 2.0f * t)) / 2.0f : (1.0f + easeOutBounce(2.0f * t - 1.0f)) / 2.0f;
181}
182
183inline f32 step(f32 t) {
184 return t < 1.0f ? 0.0f : 1.0f;
185}
186
187using EasingFn = f32 (*)(f32);
188
190 switch (type) {
191 case EasingType::Linear: return easeLinear;
205 case EasingType::Step: return step;
225 default: return easeLinear;
226 }
227}
228
229} // namespace esengine::animation
Core type definitions and utilities for ESEngine.
EasingFn getEasingFunction(EasingType type)
f32 cubicBezier(f32 t, f32 p1x, f32 p1y, f32 p2x, f32 p2y)
std::int32_t i32
Signed 32-bit integer.
Definition Types.hpp:54
float f32
32-bit floating point
Definition Types.hpp:59