use crate::geometry::{Point, Points, point};
#[derive(Debug, Default, Clone, Copy, PartialEq)]
struct Offset {
x: f32,
y: f32,
}
impl Offset {
fn lerp(self, to: Self, k: f32) -> Self {
Self {
x: self.x + (to.x - self.x) * k,
y: self.y + (to.y - self.y) * k,
}
}
}
impl std::ops::Add for Offset {
type Output = Self;
fn add(self, o: Self) -> Self {
Self {
x: self.x + o.x,
y: self.y + o.y,
}
}
}
impl std::ops::Sub for Offset {
type Output = Self;
fn sub(self, o: Self) -> Self {
Self {
x: self.x - o.x,
y: self.y - o.y,
}
}
}
impl From<Point<Points>> for Offset {
fn from(p: Point<Points>) -> Self {
Self { x: p.x.0, y: p.y.0 }
}
}
impl From<Offset> for Point<Points> {
fn from(o: Offset) -> Self {
point(Points(o.x), Points(o.y))
}
}
const FRAME: f64 = 1000.0 / 60.0;
const RAMP_START: f64 = 120.0;
const RAMP_END: f64 = 480.0;
const MIN_FRAMES: f64 = 6.0;
const MAX_FRAMES: f64 = 12.0;
const X1: f64 = 0.42;
const X2: f64 = 0.58;
const Y2: f64 = 1.0;
const EPSILON: f64 = 0.01;
#[derive(Debug, Default, Clone, Copy, PartialEq)]
pub struct Glide {
initial: Offset,
start: f64,
retarget: f64,
total: f64,
y1: f64,
}
fn major(d: Offset) -> f64 {
if d.x.abs() > d.y.abs() {
d.x as f64
} else {
d.y as f64
}
}
fn segment(delta: Offset, late: f64) -> f64 {
let slope = (MIN_FRAMES - MAX_FRAMES) / (RAMP_END - RAMP_START);
let offset = MAX_FRAMES - RAMP_START * slope;
let frames = (offset + major(delta).abs() * slope).clamp(MIN_FRAMES, MAX_FRAMES);
(frames * FRAME - late).max(0.0)
}
impl Glide {
pub fn new(initial: Point<Points>, target: Point<Points>, now: f64) -> Self {
let (initial, target) = (Offset::from(initial), Offset::from(target));
Self {
initial,
start: now - FRAME,
retarget: 0.0,
total: segment(target - initial, 0.0),
y1: 0.0,
}
}
pub fn end(&self) -> f64 {
self.start + self.total
}
pub fn shifted(self, d: Point<Points>) -> Self {
Self {
initial: self.initial + d.into(),
..self
}
}
fn curve(&self) -> Bezier {
Bezier::new(X1, self.y1, X2, Y2)
}
pub fn value(&self, target: Point<Points>, now: f64) -> Point<Points> {
self.at(target.into(), now).into()
}
fn at(&self, target: Offset, now: f64) -> Offset {
let duration = self.total - self.retarget;
let t = now - self.start - self.retarget;
if duration <= 0.0 || t >= duration {
return target;
}
if t <= 0.0 {
return self.initial;
}
let p = self.curve().solve(t / duration);
self.initial.lerp(target, p as f32)
}
fn velocity(&self, target: Offset, t: f64) -> f64 {
let duration = self.total - self.retarget;
if duration <= 0.0 {
return 0.0;
}
let slope = self.curve().velocity((t - self.retarget) / duration);
slope * (major(target - self.initial) / (duration / 1000.0))
}
pub fn retargeted(self, old: Point<Points>, new: Point<Points>, now: f64) -> Self {
self.moved_on(old.into(), new.into(), now)
}
fn moved_on(mut self, old: Offset, new: Offset, now: f64) -> Self {
let mut t = now - self.start;
let late = (self.retarget - t).max(0.0);
t = t.max(self.retarget);
if major(old - new).abs() < EPSILON {
return self;
}
let here = self.at(old, self.start + t);
let delta = new - here;
if major(delta).abs() < EPSILON {
self.initial = here;
self.retarget = t;
self.total = t;
return self;
}
let old_duration = self.total - self.retarget;
if old_duration <= 0.0 {
self.total = segment(delta, late);
return self;
}
let velocity = self.velocity(old, t);
let bound = if velocity.abs() < EPSILON {
f64::INFINITY
} else {
let b = major(delta) / velocity * 2.5 * 1000.0;
if b < 0.0 { f64::INFINITY } else { b }
};
let duration = segment(delta, late).min(bound);
if duration / 1000.0 < EPSILON {
self.total = t;
return self;
}
let slope = (velocity * (duration / 1000.0) / major(delta)).clamp(-1000.0, 1000.0);
self.y1 = X1 * slope;
self.initial = here;
self.retarget = t;
self.total = t + duration;
self
}
}
struct Bezier {
ax: f64,
bx: f64,
cx: f64,
ay: f64,
by: f64,
cy: f64,
}
impl Bezier {
fn new(x1: f64, y1: f64, x2: f64, y2: f64) -> Self {
let cx = 3.0 * x1;
let bx = 3.0 * (x2 - x1) - cx;
let cy = 3.0 * y1;
let by = 3.0 * (y2 - y1) - cy;
Self {
ax: 1.0 - cx - bx,
bx,
cx,
ay: 1.0 - cy - by,
by,
cy,
}
}
fn x(&self, t: f64) -> f64 {
((self.ax * t + self.bx) * t + self.cx) * t
}
fn y(&self, t: f64) -> f64 {
((self.ay * t + self.by) * t + self.cy) * t
}
fn dx(&self, t: f64) -> f64 {
(3.0 * self.ax * t + 2.0 * self.bx) * t + self.cx
}
fn dy(&self, t: f64) -> f64 {
(3.0 * self.ay * t + 2.0 * self.by) * t + self.cy
}
fn t_for(&self, x: f64) -> f64 {
let mut t = x;
for _ in 0..8 {
let err = self.x(t) - x;
if err.abs() < 1e-7 {
return t;
}
let d = self.dx(t);
if d.abs() < 1e-6 {
break;
}
t -= err / d;
}
let (mut lo, mut hi) = (0.0, 1.0);
t = x;
while lo < hi {
let v = self.x(t);
if (v - x).abs() < 1e-7 {
return t;
}
if x > v {
lo = t
} else {
hi = t
}
t = (hi - lo) / 2.0 + lo;
if hi - lo < 1e-9 {
break;
}
}
t
}
fn solve(&self, x: f64) -> f64 {
self.y(self.t_for(x.clamp(0.0, 1.0)))
}
fn velocity(&self, x: f64) -> f64 {
let t = self.t_for(x.clamp(0.0, 1.0));
let dx = self.dx(t);
if dx.abs() < 1e-9 {
0.0
} else {
self.dy(t) / dx
}
}
}
#[cfg(test)]
mod tests {
use super::*;
fn y(v: f32) -> Offset {
Offset { x: 0.0, y: v }
}
#[test]
fn durations_follow_the_step() {
let d = |v: f32| segment(y(v), 0.0);
assert!((d(40.0) - 200.0).abs() < 0.01);
assert!((d(300.0) - 150.0).abs() < 0.01);
assert!((d(1000.0) - 100.0).abs() < 0.01);
}
#[test]
fn eases_in_and_out_and_arrives() {
let to = y(100.0);
let g = Glide::new(Offset::default().into(), to.into(), 0.0);
assert_eq!(g.at(to, -FRAME), Offset::default());
let mid = g.at(to, g.start + g.total / 2.0).y;
assert!((mid - 50.0).abs() < 0.5);
assert_eq!(g.at(to, g.end()), to);
}
#[test]
fn a_step_mid_glide_keeps_its_speed() {
let (a, b) = (y(100.0), y(200.0));
let g = Glide::new(Offset::default().into(), a.into(), 0.0);
let now = g.start + g.total / 2.0;
let before = g.velocity(a, now - g.start);
let h = g.moved_on(a, b, now);
assert!((h.at(b, now).y - g.at(a, now).y).abs() < 0.01);
let after = h.velocity(b, now - h.start);
assert!((after - before).abs() / before < 0.02, "{before} {after}");
assert_eq!(h.at(b, h.end()), b);
}
}