1use std::fmt::{self, Debug};
4
5use waft_embedder::clamp_double;
6use waft_physics::{
7 FrictionSimulation, ScrollSpringSimulation, Simulation, SpringDescription, Tolerance,
8};
9
10pub struct BouncingScrollSimulation {
16 pub leading_extent: f64,
20
21 pub trailing_extent: f64,
25
26 pub spring: SpringDescription,
30
31 friction_simulation: Option<FrictionSimulation>,
32 spring_simulation: Option<ScrollSpringSimulation>,
33 spring_time: f64,
34
35 pub tolerance: Tolerance,
39}
40
41impl BouncingScrollSimulation {
42 pub fn new(
57 position: f64,
58 velocity: f64,
59 leading_extent: f64,
60 trailing_extent: f64,
61 spring: SpringDescription,
62 ) -> BouncingScrollSimulation {
63 BouncingScrollSimulation::with_options(
64 position,
65 velocity,
66 leading_extent,
67 trailing_extent,
68 spring,
69 0.0,
70 Tolerance::DEFAULT_TOLERANCE,
71 )
72 }
73
74 pub fn with_options(
76 position: f64,
77 velocity: f64,
78 leading_extent: f64,
79 trailing_extent: f64,
80 spring: SpringDescription,
81 constant_deceleration: f64,
82 tolerance: Tolerance,
83 ) -> BouncingScrollSimulation {
84 debug_assert!(leading_extent <= trailing_extent);
85 let mut simulation = BouncingScrollSimulation {
86 leading_extent,
87 trailing_extent,
88 spring,
89 friction_simulation: None,
90 spring_simulation: None,
91 spring_time: 0.0,
92 tolerance,
93 };
94 if position < leading_extent {
95 simulation.spring_simulation =
96 Some(simulation.underscroll_simulation(position, velocity));
97 simulation.spring_time = f64::NEG_INFINITY;
98 } else if position > trailing_extent {
99 simulation.spring_simulation =
100 Some(simulation.overscroll_simulation(position, velocity));
101 simulation.spring_time = f64::NEG_INFINITY;
102 } else {
103 let friction_simulation = FrictionSimulation::with_options(
106 0.135,
107 position,
108 velocity,
109 tolerance,
110 constant_deceleration,
111 );
112 let final_x = friction_simulation.final_x();
113 if velocity > 0.0 && final_x > trailing_extent {
114 simulation.spring_time = friction_simulation.time_at_x(trailing_extent);
115 simulation.spring_simulation = Some(
116 simulation.overscroll_simulation(
117 trailing_extent,
118 friction_simulation
119 .dx(simulation.spring_time)
120 .min(BouncingScrollSimulation::MAX_SPRING_TRANSFER_VELOCITY),
121 ),
122 );
123 debug_assert!(simulation.spring_time.is_finite());
124 } else if velocity < 0.0 && final_x < leading_extent {
125 simulation.spring_time = friction_simulation.time_at_x(leading_extent);
126 simulation.spring_simulation = Some(
127 simulation.underscroll_simulation(
128 leading_extent,
129 friction_simulation
130 .dx(simulation.spring_time)
131 .min(BouncingScrollSimulation::MAX_SPRING_TRANSFER_VELOCITY),
132 ),
133 );
134 debug_assert!(simulation.spring_time.is_finite());
135 } else {
136 simulation.spring_time = f64::INFINITY;
137 }
138 simulation.friction_simulation = Some(friction_simulation);
139 }
140 simulation
141 }
142
143 pub const MAX_SPRING_TRANSFER_VELOCITY: f64 = 5000.0;
146
147 fn underscroll_simulation(&self, x: f64, dx: f64) -> ScrollSpringSimulation {
148 ScrollSpringSimulation::new(self.spring, x, self.leading_extent, dx, self.tolerance)
149 }
150
151 fn overscroll_simulation(&self, x: f64, dx: f64) -> ScrollSpringSimulation {
152 ScrollSpringSimulation::new(self.spring, x, self.trailing_extent, dx, self.tolerance)
153 }
154
155 fn simulation(&self, time: f64) -> (&dyn Simulation, f64) {
158 if time > self.spring_time {
159 let time_offset = if self.spring_time.is_finite() {
160 self.spring_time
161 } else {
162 0.0
163 };
164 let spring = self
165 .spring_simulation
166 .as_ref()
167 .expect("a spring time below infinity means a spring simulation was built");
168 (spring, time_offset)
169 } else {
170 let friction = self
171 .friction_simulation
172 .as_ref()
173 .expect("a finite spring time means the friction simulation was built");
174 (friction, 0.0)
175 }
176 }
177}
178
179impl Simulation for BouncingScrollSimulation {
180 fn x(&self, time: f64) -> f64 {
181 let (simulation, time_offset) = self.simulation(time);
182 simulation.x(time - time_offset)
183 }
184
185 fn dx(&self, time: f64) -> f64 {
186 let (simulation, time_offset) = self.simulation(time);
187 simulation.dx(time - time_offset)
188 }
189
190 fn is_done(&self, time: f64) -> bool {
191 let (simulation, time_offset) = self.simulation(time);
192 simulation.is_done(time - time_offset)
193 }
194
195 fn tolerance(&self) -> Tolerance {
196 self.tolerance
197 }
198
199 fn set_tolerance(&mut self, tolerance: Tolerance) {
203 self.tolerance = tolerance;
204 if let Some(friction) = &mut self.friction_simulation {
205 friction.set_tolerance(tolerance);
206 }
207 if let Some(spring) = &mut self.spring_simulation {
208 spring.set_tolerance(tolerance);
209 }
210 }
211}
212
213impl Debug for BouncingScrollSimulation {
214 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
215 write!(
216 f,
217 "BouncingScrollSimulation(leadingExtent: {:?}, trailingExtent: {:?})",
218 self.leading_extent, self.trailing_extent
219 )
220 }
221}
222
223#[derive(Debug)]
254pub struct ClampingScrollSimulation {
255 pub position: f64,
258
259 pub velocity: f64,
262
263 pub friction: f64,
272
273 duration: f64,
275
276 distance: f64,
278
279 pub tolerance: Tolerance,
283}
284
285fn k_deceleration_rate() -> f64 {
287 0.78_f64.ln() / 0.9_f64.ln()
288}
289
290const K_INFLEXION: f64 = 0.35;
292
293const PHYSICAL_COEFF: f64 = 9.80665 * 39.37 * 160.0 * 0.84; impl ClampingScrollSimulation {
301 pub const DEFAULT_FRICTION: f64 = 0.015;
304
305 pub fn new(position: f64, velocity: f64) -> ClampingScrollSimulation {
307 ClampingScrollSimulation::with_options(
308 position,
309 velocity,
310 ClampingScrollSimulation::DEFAULT_FRICTION,
311 Tolerance::DEFAULT_TOLERANCE,
312 )
313 }
314
315 pub fn with_options(
317 position: f64,
318 velocity: f64,
319 friction: f64,
320 tolerance: Tolerance,
321 ) -> ClampingScrollSimulation {
322 let mut simulation = ClampingScrollSimulation {
323 position,
324 velocity,
325 friction,
326 duration: 0.0,
327 distance: 0.0,
328 tolerance,
329 };
330 simulation.duration = simulation.fling_duration();
331 simulation.distance = simulation.fling_distance();
332 simulation
333 }
334
335 fn fling_duration(&self) -> f64 {
337 let reference_velocity = self.friction * PHYSICAL_COEFF / K_INFLEXION;
340
341 let android_duration =
343 (self.velocity.abs() / reference_velocity).powf(1.0 / (k_deceleration_rate() - 1.0));
344
345 k_deceleration_rate() * K_INFLEXION * android_duration
348 }
349
350 fn fling_distance(&self) -> f64 {
353 let distance = self.velocity * self.duration / k_deceleration_rate();
354 if cfg!(debug_assertions) {
355 let reference_velocity = self.friction * PHYSICAL_COEFF / K_INFLEXION;
358 let log_velocity = (self.velocity.abs() / reference_velocity).ln();
359 let distance_again = self.friction
360 * PHYSICAL_COEFF
361 * (log_velocity * k_deceleration_rate() / (k_deceleration_rate() - 1.0)).exp();
362 debug_assert!((distance.abs() - distance_again).abs() < self.tolerance.distance);
363 }
364 distance
365 }
366}
367
368impl Simulation for ClampingScrollSimulation {
369 fn x(&self, time: f64) -> f64 {
370 let t = clamp_double(time / self.duration, 0.0, 1.0);
371 self.position + self.distance * (1.0 - (1.0 - t).powf(k_deceleration_rate()))
372 }
373
374 fn dx(&self, time: f64) -> f64 {
375 let t = clamp_double(time / self.duration, 0.0, 1.0);
376 self.velocity * (1.0 - t).powf(k_deceleration_rate() - 1.0)
377 }
378
379 fn is_done(&self, time: f64) -> bool {
380 time >= self.duration
381 }
382
383 fn tolerance(&self) -> Tolerance {
384 self.tolerance
385 }
386
387 fn set_tolerance(&mut self, tolerance: Tolerance) {
388 self.tolerance = tolerance;
389 }
390}
391
392#[cfg(test)]
393mod tests {
394 use super::*;
395
396 fn default_spring() -> SpringDescription {
397 SpringDescription::with_damping_ratio_value(0.5, 100.0, 1.1)
398 }
399
400 fn run_to_completion(simulation: &dyn Simulation, deadline: f64) -> f64 {
402 let mut time = 0.0;
403 while !simulation.is_done(time) {
404 time += 1.0 / 60.0;
405 assert!(
406 time < deadline,
407 "the simulation must finish before {deadline}s"
408 );
409 }
410 time
411 }
412
413 #[test]
416 fn clamping_scroll_simulation_has_stable_initial_conditions() {
417 for (position, velocity) in [
418 (51.0, 2866.91537),
419 (584.0, 2617.294734),
420 (0.0, 1831.366634),
421 (-156.2, 1541.57665),
422 (5469.0, 182.114534),
423 ] {
424 let simulation = ClampingScrollSimulation::new(position, velocity);
425 assert!((simulation.x(0.0) - position).abs() < 1e-9);
426 assert!((simulation.dx(0.0) - velocity).abs() < 1e-9);
427 }
428 }
429
430 #[test]
433 fn clamping_scroll_simulation_only_decelerates() {
434 let simulation = ClampingScrollSimulation::new(0.0, 8000.0);
435 let mut time = 0.0;
436 let mut velocity = simulation.dx(time);
437 while !simulation.is_done(time) {
438 assert!(time < 3.0);
439 time += 1.0 / 60.0;
440 let next_velocity = simulation.dx(time);
441 assert!(next_velocity <= velocity);
442 velocity = next_velocity;
443 }
444 assert!(velocity.abs() < 1e-9);
445 }
446
447 #[test]
450 fn clamping_scroll_simulation_travels_its_fling_distance_over_its_fling_duration() {
451 let simulation = ClampingScrollSimulation::new(0.0, 8000.0);
452 assert!(!simulation.is_done(2.1183617));
453 assert!(simulation.is_done(2.118361712));
454 assert!((simulation.x(2.2) - 7186.362753988744).abs() < 1e-6);
455 assert_eq!(simulation.dx(2.2), 0.0);
456 }
457
458 #[test]
460 fn bouncing_scroll_simulation_uses_friction_inside_the_extents() {
461 let simulation =
462 BouncingScrollSimulation::new(0.0, 200.0, -1000.0, 1000.0, default_spring());
463 assert_eq!(simulation.x(0.0), 0.0);
464 assert_eq!(simulation.dx(0.0), 200.0);
465 let time = run_to_completion(&simulation, 10.0);
466 let resting = simulation.x(time);
467 assert!(resting > 0.0 && resting < 1000.0, "stopped at {resting}");
468 }
469
470 #[test]
472 fn bouncing_scroll_simulation_springs_back_to_the_leading_extent() {
473 let simulation = BouncingScrollSimulation::new(-100.0, 0.0, 0.0, 500.0, default_spring());
474 assert_eq!(simulation.x(0.0), -100.0);
475 let time = run_to_completion(&simulation, 10.0);
476 assert!(
477 simulation.x(time).abs() < 0.1,
478 "settled at {}",
479 simulation.x(time)
480 );
481 }
482
483 #[test]
490 fn bouncing_scroll_simulation_springs_a_release_moving_back_from_past_the_end_to_the_end() {
491 let simulation =
492 BouncingScrollSimulation::new(377.0, -7276.0, 0.0, 303.0, default_spring());
493 let stopped = run_to_completion(&simulation, 5.0);
494 assert!(
495 (simulation.x(stopped) - 303.0).abs() < 0.01,
496 "{}",
497 simulation.x(stopped)
498 );
499 }
500
501 #[test]
502 fn bouncing_scroll_simulation_hands_a_fast_fling_to_the_trailing_spring() {
503 let simulation = BouncingScrollSimulation::new(0.0, 5000.0, 0.0, 100.0, default_spring());
504 assert!(simulation.x(0.1) > 100.0);
506 let time = run_to_completion(&simulation, 10.0);
507 assert!(
508 (simulation.x(time) - 100.0).abs() < 0.1,
509 "settled at {}",
510 simulation.x(time)
511 );
512 }
513
514 #[test]
516 fn bouncing_scroll_simulation_switches_from_friction_to_the_spring() {
517 let spring = SpringDescription::with_damping_ratio_value(1.0, 50.0, 0.5);
518
519 let mut scroll = BouncingScrollSimulation::new(100.0, 800.0, 0.0, 300.0, spring);
520 scroll.set_tolerance(Tolerance {
521 velocity: 0.5,
522 distance: 0.1,
523 ..Tolerance::DEFAULT_TOLERANCE
524 });
525 assert!(!scroll.is_done(0.0));
526 assert!(!scroll.is_done(0.5)); assert!(scroll.is_done(3.5));
528
529 let mut scroll2 = BouncingScrollSimulation::new(100.0, -800.0, 0.0, 300.0, spring);
530 scroll2.set_tolerance(Tolerance {
531 velocity: 0.5,
532 distance: 0.1,
533 ..Tolerance::DEFAULT_TOLERANCE
534 });
535 assert!(!scroll2.is_done(0.0));
536 assert!(!scroll2.is_done(0.5)); assert!(scroll2.is_done(3.5));
538 }
539
540 #[test]
542 fn bouncing_scroll_simulation_stays_on_friction_with_an_infinite_trailing_extent() {
543 let spring = SpringDescription::with_damping_ratio_value(1.0, 50.0, 0.5);
544 let mut scroll = BouncingScrollSimulation::new(100.0, 400.0, 0.0, f64::INFINITY, spring);
545 scroll.set_tolerance(Tolerance {
546 velocity: 1.0,
547 ..Tolerance::DEFAULT_TOLERANCE
548 });
549
550 assert!(!scroll.is_done(0.0));
551 assert_eq!(scroll.x(0.0), 100.0);
552 assert_eq!(scroll.dx(0.0), 400.0);
553
554 assert!((scroll.x(1.0) - 272.0).abs() < 1.0);
555 assert!((scroll.dx(1.0) - 54.0).abs() < 1.0);
556 assert!((scroll.dx(2.0) - 7.0).abs() < 1.0);
557 assert!(scroll.dx(3.0) < 1.0);
558
559 assert!(scroll.is_done(5.0));
560 assert!((scroll.x(5.0) - 300.0).abs() < 1.0);
561 }
562
563 #[test]
565 fn bouncing_scroll_simulation_overscrolls_then_springs_back() {
566 let spring = SpringDescription::with_damping_ratio_value(1.0, 170.0, 1.1);
567 let mut scroll = BouncingScrollSimulation::new(500.0, -7500.0, 0.0, 1000.0, spring);
568 scroll.set_tolerance(Tolerance {
569 velocity: 45.0,
570 distance: 1.5,
571 ..Tolerance::DEFAULT_TOLERANCE
572 });
573
574 assert!(!scroll.is_done(0.0));
575 assert!((scroll.x(0.0) - 500.0).abs() < 1e-9);
576 assert!((scroll.dx(0.0) + 7500.0).abs() < 1e-9);
577
578 assert!(!scroll.is_done(0.065));
581 assert!((scroll.x(0.065) - 42.0).abs() < 1.0);
582 assert!((scroll.dx(0.065) + 6584.0).abs() < 1.0);
583
584 assert!(!scroll.is_done(0.1));
587 assert!((scroll.x(0.1) + 123.0).abs() < 1.0);
588 assert!((scroll.dx(0.1) + 2613.0).abs() < 1.0);
589
590 assert!(!scroll.is_done(0.5));
592 assert!((scroll.x(0.5) + 15.0).abs() < 1.0);
593 assert!((scroll.dx(0.5) - 124.0).abs() < 1.0);
594
595 assert!(!scroll.is_done(0.0));
597 assert!((scroll.x(0.0) - 500.0).abs() < 1e-9);
598 assert!((scroll.dx(0.0) + 7500.0).abs() < 1e-9);
599
600 assert!(scroll.is_done(2.0));
601 assert_eq!(scroll.x(2.0), 0.0);
602 assert!(scroll.dx(2.0).abs() < 1.0);
603 }
604
605 #[test]
606 fn bouncing_scroll_simulation_to_string_names_its_extents() {
607 let simulation = BouncingScrollSimulation::new(0.0, 0.0, -1.0, 500.0, default_spring());
608 assert_eq!(
609 format!("{simulation:?}"),
610 "BouncingScrollSimulation(leadingExtent: -1.0, trailingExtent: 500.0)"
611 );
612 }
613}