From 5f117c61dc4dd91564e02b32836e98dd0e648246 Mon Sep 17 00:00:00 2001 From: Ivan Molodetskikh Date: Tue, 29 Apr 2025 10:31:04 +0300 Subject: animation/spring: Guard against numerical instability --- src/animation/spring.rs | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) (limited to 'src') diff --git a/src/animation/spring.rs b/src/animation/spring.rs index f7bcb1e4..499ec891 100644 --- a/src/animation/spring.rs +++ b/src/animation/spring.rs @@ -94,6 +94,12 @@ impl Spring { x1 = (self.to - y0 + m * x0) / m; y1 = self.oscillate(x1); + + // Overdamped springs have some numerical stability issues... + if !y1.is_finite() { + return Duration::from_secs_f64(x0); + } + i += 1; } @@ -187,4 +193,17 @@ mod tests { let _ = spring.clamped_duration(); let _ = spring.value_at(Duration::ZERO); } + + #[test] + fn overdamped_spring_duration_panic() { + let spring = Spring { + from: 0., + to: 1., + initial_velocity: 0., + params: SpringParams::new(6., 1200., 0.0001), + }; + let _ = spring.duration(); + let _ = spring.clamped_duration(); + let _ = spring.value_at(Duration::ZERO); + } } -- cgit