animation/spring: Guard against numerical instability
Browse files at this commit5f117c61dc4dd91564e02b32836e98dd0e648246Parent: cb857e31 changed file +19 -0
- src/animation/spring.rs +19 -0
src/animation/spring.rsmodified+19 -0
| @@ -94,6 +94,12 @@ impl Spring { | ||
| 94 | 94 | |
| 95 | 95 | x1 = (self.to - y0 + m * x0) / m; |
| 96 | 96 | 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 | + | |
| 97 | 103 | i += 1; |
| 98 | 104 | } |
| 99 | 105 | |
| @@ -187,4 +193,17 @@ mod tests { | ||
| 187 | 193 | let _ = spring.clamped_duration(); |
| 188 | 194 | let _ = spring.value_at(Duration::ZERO); |
| 189 | 195 | } |
| 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 | + } | |
| 190 | 209 | } |