Not Enough Math
Lightweight zero-dependency C++ math library
Loading...
Searching...
No Matches
easing.hpp
Go to the documentation of this file.
1#pragma once
2
3#include <concepts>
4#include "config.hpp"
5#include "utils.hpp"
6#include "trig.hpp"
7#include "power.hpp"
8
9// from my older project
10// https://github.com/AnanasikDev/AEngine/blob/main/AEngine/Core/Mathf.cpp
11
12namespace nem
13{
14 namespace ease
15 {
16 template <std::floating_point T> constexpr T in_out_back(T v)
17 {
18 constexpr T c1 = (T)1.70158;
19 constexpr T c2 = c1 * (T)1.525;
20 v *= 2;
21
22 return v < (T)0.5
23 ? (nem::sqr(v) * ((c2 + 1) * v - c2)) / 2
24 : (nem::sqr(v - 2) * ((c2 + 1) * (v - 2) + c2) + 2) / 2;
25 }
26
27 template <std::floating_point T> constexpr T in_back(T v)
28 {
29 constexpr T c1 = (T)1.70158;
30 constexpr T c2 = c1 + 1;
31
32 return c2 * nem::cube(v) - c1 * nem::sqr(v);
33 }
34
35 template <std::floating_point T> constexpr T out_back(T v)
36 {
37 constexpr T c1 = (T)1.70158;
38 constexpr T c2 = c1 + 1;
39
40 return 1 + c2 * nem::cube(v - 1) + c1 * nem::sqr(v - 1);
41 }
42
43 template <std::floating_point T> constexpr T in_sine(T v)
44 {
45 return 1 - nem::cos(v * nem::PI<T> * (T)0.5);
46 }
47
48 template <std::floating_point T> constexpr T out_sine(T v)
49 {
50 return nem::sin(v * nem::PI<T> * (T)0.5);
51 }
52
53 template <std::floating_point T> constexpr T in_out_sine(T v)
54 {
55 return -(nem::cos(nem::PI<T> * v) - 1) * (T)0.5;
56 }
57
58 template <std::floating_point T> constexpr T in_bounce(T v)
59 {
60 constexpr T n1 = (T)7.5625;
61 constexpr T d1 = (T)(1.0 / 2.75);
62
63 if (v < d1)
64 {
65 return n1 * v * v;
66 }
67 else if (v < 2 * d1)
68 {
69 return n1 * (v -= (T)1.5f * d1) * v + (T)0.75;
70 }
71 else if (v < (T)2.5 * d1)
72 {
73 return n1 * (v -= (T)2.25f * d1) * v + (T)0.9375;
74 }
75 else
76 {
77 return n1 * (v -= (T)2.625f * d1) * v + (T)0.984375;
78 }
79 }
80
81 template <std::floating_point T> constexpr T out_bounce(T v)
82 {
83 constexpr T n1 = (T)7.5625;
84 constexpr T d1 = (T)(1.0 / 2.75);
85
86 if (v < 1 * d1)
87 {
88 return n1 * v * v;
89 }
90 else if (v < 2 * d1)
91 {
92 return n1 * (v -= (T)1.5 * d1) * v + (T)0.75;
93 }
94 else if (v < 2.5f * d1)
95 {
96 return n1 * (v -= (T)2.25 * d1) * v + (T)0.9375;
97 }
98 else
99 {
100 return n1 * (v -= (T)2.625 * d1) * v + (T)0.984375;
101 }
102 }
103
104 template <std::floating_point T> constexpr T in_out_bounce(T v)
105 {
106 return v < 0.5
107 ? (1 - out_bounce(1 - 2 * v)) * (T)0.5
108 : (1 + out_bounce(2 * v - 1)) * (T)0.5;
109 }
110
111 NEM_INLINE template <std::floating_point T> T in_elastic(T v)
112 {
113 constexpr T c4 = (2 * nem::PI<T>) / (T)3.0;
114
115 return v == 0
116 ? 0
117 : (v == 1
118 ? 1
119 : -nem::pow((T)2, (T)10 * v - (T)10) * nem::sin((v * (T)10 - (T)10.75) * c4));
120 }
121
122 NEM_INLINE template <std::floating_point T> T out_elastic(T v)
123 {
124 constexpr T c4 = (2 * nem::PI<T>) / (T)3.0;
125
126 return v == 0
127 ? 0
128 : (v == 1
129 ? 1
130 : nem::pow((T)2, (T)-10 * v) * nem::sin((v * (T)10 - (T)0.75) * c4) + (T)1);
131 }
132
133 NEM_INLINE template <std::floating_point T> T in_out_elastic(T v)
134 {
135 constexpr T c5 = (2 * nem::PI<T>) / (T)4.5;
136
137 return v == 0
138 ? 0
139 : (v == 1
140 ? 1
141 : (v < 0.5
142 ? -(nem::pow((T)2.0, (T)20 * v - (T)10) * nem::sin(((T)20 * v - (T)11.125) * c5)) * (T)0.5
143 : (nem::pow((T)2.0, (T)-20 * v + (T)10) * nem::sin(((T)20 * v - (T)11.125) * c5)) * (T)0.5 + (T)1));
144 }
145
146 template <std::floating_point T> constexpr T in_cubic(T v)
147 {
148 return v * v * v;
149 }
150
151 template <std::floating_point T> constexpr T out_cubic(T v)
152 {
153 return 1 - nem::cube(1 - v);
154 }
155
156 template <std::floating_point T> constexpr T in_out_cubic(T v)
157 {
158 return v < 0.5f ? 4 * v * v * v : 1 - nem::cube(-2 * v + 2) * (T)0.5;
159 }
160 }
161}
#define NEM_INLINE
Definition config.hpp:12
constexpr T in_out_back(T v)
Definition easing.hpp:16
constexpr T out_back(T v)
Definition easing.hpp:35
constexpr T in_out_bounce(T v)
Definition easing.hpp:104
T in_elastic(T v)
Definition easing.hpp:111
T in_out_elastic(T v)
Definition easing.hpp:133
constexpr T out_cubic(T v)
Definition easing.hpp:151
constexpr T in_sine(T v)
Definition easing.hpp:43
constexpr T out_bounce(T v)
Definition easing.hpp:81
constexpr T out_sine(T v)
Definition easing.hpp:48
constexpr T in_out_sine(T v)
Definition easing.hpp:53
T out_elastic(T v)
Definition easing.hpp:122
constexpr T in_bounce(T v)
Definition easing.hpp:58
constexpr T in_back(T v)
Definition easing.hpp:27
constexpr T in_cubic(T v)
Definition easing.hpp:146
constexpr T in_out_cubic(T v)
Definition easing.hpp:156
Definition config.hpp:16
constexpr T pow(T base, E exp)
Definition power.hpp:131
constexpr T PI
Definition consts.hpp:10
constexpr T cos(T x)
Definition trig.hpp:100
constexpr T sin(T x)
Definition trig.hpp:83
constexpr T sqr(T value) noexcept
Definition utils.hpp:72
constexpr T cube(T value) noexcept
Definition utils.hpp:73