Module interpolation
Expand description
Two-sample presentation history with explicit holds and discontinuities. Two retained simulation values and caller-selected presentation interpolation.
Commit once after each actual simulation update, including every update in
a catch-up frame. A zero-tick frame only samples. Call InterpolationPair::snap
for an accepted teleport, respawn or restart, replacing both endpoints and
advancing a local discontinuity generation. No clock, input, tick scheduling,
replay format or network history is owned here; longer histories still belong
to crate::snapshots and crate::replay::ghost.
A presentation hold retains the last sampled alpha and the existing endpoints, so an inactive session’s zero alpha cannot rewind the picture. Explicit resume, a committed update or a snap releases the hold. Thus a local presentation hold cannot prevent authoritative simulation updates. To freeze an independent image while a remote match keeps updating, retain that image separately in the host.
§Local session: zero ticks, catch-up, pause, resume and teleport
This runnable recipe injects elapsed time into the existing session. The host still owns input consumption, update success and lifecycle transitions.
use ferravex_engine::{
interpolation::{InterpolationError, InterpolationPair},
math::Vec2,
session::GameSession,
};
let mut session = GameSession::new(64.0, 4, true);
let mut position = Vec2::ZERO;
let mut history = InterpolationPair::new(position);
let blend = |a: &Vec2, b: &Vec2, t| a.lerp(*b, t);
let frame = session.advance(0.5 / 64.0);
assert_eq!(frame.steps, 0); // Do not commit or consume gameplay edges here.
assert_eq!(history.sample_with(frame.alpha, blend)?, Vec2::ZERO);
let frame = session.advance(2.0 / 64.0);
for _dt in frame.ticks() {
if !session.is_playing() { break; } // A prior update could finish the game.
position.x += 1.0; // Execute rules and consume tick input here.
history.commit(position); // Only the accepted, actually executed update.
}
assert_eq!(*history.previous(), Vec2::new(1.0, 0.0));
assert_eq!(*history.current(), Vec2::new(2.0, 0.0));
let visible = history.sample_with(frame.alpha, blend)?;
assert_eq!(visible, Vec2::new(1.5, 0.0));
session.set_paused(true);
history.hold(); // Capture the last successful sample's fraction (0.5).
let inactive = session.advance(30.0);
assert_eq!((inactive.steps, inactive.alpha), (0, 0.0));
assert_eq!(history.sample_with(inactive.alpha, blend)?, visible);
session.set_paused(false);
history.resume();
let resumed = session.advance(0.0); // Retained session fraction, no new tick.
assert_eq!(resumed.steps, 0);
assert_eq!(history.sample_with(resumed.alpha, blend)?, visible);
position = Vec2::new(100.0, 20.0); // The simulation accepted a teleport.
assert_eq!(history.snap(position), 1);
assert_eq!(history.sample_with(0.0, blend)?, position); // No blend across it.
session.restart();
position = Vec2::ZERO; // Reset the simulation as well as presentation.
assert_eq!(history.snap(position), 2);
assert_eq!(history.sample_with(0.75, blend)?, position);§A composed Vec3 / Quat pose
Use an existing pose type or a tuple; no second transform type is required. Position uses linear interpolation and rotation uses the existing shortest-arc quaternion interpolation. Value validity and interpolation policy belong to the caller, including normalizing/validating rotations before committing them.
use ferravex_engine::{interpolation::InterpolationPair, math::{Quat, Vec3}};
use std::f32::consts::FRAC_PI_2;
let mut pose = InterpolationPair::new((Vec3::ZERO, Quat::IDENTITY));
let turn = Quat::from_rotation_y(FRAC_PI_2);
pose.commit((Vec3::new(8.0, 4.0, 2.0), -turn)); // q and -q are equivalent.
let (position, rotation) = pose.sample_with(0.5, |a, b, t| {
(a.0.lerp(b.0, t), a.1.slerp(b.1, t))
})?;
assert_eq!(position, Vec3::new(4.0, 2.0, 1.0));
let expected = Quat::from_rotation_y(FRAC_PI_2 * 0.5);
assert!(rotation.try_angle_between(expected).unwrap() < 1e-6);Structs§
- Interpolation
Pair - Exactly two values, a presentation fraction, a hold flag and a local generation.
Enums§
- Interpolation
Error - Rejected presentation input; endpoints, alpha, hold and generation are unchanged.