1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
//! Spring-physics animated value wrapper.
//!
//! `Animated<T>` wraps any value that can be linearly interpolated and provides
//! a spring-damper integration that converges toward a target without overshoot
//! artifacts. Used by the layout engine and view modifiers to drive smooth
//! transitions (e.g. magnetic pull, focus changes, scroll animations).
use std::time::Duration;
/// Configuration for a critically-damped spring.
///
/// The spring follows the equation: `a = -2 * zeta * omega * v - omega^2 * (x - target)`
/// where `zeta` is the damping ratio and `omega = 2 * PI / period`.
#[derive(Debug, Clone, Copy, PartialEq)]
pub struct SpringConfig {
/// Oscillation period in seconds. Smaller = snappier.
pub period: f32,
/// Damping ratio. 1.0 = critically damped (no overshoot).
/// < 1.0 = underdamped (bouncy), > 1.0 = overdamped (sluggish).
pub damping_ratio: f32,
/// Convergence threshold. When both position delta and velocity fall below
/// this, the spring is considered settled.
pub epsilon: f32,
}
impl SpringConfig {
/// A snappy spring (150ms period, critically damped).
pub fn snappy() -> Self {
Self {
period: 0.15,
damping_ratio: 1.0,
epsilon: 0.001,
}
}
/// A smooth spring (300ms period, critically damped).
pub fn smooth() -> Self {
Self {
period: 0.30,
damping_ratio: 1.0,
epsilon: 0.001,
}
}
/// A bouncy spring (400ms period, slightly underdamped).
pub fn bouncy() -> Self {
Self {
period: 0.40,
damping_ratio: 0.7,
epsilon: 0.005,
}
}
}
impl Default for SpringConfig {
fn default() -> Self {
Self::snappy()
}
}
/// A value animated by spring physics toward a target.
///
/// Each call to `update(dt)` advances the simulation by `dt` seconds.
/// Use `set_target()` to change the destination; the spring will
/// automatically converge.
///
/// Type requirements on `T`:
/// - `Copy + Default` for value semantics
/// - `std::ops::Add<Output = T>` for position + velocity integration
/// - `std::ops::Sub<Output = T>` for displacement computation
/// - `std::ops::Mul<f32, Output = T>` for scalar multiplication (velocity, damping)
/// - `f32::from(&T)` via a custom trait for convergence checking
///
/// For `f32`, `glam::Vec2`, `glam::Vec4`, and `Color`, blanket impls are provided.
#[derive(Debug, Clone, Copy, PartialEq)]
pub struct Animated<T: SpringValue> {
pub value: T,
target: T,
velocity: T,
config: SpringConfig,
settled: bool,
}
/// Trait for types that can be used inside `Animated<T>`.
///
/// Provides linear interpolation, scalar multiplication, and convergence detection.
pub trait SpringValue:
Copy + Default + std::ops::Add<Output = Self> + std::ops::Sub<Output = Self>
{
/// Multiply by a scalar.
fn mul_scalar(self, s: f32) -> Self;
/// Element-wise absolute value.
fn abs(self) -> Self;
/// Element-wise maximum with a scalar threshold.
fn max_scalar(self, s: f32) -> Self;
/// Returns true if all components are approximately zero (within epsilon).
fn is_near_zero(self, epsilon: f32) -> bool;
/// Linearly interpolate between self and other by t in [0, 1].
fn lerp(self, other: Self, t: f32) -> Self;
}
impl SpringValue for f32 {
fn mul_scalar(self, s: f32) -> Self {
self * s
}
fn abs(self) -> Self {
self.abs()
}
fn max_scalar(self, s: f32) -> Self {
self.max(s)
}
fn is_near_zero(self, epsilon: f32) -> bool {
self.abs() < epsilon
}
fn lerp(self, other: Self, t: f32) -> Self {
self + (other - self) * t
}
}
impl SpringValue for glam::Vec2 {
fn mul_scalar(self, s: f32) -> Self {
self * s
}
fn abs(self) -> Self {
self.abs()
}
fn max_scalar(self, s: f32) -> Self {
Self::new(self.x.max(s), self.y.max(s))
}
fn is_near_zero(self, epsilon: f32) -> bool {
self.x.abs() < epsilon && self.y.abs() < epsilon
}
fn lerp(self, other: Self, t: f32) -> Self {
self + (other - self) * t
}
}
impl SpringValue for glam::Vec4 {
fn mul_scalar(self, s: f32) -> Self {
self * s
}
fn abs(self) -> Self {
self.abs()
}
fn max_scalar(self, s: f32) -> Self {
Self::new(self.x.max(s), self.y.max(s), self.z.max(s), self.w.max(s))
}
fn is_near_zero(self, epsilon: f32) -> bool {
self.x.abs() < epsilon
&& self.y.abs() < epsilon
&& self.z.abs() < epsilon
&& self.w.abs() < epsilon
}
fn lerp(self, other: Self, t: f32) -> Self {
self + (other - self) * t
}
}
impl<T: SpringValue + std::cmp::PartialEq> Animated<T> {
/// Creates a new animated value starting at `initial` with the given spring config.
pub fn new(initial: T, config: SpringConfig) -> Self {
Self {
value: initial,
target: initial,
velocity: T::default(),
config,
settled: true,
}
}
/// Creates a new animated value starting at `initial` with default (snappy) config.
pub fn default(initial: T) -> Self {
Self::new(initial, SpringConfig::default())
}
/// Sets the target value the spring should converge toward.
pub fn set_target(&mut self, target: T) {
if self.target != target {
self.target = target;
self.settled = false;
}
}
/// Returns the current target value.
pub fn target(&self) -> T {
self.target
}
/// Returns true if the spring has converged and no further updates are needed.
pub fn is_settled(&self) -> bool {
self.settled
}
/// Advances the spring simulation by `dt` seconds.
///
/// Uses a semi-implicit Euler integration of the spring-damper equation:
/// ```text
/// omega = 2 * PI / period
/// displacement = value - target
/// acceleration = -2 * zeta * omega * velocity - omega^2 * displacement
/// velocity += acceleration * dt
/// value += velocity * dt
/// ```
pub fn update(&mut self, dt: f32) {
if self.settled {
return;
}
let omega = 2.0 * std::f32::consts::PI / self.config.period;
let zeta = self.config.damping_ratio;
let displacement = self.value - self.target;
let acceleration =
displacement.mul_scalar(-omega * omega) + self.velocity.mul_scalar(-2.0 * zeta * omega);
self.velocity = self.velocity + acceleration.mul_scalar(dt);
self.value = self.value + self.velocity.mul_scalar(dt);
// Check convergence
if displacement.is_near_zero(self.config.epsilon)
&& self.velocity.is_near_zero(self.config.epsilon)
{
self.value = self.target;
self.velocity = T::default();
self.settled = true;
}
}
/// Advances the spring by a `Duration`.
pub fn update_duration(&mut self, dt: Duration) {
self.update(dt.as_secs_f32());
}
/// Immediately snaps to the target without animation.
pub fn snap_to_target(&mut self) {
self.value = self.target;
self.velocity = T::default();
self.settled = true;
}
/// Resets the value and target to `value` without animation.
pub fn reset(&mut self, value: T) {
self.value = value;
self.target = value;
self.velocity = T::default();
self.settled = true;
}
/// Returns the current spring configuration.
pub fn config(&self) -> SpringConfig {
self.config
}
/// Replaces the spring configuration.
pub fn set_config(&mut self, config: SpringConfig) {
self.config = config;
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn spring_converges_to_target() {
let mut anim = Animated::new(0.0f32, SpringConfig::snappy());
anim.set_target(1.0);
for _ in 0..100 {
anim.update(0.016);
}
assert!(anim.is_settled());
assert!((anim.value - 1.0).abs() < 0.01);
}
#[test]
fn spring_is_initially_settled() {
let anim = Animated::new(5.0f32, SpringConfig::default());
assert!(anim.is_settled());
}
#[test]
fn spring_snap_to_target() {
let mut anim = Animated::new(0.0f32, SpringConfig::snappy());
anim.set_target(10.0);
anim.snap_to_target();
assert!(anim.is_settled());
assert_eq!(anim.value, 10.0);
}
#[test]
fn spring_reset() {
let mut anim = Animated::new(0.0f32, SpringConfig::snappy());
anim.set_target(10.0);
anim.update(0.016);
anim.reset(5.0);
assert!(anim.is_settled());
assert_eq!(anim.value, 5.0);
assert_eq!(anim.target(), 5.0);
}
#[test]
fn spring_vec2_converges() {
let mut anim = Animated::new(glam::Vec2::ZERO, SpringConfig::smooth());
anim.set_target(glam::Vec2::new(100.0, 200.0));
for _ in 0..200 {
anim.update(0.016);
}
assert!(anim.is_settled());
assert!((anim.value.x - 100.0).abs() < 0.01);
assert!((anim.value.y - 200.0).abs() < 0.01);
}
#[test]
fn spring_does_not_overshoot_critically_damped() {
let mut anim = Animated::new(0.0f32, SpringConfig::snappy());
anim.set_target(1.0);
let mut max_val = 0.0f32;
for _ in 0..200 {
anim.update(0.016);
if anim.value > max_val {
max_val = anim.value;
}
}
// Critically damped should not overshoot by more than ~5%
assert!(max_val < 1.05, "overshoot: {}", max_val);
}
}