pub struct FixedClock { /* private fields */ }Expand description
Frame-rate independent simulation with bounded catch-up work.
Supply elapsed seconds from your platform. Non-finite/negative durations are ignored. Over-budget whole steps are discarded; fractional remainder is retained for interpolation.
Implementations§
Source§impl FixedClock
impl FixedClock
Sourcepub fn new(hz: f64, max_steps: u32) -> FixedClock
pub fn new(hz: f64, max_steps: u32) -> FixedClock
Create a clock with the given tick rate and maximum steps per frame.
§Panics
Panics unless hz is finite and in 1..=1000, and max_steps is nonzero.
Use Self::try_new to report invalid user configuration without panicking.
Sourcepub fn try_new(hz: f64, max_steps: u32) -> Result<FixedClock, FixedClockError>
pub fn try_new(hz: f64, max_steps: u32) -> Result<FixedClock, FixedClockError>
Create a clock, returning an actionable error for invalid configuration.
hz must be finite and in 1..=1000; max_steps must be at least one.
The tick rate is checked first if both values are invalid. Valid inputs have
exactly the same timing behavior as Self::new.
use ferravex_core::FixedClock;
let mut clock = FixedClock::try_new(60.0, 8)?;
assert_eq!(clock.advance(1.0 / 30.0).ticks().count(), 2);Sourcepub fn advance(&mut self, frame_seconds: f64) -> FrameStep
pub fn advance(&mut self, frame_seconds: f64) -> FrameStep
Schedule fixed ticks from elapsed platform time, preserving fractional time.
Negative and non-finite durations are ignored. Finite durations are capped
at one hour before accumulation; time above that cap is ignored and is not
included in FrameStep::dropped_seconds. Within the cap, whole ticks beyond
the catch-up budget are discarded and reported in dropped_seconds.
Sourcepub fn advance_batch(&mut self, frame_seconds: f64) -> TickBatch
pub fn advance_batch(&mut self, frame_seconds: f64) -> TickBatch
Schedule the same work as Self::advance, retaining per-update tick IDs.
IDs start at 1, wrap through 0 and restart at 1 after Self::reset. They
count scheduled work, including any batch tail the caller does not execute.
No extra steps or time are consumed by constructing/iterating the batch.
use ferravex_core::{FixedClock, TickContext};
let mut clock = FixedClock::new(64.0, 4);
assert_eq!(clock.advance_batch(0.5 / 64.0).ticks().len(), 0);
let batch = clock.advance_batch(2.0 / 64.0);
let mut position = 0.0;
for TickContext { tick, batch_index, dt } in batch.ticks() {
assert_eq!(tick, u64::from(batch_index) + 1);
position += 2.0 * dt;
}
assert_eq!(position, 4.0 / 64.0);
assert_eq!(batch.frame().alpha, 0.5);
assert_eq!(clock.tick(), 2);Sourcepub fn tick(&self) -> u64
pub fn tick(&self) -> u64
Number of scheduled ticks since construction or reset, wrapping at u64::MAX.
Discarded catch-up ticks are not counted.
The whole batch is counted before execution; use Self::advance_batch
for individual IDs instead of reading this batch-end count inside a loop.
pub fn dt(&self) -> f32
pub fn reset(&mut self)
Trait Implementations§
Source§impl Clone for FixedClock
impl Clone for FixedClock
Source§fn clone(&self) -> FixedClock
fn clone(&self) -> FixedClock
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read more