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]
101 pub fn from_gesture_velocity(
102 initial: f32,
103 target: f32,
104 velocity_px_per_sec: f32,
105 config: SpringConfig,
106 ) -> Self {
107 let mut sp = Self {
108 value: initial,
109 velocity: velocity_px_per_sec,
110 target,
111 config,
112 settled: false,
113 };
114 let eps = config.rest_eps.max(1e-4);
115 if abs_f32(initial - target) <= eps && abs_f32(velocity_px_per_sec) <= eps * 10.0 {
116 sp.value = target;
117 sp.velocity = 0.0;
118 sp.settled = true;
119 }
120 sp
121 }
122
123 #[inline(always)]
125 pub const fn value(&self) -> f32 {
126 self.value
127 }
128
129 #[inline(always)]
131 pub fn value_i32(&self) -> i32 {
132 if self.value >= 0.0 {
133 (self.value + 0.5) as i32
134 } else {
135 (self.value - 0.5) as i32
136 }
137 }
138
139 #[inline(always)]
141 pub const fn velocity(&self) -> f32 {
142 self.velocity
143 }
144
145 #[inline(always)]
147 pub const fn target(&self) -> f32 {
148 self.target
149 }
150
151 #[inline(always)]
153 pub const fn is_settled(&self) -> bool {
154 self.settled
155 }
156
157 #[inline(always)]
159 pub const fn is_running(&self) -> bool {
160 !self.settled
161 }
162
163 #[inline]
165 pub fn animate_to(&mut self, target: f32) {
166 if abs_f32(target - self.target) <= 1e-6 && self.settled {
167 return;
168 }
169 self.target = target;
170 self.settled = false;
171 }
172
173 #[inline]
175 pub fn impulse(&mut self, delta_velocity: f32) {
176 self.velocity += delta_velocity;
177 if abs_f32(self.velocity) > self.config.rest_eps {
178 self.settled = false;
179 }
180 }
181
182 #[inline]
184 pub fn set_velocity(&mut self, velocity: f32) {
185 self.velocity = velocity;
186 if abs_f32(self.velocity) > self.config.rest_eps {
187 self.settled = false;
188 }
189 }
190
191 #[inline(always)]
193 pub fn set_config(&mut self, config: SpringConfig) {
194 self.config = config;
195 }
196
197 #[inline(always)]
199 pub fn jump_to(&mut self, value: f32) {
200 self.value = value;
201 self.target = value;
202 self.velocity = 0.0;
203 self.settled = true;
204 }
205
206 pub fn step(&mut self, dt_ms: u16) -> f32 {
208 if self.settled || dt_ms == 0 {
209 return self.value;
210 }
211 let total_s = (dt_ms.min(100) as f32) * 0.001;
213 let max_sub_s = 0.004f32;
214 let sub_steps = ((total_s / max_sub_s) as u32).max(1);
215 let h = total_s / (sub_steps as f32);
216 let inv_m = 1.0 / self.config.mass.max(0.01);
217 let k = self.config.stiffness.max(1.0);
218 let c = self.config.damping.max(0.1);
219
220 for _ in 0..sub_steps {
221 let disp = self.value - self.target;
222 let accel = (-k * disp - c * self.velocity) * inv_m;
223 self.velocity += accel * h;
225 self.value += self.velocity * h;
226 }
227
228 let eps = self.config.rest_eps.max(1e-4);
229 if abs_f32(self.value - self.target) <= eps && abs_f32(self.velocity) <= eps * 10.0 {
230 self.value = self.target;
231 self.velocity = 0.0;
232 self.settled = true;
233 }
234 self.value
235 }
236}
237
238#[inline]
245pub fn rubberband(distance: f32, dimension: f32, constant: f32) -> f32 {
246 let dim = dimension.max(1.0);
247 let c = constant.clamp(0.0, 1.0);
248 if c == 0.0 || distance == 0.0 {
249 return 0.0;
250 }
251 let abs_d = abs_f32(distance);
252 (distance * dim * c) / (dim + c * abs_d)
253}
254
255#[inline]
257pub fn rubberband_i16(distance: i16, dimension: i16, constant_q8: u16) -> i16 {
258 let d = distance as i64;
259 let dim = (dimension.max(1)) as i64;
260 let c = (constant_q8.min(256)) as i64;
261 if d == 0 || c == 0 {
262 return 0;
263 }
264 let abs_d = d.abs();
265 let num = d * dim * c;
267 let den = (dim << 8) + c * abs_d;
268 (num / den.max(1)).clamp(i16::MIN as i64, i16::MAX as i64) as i16
269}