# misc/shaker (vanilla-engine 0.4.0)

> https://vanillapunch.com/docs/vanilla-engine/0.4.0/api/misc/shaker/

Shake generator. (shared by screen / nodes)
- A port of the original misc/shaker.js. Makes x / y offsets from two sine waves with different phases and lets them die down with exponential decay.
- Add strength with add_shake(strength), and every frame after tick(time_delta), add get_offset() to a camera or node position.
- Differences from the original:
  The constructor's options object ({ decay_rate, frequency }) is the ShakerOptions struct. Omitted values are filled with ..ShakerOptions::default().
  ```text
    let shaker = Shaker::new(ShakerOptions { decay_rate: 10.0, ..ShakerOptions::default() });
  ```

## ShakerOptions (struct) (Clone, Copy, Debug, PartialEq, Default)

Shake settings. (original options = { decay_rate = 9, frequency = 42 })

Example: Change only the decay rate and leave the rest at defaults

```rust
use vanilla::{Shaker, ShakerOptions};

let shaker_options = ShakerOptions {
	decay_rate: 10.0,
	..ShakerOptions::default()
};
assert_eq!(shaker_options.frequency, 42.0);
let shaker = Shaker::new(shaker_options);
assert!(!shaker.is_shaking());
```

- `pub decay_rate: f64` (field, since 0.3.2): Decay rate. (the larger, the faster it dies down)
  - returns f64
- `pub frequency: f64` (field, since 0.3.2): Oscillation frequency. (radians / second)
  - returns f64

## Shaker (struct) (Clone, Debug, Default)

Shake generator.
- Put in strength with add_shake, and after tick every frame add get_offset to a camera or node position.

Example: Shake and update until it dies down

```rust
use vanilla::{Shaker, ShakerOptions, Vector2};

let mut shaker = Shaker::new(ShakerOptions {
	decay_rate: 10.0,
	..ShakerOptions::default()
});
shaker.add_shake(8.0);
shaker.tick(0.1);
let offset = shaker.get_offset();
assert!(offset.x.abs() <= 8.0);
assert!(offset.y.abs() <= 8.0);
while shaker.is_shaking() {
	shaker.tick(0.1);
}
assert_eq!(shaker.get_offset(), Vector2::zero());
```

- `pub fn new(options: ShakerOptions) -> Shaker` (constructor, since 0.3.2): Constructor.
  - `options` ShakerOptions: Decay rate and frequency.
  - returns Shaker: A new shake generator that is not shaking (strength 0).
- `pub fn add_shake(&mut self, strength: f64)` (method, since 0.3.2): Adds a shake. (maximum offset in pixels. If already shaking, the stronger one wins)
  - `strength` f64: Shake strength. (maximum offset, pixels) Ignored if less than the current strength.
- `pub fn tick(&mut self, time_delta: f64)` (method, since 0.3.2): Update. (stops when the strength drops below 0.05)
  - `time_delta` f64: Elapsed time. (seconds, the strength is multiplied by exp(-decay_rate * time_delta))
- `pub fn get_offset(&self) -> Vector2` (method, since 0.3.2): Returns the current offset. (the two axes use a frequency ratio of 1 : 1.37 so the path is not monotonous)
  - returns Vector2: Offset to add. (pixels, -strength ~ strength per axis) (0, 0) if not shaking.
- `pub fn is_shaking(&self) -> bool` (method, since 0.3.2): Whether it is shaking.
  - returns bool: true if the strength is greater than 0.
- `pub fn stop(&mut self)` (method, since 0.3.2): Stops immediately.
- `pub fn set_decay_rate(&mut self, decay_rate: f64)` (method, since 0.3.2): Sets the decay rate. (larger fades faster)
  - `decay_rate` f64: New decay rate. (exponential decay coefficient per second)
- `pub fn set_frequency(&mut self, frequency: f64)` (method, since 0.3.2): Sets the oscillation frequency.
  - `frequency` f64: New oscillation frequency. (radians/second, the y axis is 1.37 times this)
