Skip to main content

Shake

Struct Shake 

Source
pub struct Shake { /* private fields */ }
Expand description

Trauma-driven camera shake with smooth, seeded, deterministic noise.

Add trauma (0..=1) on impacts. Shake strength is trauma squared, so small hits stay subtle, and trauma decays linearly with explicitly supplied time. Apply the shake to a copy of the camera when drawing; the game keeps its unshaken camera. With zero trauma the camera is returned unchanged.

use rayengine_core::{camera::Camera2D, tween::{Shake, ShakeConfig}};
use std::time::Duration;
let mut shake = Shake::new(ShakeConfig::default()).unwrap();
let camera = Camera2D::default();
shake.add_trauma(0.6);
shake.advance(Duration::from_millis(16));
let shaken = shake.apply_2d(camera);
assert_ne!(shaken.target, camera.target);
shake.advance(Duration::from_secs(1)); // fully decayed
assert_eq!(shake.apply_2d(camera).target, camera.target);

Implementations§

Source§

impl Shake

Source

pub fn new(config: ShakeConfig) -> Result<Self, ShakeError>

Validates finite, nonnegative limits, decay and a positive frequency.

Source

pub fn config(&self) -> &ShakeConfig

Current limits.

Source

pub fn set_config(&mut self, config: ShakeConfig) -> Result<(), ShakeError>

Replaces limits, keeping trauma and noise position.

Source

pub fn add_trauma(&mut self, amount: f32)

Adds trauma, saturating at one. Nonfinite amounts are ignored; negative amounts reduce trauma.

Source

pub fn set_trauma(&mut self, trauma: f32)

Sets trauma, clamped to 0..=1. Nonfinite values are ignored.

Source

pub fn trauma(&self) -> f32

Current trauma in 0..=1.

Source

pub fn intensity(&self) -> f32

Shake strength, trauma².

Source

pub fn advance(&mut self, dt: Duration)

Decays trauma and moves along the noise by explicitly supplied time.

Source

pub fn sample(&self) -> ShakeSample

Current displacement; exactly zero without trauma.

Source

pub fn apply_2d(&self, camera: Camera2D) -> Camera2D

Shaken copy of a 2D camera. The offset follows the camera’s screen axes.

Source

pub fn apply_3d(&self, camera: Camera3D) -> Camera3D

Shaken copy of a 3D camera: the eye and target move together along the view’s right/up axes and up rolls around the view direction. A degenerate camera (eye at target, or up parallel to the view) is returned unchanged.

Trait Implementations§

Source§

impl Clone for Shake

Source§

fn clone(&self) -> Self

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

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

Performs copy-assignment from source. Read more
Source§

impl Debug for Shake

Source§

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

Formats the value using the given formatter. Read more

Auto Trait Implementations§

§

impl Freeze for Shake

§

impl RefUnwindSafe for Shake

§

impl Send for Shake

§

impl Sync for Shake

§

impl Unpin for Shake

§

impl UnsafeUnpin for Shake

§

impl UnwindSafe for Shake

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> Component for T
where T: Send + Sync + 'static,

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 = !

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

fn try_from(value: U) -> Result<T, !>

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.