Struct CameraShake

Source
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

Source

pub const CAPACITY: usize = 8usize

Maximum simultaneous non-silent impulses. Pushing beyond this fails with CameraError::ShakeCapacity without replacing an existing layer.

Source

pub const fn new(seed: u64) -> CameraShake

Source

pub fn active(&self) -> usize

Source

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.

Source

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);
Source

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.

Source

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);
Source

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.

Source

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.

Source

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

Source§

fn clone(&self) -> CameraShake

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for CameraShake

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), Error>

Formats the value using the given formatter. Read more
Source§

impl PartialEq for CameraShake

Source§

fn eq(&self, other: &CameraShake) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl Copy for CameraShake

Source§

impl StructuralPartialEq for CameraShake

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.