veloren_voxygen_anim/
util.rs

1use std::f32::consts::PI;
2
3// Useful easing functions
4// Source: https://easings.net/
5pub fn bounce(x: f32) -> f32 {
6    if x < (1.0 / 2.75) {
7        7.5625 * x.powi(2)
8    } else if x < (2.0 / 2.75) {
9        7.5625 * (x - (1.5 / 2.75)).powi(2) + 0.75
10    } else if x < (2.5 / 2.75) {
11        7.5625 * (x - (2.25 / 2.75)).powi(2) + 0.9375
12    } else {
13        7.5625 * (x - (2.625 / 2.75)).powi(2) + 0.984375
14    }
15}
16
17// Source: https://easings.net/
18pub fn elastic(x: f32) -> f32 {
19    fn f(x: f32, a: f32, b: f32) -> f32 {
20        let p = 0.8;
21        b + a * 2.0_f32.powf(a * 10.0 * x) * ((4.0 * PI * x) / p).cos()
22    }
23    f(x, -1.0, 1.0) / f(1.0, -1.0, 1.0)
24}
25
26// Source: https://easings.net/
27pub fn ease_in_back(x: f32) -> f32 {
28    let a = 1.70158;
29    let b = a + 1.0;
30    b * x.powi(3) - a * x.powi(2)
31}
32
33pub fn out_and_in(x: f32) -> f32 { (x - 0.5).powi(2) - 0.25 }