1use serde::{Deserialize, Serialize};
7
8#[derive(Debug, Clone, Copy, Default, Serialize, Deserialize)]
9pub struct Vibe {
10 pub position: f64,
11 pub velocity: f64,
12 pub acceleration: f64,
13 pub damping: f64,
14}
15
16impl Vibe {
17 pub fn new() -> Self {
18 Self { position: 0.0, velocity: 0.0, acceleration: 0.0, damping: 0.99 }
19 }
20 pub fn tick(&mut self, dt: f64) {
21 self.velocity += self.acceleration * dt;
22 self.position += self.velocity * dt;
23 self.velocity *= self.damping;
24 }
25 pub fn nudge(&mut self, force: f64) {
26 self.acceleration += force;
27 }
28 pub fn reset(&mut self) {
29 self.position = 0.0;
30 self.velocity = 0.0;
31 self.acceleration = 0.0;
32 }
33}
34
35#[cfg(test)]
36mod tests {
37 use super::*;
38 #[test]
39 fn test_vibe_motion() {
40 let mut v = Vibe::new();
41 v.nudge(1.0);
42 v.tick(1.0);
43 assert!(v.position > 0.0);
44 }
45}