npm package: @perplexdotgg/bounce-character-controller
Description: Reference character controller implementation for the Bounce physics engine
Author: perplex.gg
Latest version: 0.3.0

[![npm version](https://img.shields.io/npm/v/@perplexdotgg/bounce-character-controller.svg)](https://www.npmjs.com/package/@perplexdotgg/bounce-character-controller)
[![repo](https://img.shields.io/badge/codeberg-repo-7d48ff?logo=codeberg&logoColor=ff7d48)](https://codeberg.org/perplexdotgg/bounce-character-controller)
[![license](https://img.shields.io/npm/l/@perplexdotgg/bounce-character-controller.svg?color=7d48ff)](http://codeberg.org/perplexdotgg/bounce-character-controller/src/branch/main/LICENSE)
[![PRs Welcome](https://img.shields.io/badge/PRs-welcome-7d48ff.svg)](https://codeberg.org/perplexdotgg/bounce-character-controller/pulls)

# Bounce Character Controller

This project is a reference Typescript implementation of a character controller created using the [Bounce](https://codeberg.org/perplexdotgg/bounce) physics library.

This implementation is heavily inspired by Jolt's reference character controller implementation, and has the following features:
- collision detection and resolution against all bounce shape and body types (e.g. height map, trimesh, etc)
- character applies forces to dynamic bodies (and vice versa)
- character can go up and down stairs
- character can go up and down slopes while sticking to floor
- character can jump while on the ground (air movement control is optional)

This library provides only the character controller logic and physics integration. Rendering and input handling are outside the scope of this library. You can find an example implementation that uses this library alongside basic keyboard + mouse input handling and rendering using [ThreeJS](https://github.com/mrdoob/three.js/) at https://codeberg.org/perplexdotgg/bounce-character-controller-example/.

Although this is a fairly robust implementation, **this is _not_ intended as a game-ready implementation**, rather it is intended as a starting point or reference for one.

## Installation

For serious use, please fork and modify this library's [source code](https://codeberg.org/perplexdotgg/bounce-character-controller/). This package exists as a library mostly for prototyping convenience.

```bash
npm install @perplexdotgg/bounce-character-controller
```

Assumes you already have `@perplexdotgg/bounce` installed.

## Usage

```js
// initialization code
import { World } from '@perplexdotgg/bounce';
import CharacterController from '@perplexdotgg/bounce-character-controller';

const physicsWorld = new World();

const character = new CharacterController(physicsWorld, {
  inertiaDecayRate: 100,
  gravityScale: 1,
  walkingSpeed: 9,
  jumpingSpeed: 6,
  canControlMovementDuringJump: true,
  shouldCharacterStickToFloor: true,
});

const characterRadius = .4;
const characterHeight = .9;
const characterShape = physicsWorld.createCapsule({ radius: characterRadius, height: characterHeight });

// create the character
const characterBody = physicsWorld.createKinematicBody({
  shape: characterShape,
  position: [0, 10, 0],
  orientation: [0, 0, 0],
  belongsToGroups: NoneFlag,
  collidesWithGroups: NoneFlag,
});

character.setBody(characterBody);


// later, in your update function
// movementX and movementY should be in the range [-1, 1], representing the desired movement direction on the XZ plane
// (movementX, movementY) should already be normalized
character.setTargetMovement(movementX, movementY, isJumping);
character.update(deltaInSeconds);

// after the update(), you would put your code for updating your character mesh to the new position/orientation (i.e. copy from characterBody.position and characterBody.orientation)
characterMesh.position.copy(characterBody.position);
characterMesh.quaternion.copy(characterBody.orientation);
```

### Default option values

```js
{
  walkingSpeed: 30,
  jumpingSpeed: 15,
  gravity: { x: 0, y: -25, z: 0 },
  gravityScale: 1,
  inertiaDecayRate: 10,

  canControlMovementDuringJump: true,
  shouldCharacterStickToFloor: true,
  stickToFloorStepDownSpeed: 30,
  stickToFloorStepDownDistance: 0.5,

  shouldCharacterWalkStairs: false,
  walkStairsStepUpDistance: 0.4,
  walkStairsStepUpAmount: 0.4,
  walkStairsMinStepForward: 0.02,
  walkStairsStepForwardTest: 0.15,
  walkStairsCosAngleForwardContact: Math.cos(degreesToRadians(75)),
}
```

## How to contribute

If you like this project and would like to support our work, please consider contributing code via [pull requests](https://codeberg.org/perplexdotgg/bounce-character-controller/pulls), or donating via [open collective](https://opencollective.com/perplexgg). Contributions are greatly appreciated!