# misc/platformerbody (vanilla-engine 0.4.0)

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

Platformer physics component.
- A port of the original misc/platformerbody.js. Holds the minimal physics (kinematics) that side-scrolling games kept rebuilding,
  like gravity integration, ground contact, and horizontal movement bounds. Moves the node's local position directly. (down is +Y)
- Usage:
  ```text
    let body = node_tree[hero_id].add_component(PlatformerBody::new());
    body.set_ground_y(560.0);
    body.set_horizontal_bounds(Some(40.0), Some(920.0));
    // move: body.set_velocity_x(±speed), jump: body.jump(-720.0)
  ```
- Differences from the original:
  null bounds are None. The landing notification is an FnMut() callback.

## PlatformerBody (struct) (Default, Component)

Platformer physics component.
- Each tick computes the next position with integrate and puts it into the node's local position.

Example: Attach to a node, let it fall, and jump from the ground

```rust
use vanilla::{Engine, EngineConfiguration, NodeTree, PlatformerBody, Vector2};

let engine = Engine::new(EngineConfiguration::new());
let mut node_tree = NodeTree::new();
let root_id = node_tree.get_root_id();
let hero_id = node_tree.create_child(root_id, "hero");
node_tree[hero_id].set_local_position(Vector2::create(100.0, 0.0));
let body = node_tree[hero_id].add_component(PlatformerBody::new());
body.set_ground_y(560.0);
body.set_horizontal_bounds(Some(40.0), Some(920.0));

for _ in 0..60 {
	node_tree.tick(&engine, 1.0 / 60.0);
}
assert_eq!(node_tree[hero_id].get_local_position().y, 560.0);
let body = node_tree[hero_id].get_component_mut::<PlatformerBody>().unwrap();
assert!(body.is_grounded());
body.set_velocity_x(300.0);
assert!(body.jump(-720.0));
```

- `pub fn new() -> PlatformerBody` (constructor, since 0.3.2): Constructor. (gravity 2000 per second, ground 0, no bounds, airborne)
  - returns PlatformerBody: New platformer physics with zero velocity.
- `pub fn integrate(&mut self, current_position: Vector2, time_delta: f64) -> Vector2` (method, since 0.3.2): Integrates one step. (velocity with gravity → position → ground contact → bounds. Returns the next position)
  - Called by tick. Separated out so it can be tested without a node.
  - On touching the ground, sets the vertical velocity to 0, and calls the landing notification at the moment it lands from the air.
  - `current_position` Vector2: Current position. (local coordinates relative to the parent area, down is +Y)
  - `time_delta` f64: Elapsed time. (seconds)
  - returns Vector2: Next position. (y does not go past the ground height, and x is clamped within the left and right bounds)

  Example: Integrate one step without a node

  ```rust
  use vanilla::{PlatformerBody, Vector2};
  
  let mut body = PlatformerBody::new();
  body.set_ground_y(100.0);
  let next_position = body.integrate(Vector2::create(0.0, 0.0), 0.1);
  assert_eq!(next_position, Vector2::create(0.0, 20.0));
  assert_eq!(body.get_velocity().y, 200.0);
  assert!(!body.is_grounded());
  ```

- `pub fn jump(&mut self, jump_speed: f64) -> bool` (method, since 0.3.2): Jumps. (only while grounded, up is negative velocity. true if it jumped)
  - `jump_speed` f64: Value to set as the vertical velocity. (pixels/second, negative to jump up)
  - returns bool: true if it jumped because it was grounded. false if airborne, and the velocity is unchanged.
- `pub fn set_velocity_x(&mut self, velocity_x: f64)` (method, since 0.3.2): Sets the horizontal velocity.
  - `velocity_x` f64: Horizontal velocity. (pixels/second, right is +)
- `pub fn set_velocity_y(&mut self, velocity_y: f64)` (method, since 0.3.2): Sets the vertical velocity.
  - `velocity_y` f64: Vertical velocity. (pixels/second, down is +)
- `pub fn get_velocity(&self) -> Vector2` (method, since 0.3.2): Returns the speed.
  - returns Vector2: Current velocity. (pixels/second)
- `pub fn set_gravity(&mut self, gravity: f64)` (method, since 0.3.2): Sets the gravity. (velocity change per second)
  - `gravity` f64: Gravity. (pixels/second², down is +, default 2000)
- `pub fn get_gravity(&self) -> f64` (method, since 0.3.2): Returns the gravity.
  - returns f64: Current gravity. (pixels/second²)
- `pub fn set_ground_y(&mut self, ground_y: f64)` (method, since 0.3.2): Sets the ground height. (local Y relative to the parent area)
  - `ground_y` f64: Ground height. (local Y, it does not go below this value)
- `pub fn get_ground_y(&self) -> f64` (method, since 0.3.2): Returns the ground height.
  - returns f64: Current ground height. (local Y)
- `pub fn set_horizontal_bounds(&mut self, left_bound: Option<f64>, right_bound: Option<f64>)` (method, since 0.3.2): Sets the horizontal movement bounds. (None leaves that side unbounded)
  - `left_bound` Option<f64>: Left bound. (local X, it does not go below this)
  - `right_bound` Option<f64>: Right bound. (local X, it does not go above this)
- `pub fn is_grounded(&self) -> bool` (method, since 0.3.2): Whether it is grounded.
  - returns bool: true if it was touching the ground in the last integration. Becomes false on jumping.
- `pub fn set_landed_event(&mut self, landed_event: impl FnMut() + 'static)` (method, since 0.3.2): Sets the landing callback. (the moment it touches the ground from the air)
  - `landed_event` impl FnMut() + 'static: Function to call on landing. (replaces the previous notification)

  Example: Count landings

  ```rust
  use std::cell::Cell;
  use std::rc::Rc;
  use vanilla::{PlatformerBody, Vector2};
  
  let landed_count = Rc::new(Cell::new(0));
  let landed_count_for_event = landed_count.clone();
  let mut body = PlatformerBody::new();
  body.set_ground_y(10.0);
  body.set_landed_event(move || {
  	landed_count_for_event.set(landed_count_for_event.get() + 1);
  });
  let mut position = Vector2::zero();
  for _ in 0..30 {
  	position = body.integrate(position, 0.1);
  }
  assert_eq!(landed_count.get(), 1);
  ```

