fix the timer's jumpiness by recalling basic trig. ;)
use abstutil::elapsed_seconds;
+use std::f64::consts::PI;
use std::time::Instant;
pub struct Cycler {
@@ -16,12 +17,10 @@ impl Cycler {
// Returns [0.0, 1.0], going up from 0 to 1 and back down to 0 over the specified period.
pub fn value(&self) -> f64 {
- let result: f64 = (elapsed_seconds(self.start) % self.period_s) / self.period_s;
+ // Ranges from -1 to 1
+ let cosine = (elapsed_seconds(self.start) * (2.0 * PI) / self.period_s).cos();
+ let result = (cosine + 1.0) / 2.0;
assert!(result >= 0.0 && result < 1.0);
- if result >= 0.5 {
- 1.0 - result
- } else {
- result * 2.0
- }
+ result
}
}
