forge
Explore
Log in

Don't you dare go Hollow

animation/spring: Guard against numerical instability

Browse files at this commit

5f117c61dc4dd91564e02b32836e98dd0e648246Ivan MolodetskikhParent: cb857e3
src/animation/spring.rsmodified+19 -0
@@ -94,6 +94,12 @@ impl Spring {
9494
9595 x1 = (self.to - y0 + m * x0) / m;
9696 y1 = self.oscillate(x1);
97+
98+ // Overdamped springs have some numerical stability issues...
99+ if !y1.is_finite() {
100+ return Duration::from_secs_f64(x0);
101+ }
102+
97103 i += 1;
98104 }
99105
@@ -187,4 +193,17 @@ mod tests {
187193 let _ = spring.clamped_duration();
188194 let _ = spring.value_at(Duration::ZERO);
189195 }
196+
197+ #[test]
198+ fn overdamped_spring_duration_panic() {
199+ let spring = Spring {
200+ from: 0.,
201+ to: 1.,
202+ initial_velocity: 0.,
203+ params: SpringParams::new(6., 1200., 0.0001),
204+ };
205+ let _ = spring.duration();
206+ let _ = spring.clamped_duration();
207+ let _ = spring.value_at(Duration::ZERO);
208+ }
190209 }