diff options
Diffstat (limited to 'src/animation/spring.rs')
| -rw-r--r-- | src/animation/spring.rs | 31 |
1 files changed, 31 insertions, 0 deletions
diff --git a/src/animation/spring.rs b/src/animation/spring.rs index 6e100f05..506b01a3 100644 --- a/src/animation/spring.rs +++ b/src/animation/spring.rs @@ -96,6 +96,37 @@ impl Spring { Duration::from_secs_f64(x1) } + /// Computes and returns the duration until the spring reaches its target position. + pub fn clamped_duration(&self) -> Duration { + let beta = self.params.damping / (2. * self.params.mass); + + if beta.abs() <= f64::EPSILON || beta < 0. { + return Duration::MAX; + } + + if (self.to - self.from).abs() <= f64::EPSILON { + return Duration::ZERO; + } + + // The first frame is not that important and we avoid finding the trivial 0 for in-place + // animations. + let mut i = 1u16; + let mut y = self.oscillate(f64::from(i) / 1000.); + + while (self.to - self.from > f64::EPSILON && self.to - y > self.params.epsilon) + || (self.from - self.to > f64::EPSILON && y - self.to > self.params.epsilon) + { + if i > 1000 { + return Duration::ZERO; + } + + i += 1; + y = self.oscillate(f64::from(i) / 1000.); + } + + Duration::from_millis(u64::from(i)) + } + /// Returns the spring position at a given time in seconds. fn oscillate(&self, t: f64) -> f64 { let b = self.params.damping; |
