Euphoria
easing.cc
Go to the documentation of this file.
1 #include "core/easing.h"
2 
3 
4 // source: Math for Game Programmers: Fast and Funky 1D Nonlinear Transformations
5 // https://www.youtube.com/watch?v=mr5xkf6zSzk&t=1383s
6 // easing functions on steroids
7 // exploring normalized utility functions
8 
9 // easing functions
10 // filter functions (run a filter on it)
11 // tweening functions
12 
13 #include "assert/assert.h"
14 
16 {
17 
18 // easing "techniques"
19 
20 template<int steps> constexpr float ipow (float t) { return t * ipow<steps-1>(t); }
21 template<> constexpr float ipow<1>(float t) { return t; }
22 
23 constexpr float flip(float t) { return 1 - t; }
24 
25 constexpr float blend(float a, float b, float weight) { return (1-weight) * a + weight*b; }
26 
27 // ipow<2.3>(x) -> fracpow<2>(0.3f, x)
28 template<int steps> constexpr float fracpow(float frac, float t) { return blend(ipow<steps>(t), ipow<steps+1>(t), frac);}
29 
30 constexpr float fabs(float t) { return t < 0 ? -t : t; }
31 
32 constexpr float bounce_clamp_top(float t) { return fabs(t); }
33 
34 constexpr float bounce_clamp_bottom(float t) { return 1 - fabs(1 - t); }
35 
36 constexpr float bounce_clamp(float t) { return bounce_clamp_top(bounce_clamp_bottom(t)); }
37 
38 constexpr float normalized_bezier_curve3(float b, float c, float t)
39 {
40  const auto s = 1 - t;
41  const auto t2 = t*t;
42  const auto s2 = s*s;
43  const auto t3 = t2*t;
44  return 3*b*s2*t + 3*c*s*t2 + t3;
45 }
46 
47 
48 // library of easing functions
49 
50 
51 template<int steps>
52 float smooth_start(float t) { return ipow<steps>(t); }
53 
54 template<int steps>
55 float smooth_stop(float t) { return flip(ipow<steps>(flip(t))); }
56 
57 template<int steps>
58 float smooth_step(float t) { return blend(smooth_start<steps>(t), smooth_stop<steps>(t), t); }
59 
60 
61 float apply(Function f, float t)
62 {
63  switch(f)
64  {
65  case Function::linear: return t;
66 
67  case Function::smooth_start2: return smooth_start<2>(t);
68  case Function::smooth_start3: return smooth_start<3>(t);
69  case Function::smooth_start4: return smooth_start<4>(t);
70 
71  case Function::smooth_stop2: return smooth_stop<2>(t);
72  case Function::smooth_stop3: return smooth_stop<3>(t);
73  case Function::smooth_stop4: return smooth_stop<4>(t);
74 
75  case Function::smooth_step2: return smooth_step<2>(t);
76  case Function::smooth_step3: return smooth_step<3>(t);
77  case Function::smooth_step4: return smooth_step<4>(t);
78 
79  default:
80  DIE("Invalid case");
81  return 0.0f;
82  }
83 }
84 
85 
86 
87 std::vector<std::pair<const char*, Function>> get_all_values()
88 {
89  return
90  {
91 #define VALUE(x) {#x, Function::x}
92  {"Linear", Function::linear},
93 
94  {"Smooth Start 2", Function::smooth_start2},
95  {"Smooth Start 3", Function::smooth_start3},
96  {"Smooth Start 4", Function::smooth_start4},
97 
98  {"Smooth Stop 2", Function::smooth_stop2},
99  {"Smooth Stop 3", Function::smooth_stop3},
100  {"Smooth Stop 4", Function::smooth_stop4},
101 
102  {"Smooth Step 2", Function::smooth_step2},
103  {"Smooth Step 3", Function::smooth_step3},
104  {"Smooth Step 4", Function::smooth_step4}
105 #undef VALUE
106  };
107 }
108 
109 }
#define DIE(message)
Definition: assert.h:67
constexpr float bounce_clamp(float t)
Definition: easing.cc:36
float smooth_start(float t)
Definition: easing.cc:52
float smooth_stop(float t)
Definition: easing.cc:55
constexpr float flip(float t)
Definition: easing.cc:23
constexpr float fabs(float t)
Definition: easing.cc:30
constexpr float ipow(float t)
Definition: easing.cc:20
float smooth_step(float t)
Definition: easing.cc:58
constexpr float blend(float a, float b, float weight)
Definition: easing.cc:25
std::vector< std::pair< const char *, Function > > get_all_values()
Definition: easing.cc:87
float apply(Function f, float t)
Definition: easing.cc:61
constexpr float ipow< 1 >(float t)
Definition: easing.cc:21
constexpr float bounce_clamp_bottom(float t)
Definition: easing.cc:34
constexpr float normalized_bezier_curve3(float b, float c, float t)
Definition: easing.cc:38
constexpr float fracpow(float frac, float t)
Definition: easing.cc:28
constexpr float bounce_clamp_top(float t)
Definition: easing.cc:32