velox_engine/motion/
spring.rs1use crate::svg::math::abs_f32;
7
8#[derive(Copy, Clone, Debug)]
10pub struct SpringConfig {
11 pub stiffness: f32,
13 pub damping: f32,
15 pub mass: f32,
17 pub rest_eps: f32,
19}
20
21impl SpringConfig {
22 pub const GENTLE: Self = Self {
24 stiffness: 120.0,
25 damping: 18.0,
26 mass: 1.0,
27 rest_eps: 0.05,
28 };
29 pub const DEFAULT: Self = Self {
31 stiffness: 220.0,
32 damping: 24.0,
33 mass: 1.0,
34 rest_eps: 0.05,
35 };
36 pub const SNAPPY: Self = Self {
38 stiffness: 380.0,
39 damping: 32.0,
40 mass: 1.0,
41 rest_eps: 0.05,
42 };
43 pub const BOUNCY: Self = Self {
45 stiffness: 260.0,
46 damping: 14.0,
47 mass: 1.0,
48 rest_eps: 0.05,
49 };
50 pub const STIFF: Self = Self {
52 stiffness: 500.0,
53 damping: 42.0,
54 mass: 1.0,
55 rest_eps: 0.05,
56 };
57
58 #[inline(always)]
59 pub const fn new(stiffness: f32, damping: f32, mass: f32) -> Self {
60 Self {
61 stiffness,
62 damping,
63 mass,
64 rest_eps: 0.05,
65 }
66 }
67}
68
69impl Default for SpringConfig {
70 #[inline(always)]
71 fn default() -> Self {
72 Self::DEFAULT
73 }
74}
75
76#[derive(Copy, Clone, Debug)]
78pub struct Spring {
79 value: f32,
80 velocity: f32,
81 target: f32,
82 config: SpringConfig,
83 settled: bool,
84}
85
86impl Spring {
87 #[inline(always)]
89 pub const fn new(initial: f32, config: SpringConfig) -> Self {
90 Self {
91 value: initial,
92 velocity: 0.0,
93 target: initial,
94 config,
95 settled: true,
96 }
97 }
98
99 #[inline(always)]
101 pub const fn value(&self) -> f32 {
102 self.value
103 }
104
105 #[inline(always)]
107 pub fn value_i32(&self) -> i32 {
108 if self.value >= 0.0 {
109 (self.value + 0.5) as i32
110 } else {
111 (self.value - 0.5) as i32
112 }
113 }
114
115 #[inline(always)]
117 pub const fn velocity(&self) -> f32 {
118 self.velocity
119 }
120
121 #[inline(always)]
123 pub const fn target(&self) -> f32 {
124 self.target
125 }
126
127 #[inline(always)]
129 pub const fn is_settled(&self) -> bool {
130 self.settled
131 }
132
133 #[inline(always)]
135 pub const fn is_running(&self) -> bool {
136 !self.settled
137 }
138
139 #[inline]
141 pub fn animate_to(&mut self, target: f32) {
142 if abs_f32(target - self.target) <= 1e-6 && self.settled {
143 return;
144 }
145 self.target = target;
146 self.settled = false;
147 }
148
149 #[inline]
151 pub fn impulse(&mut self, delta_velocity: f32) {
152 self.velocity += delta_velocity;
153 if abs_f32(self.velocity) > self.config.rest_eps {
154 self.settled = false;
155 }
156 }
157
158 #[inline]
160 pub fn set_velocity(&mut self, velocity: f32) {
161 self.velocity = velocity;
162 if abs_f32(self.velocity) > self.config.rest_eps {
163 self.settled = false;
164 }
165 }
166
167 #[inline(always)]
169 pub fn set_config(&mut self, config: SpringConfig) {
170 self.config = config;
171 }
172
173 #[inline(always)]
175 pub fn jump_to(&mut self, value: f32) {
176 self.value = value;
177 self.target = value;
178 self.velocity = 0.0;
179 self.settled = true;
180 }
181
182 pub fn step(&mut self, dt_ms: u16) -> f32 {
184 if self.settled || dt_ms == 0 {
185 return self.value;
186 }
187 let total_s = (dt_ms.min(100) as f32) * 0.001;
189 let max_sub_s = 0.004f32;
190 let sub_steps = ((total_s / max_sub_s) as u32).max(1);
191 let h = total_s / (sub_steps as f32);
192 let inv_m = 1.0 / self.config.mass.max(0.01);
193 let k = self.config.stiffness.max(1.0);
194 let c = self.config.damping.max(0.1);
195
196 for _ in 0..sub_steps {
197 let disp = self.value - self.target;
198 let accel = (-k * disp - c * self.velocity) * inv_m;
199 self.velocity += accel * h;
201 self.value += self.velocity * h;
202 }
203
204 let eps = self.config.rest_eps.max(1e-4);
205 if abs_f32(self.value - self.target) <= eps && abs_f32(self.velocity) <= eps * 10.0 {
206 self.value = self.target;
207 self.velocity = 0.0;
208 self.settled = true;
209 }
210 self.value
211 }
212}