animation/spring: Add a check for from = to in duration()
Browse files at this commitd47b59879a595fca84bd9995babf200faa94325fParent: ef80bcc1 changed file +22 -0
- src/animation/spring.rs +22 -0
src/animation/spring.rsmodified+22 -0
| @@ -54,6 +54,10 @@ impl Spring { | |||
| 54 | return Duration::MAX; | 54 | return Duration::MAX; |
| 55 | } | 55 | } |
| 56 | 56 | ||
| 57 | + if (self.to - self.from).abs() <= f64::EPSILON { | ||
| 58 | + return Duration::ZERO; | ||
| 59 | + } | ||
| 60 | + | ||
| 57 | let omega0 = (self.params.stiffness / self.params.mass).sqrt(); | 61 | let omega0 = (self.params.stiffness / self.params.mass).sqrt(); |
| 58 | 62 | ||
| 59 | // As first ansatz for the overdamped solution, | 63 | // As first ansatz for the overdamped solution, |
| @@ -166,3 +170,21 @@ impl Spring { | |||
| 166 | } | 170 | } |
| 167 | } | 171 | } |
| 168 | } | 172 | } |
| 173 | + | ||
| 174 | +#[cfg(test)] | ||
| 175 | +mod tests { | ||
| 176 | + use super::*; | ||
| 177 | + | ||
| 178 | + #[test] | ||
| 179 | + fn overdamped_spring_equal_from_to_nan() { | ||
| 180 | + let spring = Spring { | ||
| 181 | + from: 0., | ||
| 182 | + to: 0., | ||
| 183 | + initial_velocity: 0., | ||
| 184 | + params: SpringParams::new(1.15, 850., 0.0001), | ||
| 185 | + }; | ||
| 186 | + let _ = spring.duration(); | ||
| 187 | + let _ = spring.clamped_duration(); | ||
| 188 | + let _ = spring.value_at(Duration::ZERO); | ||
| 189 | + } | ||
| 190 | +} | ||