# misc/platformerbody (vanilla.js 0.4.0)

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

Platformer physics component.

## PlatformerBody extends Component

Platformer physics component.
- Holds the minimal physics (kinematics) that side-scrollers kept reinventing, such as gravity integration + ground contact +
  horizontal movement bounds. Moves the node's local position directly.
- Usage:
    const body = node.addComponent(PlatformerBody);
    body.setGroundY(560);
    body.setHorizontalBounds(40, 920);
    // move: body.setVelocityX(±speed), jump: body.jump(-720)

Example: Walk right and jump on press

```js
import { Engine, EngineConfiguration, Scene, WorldNode, Paint, PlatformerBody, Vector2, Pivot } from "./build/vanilla.js";

const playerNode = new WorldNode();
const paint = playerNode.addComponent(Paint);
paint.setColor("#ffcc00");
const platformerBody = playerNode.addComponent(PlatformerBody);
platformerBody.setGroundY(300);
platformerBody.setHorizontalBounds(40, 600);
platformerBody.setVelocityX(120);
platformerBody.setLandedEvent(() => {
	console.log("landed");
});

class MainScene extends Scene {
	initialize(engine) {
		super.initialize(engine);
		playerNode.setPivot(Pivot.bottomCenter);
		playerNode.setLocalPosition(Vector2.create(80, 100));
		playerNode.setContentSize(Vector2.create(32, 48));
		const root = this.getRoot();
		root.addChild(playerNode);
	}

	touchPress(viewInputPosition) {
		super.touchPress(viewInputPosition);
		platformerBody.jump(-720);
	}
}

const engineConfiguration = new EngineConfiguration();
const engine = new Engine(engineConfiguration);
engine.run(new MainScene());
```

- `constructor()` (constructor, since 0.3.0): Constructor.
- `tick(timeDelta)` (method, since 0.3.0): Update. (velocity integration → gravity → ground contact → bounds)
  - `timeDelta` number: Time since the last frame. (Seconds)
- `jump(jumpSpeed)` (method, since 0.3.0): Jumps. (only while grounded, upward is negative velocity)
  - `jumpSpeed` number: Negative is up.
  - returns boolean: Whether the jump succeeded.

  Example: Jump once without the engine and measure the highest point

  ```js
  import { WorldNode, PlatformerBody } from "./build/vanilla.js";
  
  const node = new WorldNode();
  const platformerBody = node.addComponent(PlatformerBody);
  platformerBody.setGroundY(0);
  platformerBody.tick(1 / 60);
  console.log(platformerBody.isGrounded(), platformerBody.jump(-600));
  let highestY = 0;
  for (let frame = 0; frame < 60; ++frame) {
  	platformerBody.tick(1 / 60);
  	const position = node.getLocalPosition();
  	highestY = Math.min(highestY, position.y);
  }
  console.log(highestY.toFixed(1), platformerBody.isGrounded());
  ```

- `setVelocityX(velocityX)` (method, since 0.3.0): Set the horizontal velocity. (pixels / second)
  - `velocityX` number: Horizontal velocity. (pixels / second)
- `setVelocityY(velocityY)` (method, since 0.3.0): Sets the vertical velocity. (pixels / second, down is positive)
  - `velocityY` number: Vertical velocity. (pixels / second, up is negative)
- `getVelocity()` (method, since 0.3.0): Returns the velocity. (pixels / second)
  - returns Vector2: Velocity. (pixels / second, down is positive)
- `setGravity(gravity)` (method, since 0.3.0): Sets the gravity acceleration. (pixels / second², down is positive, default 2000)
  - `gravity` number: Gravity acceleration. (pixels / second², down is positive)
- `getGravity()` (method, since 0.3.0): Returns the gravity acceleration. (pixels / second²)
  - returns number: Gravity acceleration. (pixels / second²)
- `setGroundY(groundY)` (method, since 0.3.0): Sets the ground height. (the node does not go below this Y, default 0)
  - `groundY` number: Ground Y. (local coordinates relative to the parent)
- `getGroundY()` (method, since 0.3.0): Returns the ground height.
  - returns number: Ground Y. (local coordinates relative to the parent)
- `setHorizontalBounds(leftBound, rightBound)` (method, since 0.3.0): Sets the horizontal movement bounds. (null means no limit in that direction)
  - `leftBound` number | null: Left bound X. (no limit if null)
  - `rightBound` number | null: Right bound X. (no limit if null)
- `isGrounded()` (method, since 0.3.0): Whether touching the ground.
  - returns boolean: True if touching the ground.
- `setLandedEvent(landedEvent)` (method, since 0.3.0): Sets the landing callback. (the moment it touches the ground from the air)
  - `landedEvent` Function: Function called the moment it lands from the air. () => void
