aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorIvan Molodetskikh <yalterz@gmail.com>2025-04-29 10:31:04 +0300
committerIvan Molodetskikh <yalterz@gmail.com>2025-04-29 10:51:53 +0300
commit5f117c61dc4dd91564e02b32836e98dd0e648246 (patch)
tree05789bb953974ca05392193a41210fdd7ae54b74 /src
parentcb857e32e4295822fad39ebf5d9c650f6ead95aa (diff)
downloadniri-5f117c61dc4dd91564e02b32836e98dd0e648246.tar.gz
niri-5f117c61dc4dd91564e02b32836e98dd0e648246.tar.bz2
niri-5f117c61dc4dd91564e02b32836e98dd0e648246.zip
animation/spring: Guard against numerical instability
Diffstat (limited to 'src')
-rw-r--r--src/animation/spring.rs19
1 files changed, 19 insertions, 0 deletions
diff --git a/src/animation/spring.rs b/src/animation/spring.rs
index f7bcb1e4..499ec891 100644
--- a/src/animation/spring.rs
+++ b/src/animation/spring.rs
@@ -94,6 +94,12 @@ impl Spring {
x1 = (self.to - y0 + m * x0) / m;
y1 = self.oscillate(x1);
+
+ // Overdamped springs have some numerical stability issues...
+ if !y1.is_finite() {
+ return Duration::from_secs_f64(x0);
+ }
+
i += 1;
}
@@ -187,4 +193,17 @@ mod tests {
let _ = spring.clamped_duration();
let _ = spring.value_at(Duration::ZERO);
}
+
+ #[test]
+ fn overdamped_spring_duration_panic() {
+ let spring = Spring {
+ from: 0.,
+ to: 1.,
+ initial_velocity: 0.,
+ params: SpringParams::new(6., 1200., 0.0001),
+ };
+ let _ = spring.duration();
+ let _ = spring.clamped_duration();
+ let _ = spring.value_at(Duration::ZERO);
+ }
}