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
impl Shake
Sourcepub fn new(config: ShakeConfig) -> Result<Self, ShakeError>
pub fn new(config: ShakeConfig) -> Result<Self, ShakeError>
Validates finite, nonnegative limits, decay and a positive frequency.
Sourcepub fn config(&self) -> &ShakeConfig
pub fn config(&self) -> &ShakeConfig
Current limits.
Sourcepub fn set_config(&mut self, config: ShakeConfig) -> Result<(), ShakeError>
pub fn set_config(&mut self, config: ShakeConfig) -> Result<(), ShakeError>
Replaces limits, keeping trauma and noise position.
Sourcepub fn add_trauma(&mut self, amount: f32)
pub fn add_trauma(&mut self, amount: f32)
Adds trauma, saturating at one. Nonfinite amounts are ignored; negative amounts reduce trauma.
Sourcepub fn set_trauma(&mut self, trauma: f32)
pub fn set_trauma(&mut self, trauma: f32)
Sets trauma, clamped to 0..=1. Nonfinite values are ignored.
Sourcepub fn advance(&mut self, dt: Duration)
pub fn advance(&mut self, dt: Duration)
Decays trauma and moves along the noise by explicitly supplied time.
Sourcepub fn sample(&self) -> ShakeSample
pub fn sample(&self) -> ShakeSample
Current displacement; exactly zero without trauma.