# misc/steering2d (vanilla-engine 0.4.0)

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

2D steering utility.
- A port of the original misc/steering2d.js. Gathers frequently rebuilt steering calculations, like combat AI movement that "approaches when far, and keeps distance while circling left and right when within range",
  into pure functions. The return value is the desired velocity vector.
- Differences from the original:
  The strafe_rate default (1.7) of orbit_at_range is passed as the DEFAULT_STRAFE_RATE constant because Rust has no default arguments.

## Steering2D (struct)

2D steering utility. (a set of static functions)
- Stateless. Each frame, take the desired velocity, multiply by time_delta, and add it to the position.

Example: One step toward the target

```rust
use vanilla::{Steering2D, Vector2};

let mut position = Vector2::create(0.0, 0.0);
let target_position = Vector2::create(30.0, 40.0);
let velocity = Steering2D::seek(position, target_position, 10.0);
assert!((velocity.x - 6.0).abs() < 0.0001);
assert!((velocity.y - 8.0).abs() < 0.0001);
let time_delta = 0.5;
position = position.add(velocity.multiply_scalar(time_delta));
assert!((position.x - 3.0).abs() < 0.0001);
```

- `pub const DEFAULT_STRAFE_RATE: f64 = 1.7` (static, since 0.3.2): Default angular speed of the strafing wave of orbit_at_range. (original strafeRate = 1.7)
  - returns f64
- `pub fn seek(position: Vector2, target_position: Vector2, speed: f64) -> Vector2` (static, since 0.3.2): Velocity approaching the target. (0 if at nearly the same spot)
  - `position` Vector2: Current position.
  - `target_position` Vector2: Target position.
  - `speed` f64: Speed. (length of the returned vector, distance per second)
  - returns Vector2: A velocity vector of length speed toward the target. (0, 0) if the distance between the two positions is less than 0.000001.
- `pub fn orbit_at_range(position: Vector2, target_position: Vector2, preferred_range: f64, speed: f64, elapsed_seconds: f64, strafe_rate: f64) -> Vector2` (static, since 0.3.2): Velocity that circles while keeping a range.
  - Mixes in an approach component when farther than the range and a retreat component when closer, and adds sine-wave left and right strafing along the tangent to make the movement feel alive.
  - elapsed_seconds is the accumulated time used for the strafe phase.
  - `position` Vector2: Current position.
  - `target_position` Vector2: Target position.
  - `preferred_range` f64: Distance to keep. (the distance error is divided by this value and clamped to -1 ~ 1)
  - `speed` f64: Speed. (length of the returned vector)
  - `elapsed_seconds` f64: Accumulated time. (seconds, phase of the strafe waveform sin(elapsed_seconds * strafe_rate))
  - `strafe_rate` f64: Strafe waveform angular speed. (radians/second, usually DEFAULT_STRAFE_RATE)
  - returns Vector2: A velocity vector of length speed. (speed, 0) if the two positions are nearly the same, (0, 0) if at range with a strafe component of 0.

  Example: Approach when far, back off when close

  ```rust
  use vanilla::{Steering2D, Vector2};
  
  let target_position = Vector2::create(100.0, 0.0);
  let far_velocity = Steering2D::orbit_at_range(Vector2::create(-200.0, 0.0), target_position, 100.0, 5.0, 0.0, Steering2D::DEFAULT_STRAFE_RATE);
  assert!((far_velocity.x - 5.0).abs() < 0.0001);
  let near_velocity = Steering2D::orbit_at_range(Vector2::create(50.0, 0.0), target_position, 100.0, 5.0, 0.0, Steering2D::DEFAULT_STRAFE_RATE);
  assert!((near_velocity.x + 5.0).abs() < 0.0001);
  ```

