animation: Clamp spring value

I've had an overdamped spring return an extreme value and trip up
an integer overflow check.
This commit is contained in:
Ivan Molodetskikh
2024-04-18 20:42:01 +04:00
parent 65c342f2cb
commit 4d010b7943
+13 -1
View File
@@ -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,