Gamedev Framework (gf)  0.6.0
A C++11 framework for 2D games
Easings.h
1 /*
2  * Gamedev Framework (gf)
3  * Copyright (C) 2016-2017 Julien Bernard
4  *
5  * This software is provided 'as-is', without any express or implied
6  * warranty. In no event will the authors be held liable for any damages
7  * arising from the use of this software.
8  *
9  * Permission is granted to anyone to use this software for any purpose,
10  * including commercial applications, and to alter it and redistribute it
11  * freely, subject to the following restrictions:
12  *
13  * 1. The origin of this software must not be misrepresented; you must not
14  * claim that you wrote the original software. If you use this software
15  * in a product, an acknowledgment in the product documentation would be
16  * appreciated but is not required.
17  * 2. Altered source versions must be plainly marked as such, and must not be
18  * misrepresented as being the original software.
19  * 3. This notice may not be removed or altered from any source distribution.
20  *
21  * Part of this file comes from SFML, with the same license:
22  * Copyright (C) 2007-2015 Laurent Gomila (laurent@sfml-dev.org)
23  */
24 #ifndef GF_EASINGS_H
25 #define GF_EASINGS_H
26 
27 #include <cmath>
28 
29 #include "Math.h"
30 #include "Portability.h"
31 
32 namespace gf {
33 #ifndef DOXYGEN_SHOULD_SKIP_THIS
34 inline namespace v1 {
35 #endif
36 
37  /**
38  * @ingroup core
39  * @brief An easing function
40  *
41  * An easing function is a function that specifies how a value changes over
42  * time. A normalized easing function has the following properties:
43  *
44  * - @f$ f(0) = 0 @f$
45  * - @f$ f(1) = 1 @f$
46  *
47  * Usual easing functions are provided in gf::Ease.
48  *
49  * @sa gf::Ease, gf::Step
50  */
51  using Easing = float (*)(float);
52 
53 
54  /**
55  * @ingroup core
56  * @brief Predefined easing functions
57  *
58  * This class defines usual easing functions.
59  *
60  * # First set of easing functions
61  *
62  * The first set of functions are common interpolation functions.
63  *
64  * - linear
65  * @f[ f(t) = t @f]
66  * - smooth
67  * @f[ f(t) = -2 * t^3 + 3 * t^2 @f]
68  * - smoother
69  * @f[ f(t) = 6 * t^5 - 15 * t^4 + 10 * t^3 @f]
70  *
71  * Here are the plots of these functions:
72  *
73  * <table>
74  * <tr>
75  * <th>Functions</th>
76  * <th>Plot</th>
77  * </tr>
78  * <tr>
79  * <td>linear</td>
80  * <td> @image html linear.png </td>
81  * </tr>
82  * <tr>
83  * <td>smooth</td>
84  * <td> @image html smooth.png </td>
85  * </tr>
86  * <tr>
87  * <td>smoother</td>
88  * <td> @image html smoother.png </td>
89  * </tr>
90  * </table>
91  *
92  * # Second set of easing functions
93  *
94  * ## Definitions
95  *
96  * The second set are the easing functions defined by Robert Penner. From a
97  * base function @f$ f(t) @f$, four flavors are defined:
98  *
99  * - Ease-In: start slow and speed up at the end
100  * @f[ \mathtt{easeIn}(t) = f(t) @f]
101  * - Ease-Out: start fast and slow down at the end
102  * @f[ \mathtt{easeOut}(t) = 1 - \mathtt{easeIn}(1 - t) @f]
103  * - Ease-In-Out: start slow, then speed up in the middle and finally slow down at the end
104  * @f[ \mathtt{easeInOut}(t) =
105  * \begin{cases}
106  * \mathtt{easeIn}(2 \times t) \div 2 & \text{if } t < 0.5 \\
107  * \mathtt{easeOut}(2 \times t + 1) \div 2 + 0.5 & \text{if } t \geq 0.5
108  * \end{cases}
109  * @f]
110  * - Ease-Out-In: start fast, then slow down in the middle and finally speed up at the end
111  * @f[ \mathtt{easeOutIn}(t) =
112  * \begin{cases}
113  * \mathtt{easeOut}(2 \times t) \div 2 & \text{if } t < 0.5 \\
114  * \mathtt{easeIn}(2 \times t + 1) \div 2 + 0.5 & \text{if } t \geq 0.5
115  * \end{cases}
116  * @f]
117  *
118  * ## Base functions
119  *
120  * Here are the base functions:
121  *
122  * - quad
123  * @f[ f(t) = t^2 @f]
124  * - cubic
125  * @f[ f(t) = t^3 @f]
126  * - quart
127  * @f[ f(t) = t^4 @f]
128  * - quint
129  * @f[ f(t) = t^5 @f]
130  * - sine
131  * @f[ f(t) = 1 - \cos\left(t \times \frac{\pi}{2}\right)@f]
132  * - expo
133  * @f[ f(t) = \begin{cases} 0 & \text{if } t = 0 \\ 2^{10 \times (t - 1)} & \text{if } t > 0 \end{cases} @f]
134  * - circ
135  * @f[ f(t) = 1 - \sqrt{1 - t^2} @f]
136  * - back
137  * @f[ f(t) = t^2 \times (2.70158 \times t - 1.70158) @f]
138  * - bounce
139  * @f[ f(t) =
140  * \begin{cases}
141  * 1 - 7.5625 \times (1 - t)^2 & \text{if } 1 - t < 1 / 2.75 \\
142  * 1 - (7.5625 \times (1 - t - 1.5 / 2.75)^2 + 0.75) & \text{if } 1 - t < 2 / 2.75 \\
143  * 1 - (7.5625 \times (1 - t - 2.25 / 2.75)^2 + 0.9375) & \text{if } 1 - t < 2.5 / 2.75 \\
144  * 1 - (7.5625 \times (1 - t - 2.625 / 2.75)^2 + 0.984375) & \text{otherwise}
145  * \end{cases}
146  * @f]
147  * - elastic
148  * @f[ f(t) = -2^{10 \times (t - 1)} \times \sin\left(\left((t - 1) - \frac{0.3}{4}\right) \times \frac{2 \times \pi}{0.3}\right) @f]
149  *
150  * ## Plots
151  *
152  * Here are the plots for all the defined easing functions:
153  *
154  * <table>
155  * <tr>
156  * <th>Function</th>
157  * <th>Ease-In</th>
158  * <th>Ease-Out</th>
159  * <th>Ease-In-Out</th>
160  * <th>Ease-Out-In</th>
161  * </tr>
162  * <tr>
163  * <td>quad</th>
164  * <td> @image html quadin.png </td>
165  * <td> @image html quadout.png </td>
166  * <td> @image html quadinout.png </td>
167  * <td> @image html quadoutin.png </td>
168  * </tr>
169  * <tr>
170  * <td>cubic</th>
171  * <td> @image html cubicin.png </td>
172  * <td> @image html cubicout.png </td>
173  * <td> @image html cubicinout.png </td>
174  * <td> @image html cubicoutin.png </td>
175  * </tr>
176  * <tr>
177  * <td>quart</th>
178  * <td> @image html quartin.png </td>
179  * <td> @image html quartout.png </td>
180  * <td> @image html quartinout.png </td>
181  * <td> @image html quartoutin.png </td>
182  * </tr>
183  * <tr>
184  * <td>quint</th>
185  * <td> @image html quintin.png </td>
186  * <td> @image html quintout.png </td>
187  * <td> @image html quintinout.png </td>
188  * <td> @image html quintoutin.png </td>
189  * </tr>
190  * <tr>
191  * <td>sine</th>
192  * <td> @image html sinein.png </td>
193  * <td> @image html sineout.png </td>
194  * <td> @image html sineinout.png </td>
195  * <td> @image html sineoutin.png </td>
196  * </tr>
197  * <tr>
198  * <td>expo</th>
199  * <td> @image html expoin.png </td>
200  * <td> @image html expoout.png </td>
201  * <td> @image html expoinout.png </td>
202  * <td> @image html expooutin.png </td>
203  * </tr>
204  * <tr>
205  * <td>circ</th>
206  * <td> @image html circin.png </td>
207  * <td> @image html circout.png </td>
208  * <td> @image html circinout.png </td>
209  * <td> @image html circoutin.png </td>
210  * </tr>
211  * <tr>
212  * <td>back</th>
213  * <td> @image html backin.png </td>
214  * <td> @image html backout.png </td>
215  * <td> @image html backinout.png </td>
216  * <td> @image html backoutin.png </td>
217  * </tr>
218  * <tr>
219  * <td>bounce</th>
220  * <td> @image html bouncein.png </td>
221  * <td> @image html bounceout.png </td>
222  * <td> @image html bounceinout.png </td>
223  * <td> @image html bounceoutin.png </td>
224  * </tr>
225  * <tr>
226  * <td>elastic</th>
227  * <td> @image html elasticin.png </td>
228  * <td> @image html elasticout.png </td>
229  * <td> @image html elasticinout.png </td>
230  * <td> @image html elasticoutin.png </td>
231  * </tr>
232  * </table>
233  *
234  *
235  * @sa gf::Easing
236  * @sa [Robert Penner's Easing Functions](http://robertpenner.com/easing/)
237  * @sa [Easing Functions Cheat Sheet](http://easings.net/)
238  */
239  class GF_API Ease {
240  public:
241 
242  /**
243  * @brief Linear easing
244  *
245  * This is the default easing function
246  */
247  static float linear(float t);
248 
249  /**
250  * @brief Smooth easing
251  *
252  * @sa [Smoothstep - Wikipedia](https://en.wikipedia.org/wiki/Smoothstep)
253  */
254  static float smooth(float t);
255 
256  /**
257  * @brief Smoother easing
258  *
259  * @sa [Smootherstep - Wikipedia](https://en.wikipedia.org/wiki/Smoothstep#Variations)
260  */
261  static float smoother(float t);
262 
263  /**
264  * @brief Ease-In flavor of quad easing
265  */
266  static float quadIn(float t);
267 
268  /**
269  * @brief Ease-Out flavor of quad easing
270  */
271  static float quadOut(float t);
272 
273  /**
274  * @brief Ease-In-Out flavor of quad easing
275  */
276  static float quadInOut(float t);
277 
278  /**
279  * @brief Ease-Out-In flavor of quad easing
280  */
281  static float quadOutIn(float t);
282 
283  /**
284  * @brief Ease-In flavor of cubic easing
285  */
286  static float cubicIn(float t);
287 
288  /**
289  * @brief Ease-Out flavor of cubic easing
290  */
291  static float cubicOut(float t);
292 
293  /**
294  * @brief Ease-In-Out flavor of cubic easing
295  */
296  static float cubicInOut(float t);
297 
298  /**
299  * @brief Ease-Out-In flavor of cubic easing
300  */
301  static float cubicOutIn(float t);
302 
303  /**
304  * @brief Ease-In flavor of quart easing
305  */
306  static float quartIn(float t);
307 
308  /**
309  * @brief Ease-Out flavor of quart easing
310  */
311  static float quartOut(float t);
312 
313  /**
314  * @brief Ease-In-Out flavor of quart easing
315  */
316  static float quartInOut(float t);
317 
318  /**
319  * @brief Ease-Out-In flavor of quart easing
320  */
321  static float quartOutIn(float t);
322 
323  /**
324  * @brief Ease-In flavor of quint easing
325  */
326  static float quintIn(float t);
327 
328  /**
329  * @brief Ease-Out flavor of quint easing
330  */
331  static float quintOut(float t);
332 
333  /**
334  * @brief Ease-In-Out flavor of quint easing
335  */
336  static float quintInOut(float t);
337 
338  /**
339  * @brief Ease-Out-In flavor of quint easing
340  */
341  static float quintOutIn(float t);
342 
343  /**
344  * @brief Ease-In flavor of circ easing
345  */
346  static float circIn(float t);
347 
348  /**
349  * @brief Ease-Out flavor of circ easing
350  */
351  static float circOut(float t);
352 
353  /**
354  * @brief Ease-In-Out flavor of circ easing
355  */
356  static float circInOut(float t);
357 
358  /**
359  * @brief Ease-Out-In flavor of circ easing
360  */
361  static float circOutIn(float t);
362 
363  /**
364  * @brief Ease-In flavor of sine easing
365  */
366  static float sineIn(float t);
367 
368  /**
369  * @brief Ease-Out flavor of sine easing
370  */
371  static float sineOut(float t);
372 
373  /**
374  * @brief Ease-In-Out flavor of sine easing
375  */
376  static float sineInOut(float t);
377 
378  /**
379  * @brief Ease-Out-In flavor of sine easing
380  */
381  static float sineOutIn(float t);
382 
383  /**
384  * @brief Ease-In flavor of back easing
385  */
386  static float backIn(float t);
387 
388  /**
389  * @brief Ease-Out flavor of back easing
390  */
391  static float backOut(float t);
392 
393  /**
394  * @brief Ease-In-Out flavor of back easing
395  */
396  static float backInOut(float t);
397 
398  /**
399  * @brief Ease-Out-In flavor of back easing
400  */
401  static float backOutIn(float t);
402 
403  /**
404  * @brief Ease-In flavor of bounce easing
405  */
406  static float bounceIn(float t);
407 
408  /**
409  * @brief Ease-Out flavor of bounce easing
410  */
411  static float bounceOut(float t);
412 
413  /**
414  * @brief Ease-Out-In flavor of bounce easing
415  */
416  static float bounceInOut(float t);
417 
418  /**
419  * @brief Ease-In-Out flavor of bounce easing
420  */
421  static float bounceOutIn(float t);
422 
423  /**
424  * @brief Ease-In flavor of elastic easing
425  */
426  static float elasticIn(float t);
427 
428  /**
429  * @brief Ease-Out flavor of elastic easing
430  */
431  static float elasticOut(float t);
432 
433  /**
434  * @brief Ease-In-Out flavor of elastic easing
435  */
436  static float elasticInOut(float t);
437 
438  /**
439  * @brief Ease-Out-In flavor of elastic easing
440  */
441  static float elasticOutIn(float t);
442 
443  /**
444  * @brief Ease-In flavor of expo easing
445  */
446  static float expoIn(float t);
447 
448  /**
449  * @brief Ease-Out flavor of expo easing
450  */
451  static float expoOut(float t);
452 
453  /**
454  * @brief Ease-In-Out flavor of expo easing
455  */
456  static float expoInOut(float t);
457 
458  /**
459  * @brief Ease-Out-In flavor of expo easing
460  */
461  static float expoOutIn(float t);
462 
463  /**
464  * @brief Deleted constructor
465  */
466  Ease() = delete;
467 
468  };
469 
470 #ifndef DOXYGEN_SHOULD_SKIP_THIS
471 }
472 #endif
473 }
474 
475 #endif // GF_EASINGS_H
static float bounceIn(float t)
Ease-In flavor of bounce easing.
static float sineOutIn(float t)
Ease-Out-In flavor of sine easing.
static float backOut(float t)
Ease-Out flavor of back easing.
static float quadOut(float t)
Ease-Out flavor of quad easing.
static float quartInOut(float t)
Ease-In-Out flavor of quart easing.
static float expoOut(float t)
Ease-Out flavor of expo easing.
Ease()=delete
Deleted constructor.
static float circOut(float t)
Ease-Out flavor of circ easing.
static float quintInOut(float t)
Ease-In-Out flavor of quint easing.
static float sineOut(float t)
Ease-Out flavor of sine easing.
static float quadIn(float t)
Ease-In flavor of quad easing.
static float backIn(float t)
Ease-In flavor of back easing.
static float cubicOutIn(float t)
Ease-Out-In flavor of cubic easing.
static float bounceInOut(float t)
Ease-Out-In flavor of bounce easing.
static float quintOutIn(float t)
Ease-Out-In flavor of quint easing.
static float quadInOut(float t)
Ease-In-Out flavor of quad easing.
static float cubicInOut(float t)
Ease-In-Out flavor of cubic easing.
static float backOutIn(float t)
Ease-Out-In flavor of back easing.
static float cubicIn(float t)
Ease-In flavor of cubic easing.
static float circOutIn(float t)
Ease-Out-In flavor of circ easing.
static float quartOutIn(float t)
Ease-Out-In flavor of quart easing.
static float quadOutIn(float t)
Ease-Out-In flavor of quad easing.
static float elasticOutIn(float t)
Ease-Out-In flavor of elastic easing.
static float backInOut(float t)
Ease-In-Out flavor of back easing.
static float quartIn(float t)
Ease-In flavor of quart easing.
The namespace for gf classes.
Definition: Action.h:34
static float quintIn(float t)
Ease-In flavor of quint easing.
static float linear(float t)
Linear easing.
static float bounceOut(float t)
Ease-Out flavor of bounce easing.
static float quartOut(float t)
Ease-Out flavor of quart easing.
static float quintOut(float t)
Ease-Out flavor of quint easing.
static float circInOut(float t)
Ease-In-Out flavor of circ easing.
static float expoInOut(float t)
Ease-In-Out flavor of expo easing.
Predefined easing functions.
Definition: Easings.h:239
static float circIn(float t)
Ease-In flavor of circ easing.
static float sineInOut(float t)
Ease-In-Out flavor of sine easing.
static float expoOutIn(float t)
Ease-Out-In flavor of expo easing.
static float expoIn(float t)
Ease-In flavor of expo easing.
static float sineIn(float t)
Ease-In flavor of sine easing.
static float smoother(float t)
Smoother easing.
static float smooth(float t)
Smooth easing.
static float bounceOutIn(float t)
Ease-In-Out flavor of bounce easing.
#define GF_API
Definition: Portability.h:35
static float elasticInOut(float t)
Ease-In-Out flavor of elastic easing.
static float elasticIn(float t)
Ease-In flavor of elastic easing.
static float cubicOut(float t)
Ease-Out flavor of cubic easing.
static float elasticOut(float t)
Ease-Out flavor of elastic easing.