From 4d010b794305d4f94af449ef8fff7f01defd16c9 Mon Sep 17 00:00:00 2001 From: Ivan Molodetskikh Date: Thu, 18 Apr 2024 20:42:01 +0400 Subject: animation: Clamp spring value I've had an overdamped spring return an extreme value and trip up an integer overflow check. --- src/animation/mod.rs | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/src/animation/mod.rs b/src/animation/mod.rs index efdc8955..7ab0c585 100644 --- a/src/animation/mod.rs +++ b/src/animation/mod.rs @@ -294,7 +294,19 @@ impl Animation { let x = (passed / total).clamp(0., 1.); curve.y(x) * (self.to - self.from) + self.from } - Kind::Spring(spring) => spring.value_at(passed), + Kind::Spring(spring) => { + let value = spring.value_at(passed); + + // Protect against numerical instability. + let range = (self.to - self.from) * 10.; + let a = self.from - range; + let b = self.to + range; + if self.from <= self.to { + value.clamp(a, b) + } else { + value.clamp(b, a) + } + } Kind::Deceleration { initial_velocity, deceleration_rate, -- cgit