pub struct CameraShake { /* private fields */ }Expand description
Allocation-free seeded shake, driven only by explicitly supplied delta time.
Identical seeds, accepted non-silent impulses and delta-time sequences yield identical samples on the same target. Use fixed deltas for replay; different floating-point partitions of elapsed time can differ by rounding. Sampling does not advance time. Shake affects presentation, never simulation timing.
At most Self::CAPACITY impulses are summed in slot order. Each impulse’s
translation is limited to 10 per axis and rotation to 0.5 radians per axis,
bounding the aggregate by 80 and 4 respectively before caller-side scaling.
Implementations§
Source§impl CameraShake
impl CameraShake
Sourcepub const CAPACITY: usize = 8usize
pub const CAPACITY: usize = 8usize
Maximum simultaneous non-silent impulses. Pushing beyond this fails
with CameraError::ShakeCapacity without replacing an existing layer.
pub const fn new(seed: u64) -> CameraShake
pub fn active(&self) -> usize
Sourcepub fn clear(&mut self)
pub fn clear(&mut self)
Cancel all active impulses, preserving the seed and next noise sequence.
Use Self::reset to reproduce the original sequence on a restart.
Sourcepub fn reset(&mut self)
pub fn reset(&mut self)
Cancel every impulse and rewind the noise sequence, retaining the seed.
The result equals a fresh Self::new with the original seed.
use ferravex_math::Vec3;
use ferravex_play::camera::{CameraShake, ShakeImpulse};
let mut shake = CameraShake::new(42);
let impulse = ShakeImpulse {
translation: Vec3::X, rotation: Vec3::ZERO, duration: 1., frequency: 20.,
};
shake.push(impulse)?;
let first = shake.advance(0.125)?;
shake.reset();
shake.push(impulse)?;
assert_eq!(shake.advance(0.125)?, first);Sourcepub fn envelopes(&self) -> impl Iterator<Item = ShakeEnvelope>
pub fn envelopes(&self) -> impl Iterator<Item = ShakeEnvelope>
Inspect active impulses without advancing time or allocating.
Iteration follows storage slot order, not insertion order: expired or
cancelled slots are reused. Snapshots are not stable impulse handles.
See ShakeEnvelope for an inspection example.
Sourcepub fn cancel_where(
&mut self,
predicate: impl FnMut(ShakeEnvelope) -> bool,
) -> usize
pub fn cancel_where( &mut self, predicate: impl FnMut(ShakeEnvelope) -> bool, ) -> usize
Cancel matching active impulses, returning the number removed.
The predicate runs once per active impulse in slot order. Remaining
impulses keep their elapsed times and seeds; cancellation does not rewind
the noise sequence. Use Self::clear to cancel everything.
use ferravex_math::Vec3;
use ferravex_play::camera::{CameraShake, ShakeImpulse};
let mut shake = CameraShake::new(42);
shake.push(ShakeImpulse {
translation: Vec3::X, rotation: Vec3::ZERO, duration: 1., frequency: 20.,
})?;
assert_eq!(shake.cancel_where(|state| state.impulse.rotation == Vec3::ZERO), 1);
assert_eq!(shake.active(), 0);Sourcepub fn push(&mut self, impulse: ShakeImpulse) -> Result<(), CameraError>
pub fn push(&mut self, impulse: ShakeImpulse) -> Result<(), CameraError>
Add an impulse with a fresh noise seed.
Invalid inputs and capacity failures leave all state unchanged. A valid impulse with zero translation and rotation succeeds without using a slot or advancing the seed sequence, even when all slots are occupied.
Sourcepub fn advance(&mut self, dt: f32) -> Result<ShakeSample, CameraError>
pub fn advance(&mut self, dt: f32) -> Result<ShakeSample, CameraError>
Advance by a finite delta in 0..=0.25 seconds, then sample active motion.
Invalid deltas leave all state unchanged. Zero delta only samples; an
impulse expires at its duration and contributes exactly zero thereafter.
Sourcepub fn sample(&self) -> ShakeSample
pub fn sample(&self) -> ShakeSample
Sample the current aggregate without advancing time or changing state. An empty shake yields exactly zero translation and rotation.
use ferravex_math::Vec3;
use ferravex_play::camera::{CameraShake, ShakeImpulse, ShakeSample};
let mut shake = CameraShake::new(42);
shake.push(ShakeImpulse {
translation: Vec3::X, rotation: Vec3::ZERO, duration: 1., frequency: 20.,
})?;
let current = shake.sample();
assert_eq!(shake.advance(0.)?, current);
shake.clear();
assert_eq!(shake.sample(), ShakeSample::default());
assert_eq!(CameraShake::CAPACITY, 8);Trait Implementations§
Source§impl Clone for CameraShake
impl Clone for CameraShake
Source§fn clone(&self) -> CameraShake
fn clone(&self) -> CameraShake
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read more