forge
Explore
Log in

Don't you dare go Hollow

animation: Clamp spring value

Browse files at this commit

4d010b794305d4f94af449ef8fff7f01defd16c9Ivan MolodetskikhParent: 65c342f
I've had an overdamped spring return an extreme value and trip up
an integer overflow check.
src/animation/mod.rsmodified+13 -1
@@ -294,7 +294,19 @@ impl Animation {
294 let x = (passed / total).clamp(0., 1.);294 let x = (passed / total).clamp(0., 1.);
295 curve.y(x) * (self.to - self.from) + self.from295 curve.y(x) * (self.to - self.from) + self.from
296 }296 }
297- Kind::Spring(spring) => spring.value_at(passed),297+ Kind::Spring(spring) => {
298+ let value = spring.value_at(passed);
299+
300+ // Protect against numerical instability.
301+ let range = (self.to - self.from) * 10.;
302+ let a = self.from - range;
303+ let b = self.to + range;
304+ if self.from <= self.to {
305+ value.clamp(a, b)
306+ } else {
307+ value.clamp(b, a)
308+ }
309+ }
298 Kind::Deceleration {310 Kind::Deceleration {
299 initial_velocity,311 initial_velocity,
300 deceleration_rate,312 deceleration_rate,