Skip to main content

vibe/animation/families/
dynamics.rs

1//! Physical Dynamics Animation Family (Springs, Bounces, Gravity Wells, Impulses).
2
3use crate::animation::pga::Motor;
4use crate::animation::presets::AnimationSample;
5use crate::animation::spring::{SpringConfig, SpringState1D};
6
7/// Evaluates the families family.
8pub fn eval(preset: &str, t: f64) -> AnimationSample {
9    match preset {
10        "spring_settle" | "spring" => {
11            let config = SpringConfig::default();
12            let state = SpringState1D::new(0.0, 0.0, 1.0);
13            let (pos, vel, settled) = state.evaluate_at(&config, t);
14            AnimationSample {
15                scalar: pos,
16                vector: [pos, 0.0, 0.0],
17                motor: Motor::from_translation(pos, 0.0, 0.0),
18                secondary: vel,
19                settled,
20            }
21        }
22        "bouncy_drop" | "bounce" => {
23            let config = SpringConfig::bouncy();
24            let state = SpringState1D::new(10.0, 0.0, 0.0);
25            let (pos, vel, settled) = state.evaluate_at(&config, t);
26            let y = pos.abs();
27            AnimationSample {
28                scalar: y,
29                vector: [0.0, y, 0.0],
30                motor: Motor::from_translation(0.0, y, 0.0),
31                secondary: vel,
32                settled,
33            }
34        }
35        "verlet_wave" => {
36            let k = 2.0;
37            let omega = 3.0;
38            let height = (k * 1.0 - omega * t).sin();
39            let slope = (k * 1.0 - omega * t).cos() * k;
40            AnimationSample {
41                scalar: height,
42                vector: [0.0, height, 0.0],
43                motor: Motor::from_rotation_translation(
44                    [1.0, 0.0, slope * 0.1, 0.0],
45                    [0.0, height, 0.0],
46                ),
47                secondary: slope,
48                settled: false,
49            }
50        }
51        "gravity_well" => {
52            let g = 9.81;
53            let m = 100.0;
54            let dist = (10.0 - t * 2.0).max(0.5);
55            let force = (g * m) / (dist * dist);
56            AnimationSample {
57                scalar: force,
58                vector: [dist, 0.0, 0.0],
59                motor: Motor::from_translation(dist, 0.0, 0.0),
60                secondary: force,
61                settled: dist <= 0.5,
62            }
63        }
64        "collision_rebound" => {
65            let decay = (-t * 4.0).exp();
66            let impulse = (t * 20.0).cos() * decay;
67            AnimationSample {
68                scalar: impulse,
69                vector: [0.0, impulse, 0.0],
70                motor: Motor::from_translation(0.0, impulse, 0.0),
71                secondary: decay,
72                settled: decay < 0.001,
73            }
74        }
75        _ => AnimationSample::default(),
76    }
77}