Physics
MavonEngine uses Rapier3D (@dimforge/rapier3d-compat, 0.21) for physics. The engine does not create the physics world for you: you create a RAPIER.World, pass it in, and the engine steps it on every tick and hands it to everything that needs it.
Creating the world
Rapier is a WebAssembly module, so call RAPIER.init() once before you create the world. The multiplayer template does the same on both sides.
Server
Pass the world to your Server. It creates the headless BaseGame with it.
// server/src/index.ts
import RAPIER from '@dimforge/rapier3d-compat'
import logger from './Logger'
import Server from './Server'
RAPIER.init().then(() => {
const physicsWorld = new RAPIER.World({ x: 0, y: -9.83, z: 0 })
// Static ground
const groundBody = physicsWorld.createRigidBody(RAPIER.RigidBodyDesc.fixed())
physicsWorld.createCollider(
RAPIER.ColliderDesc.cuboid(100, 0.5, 100).setTranslation(0, -0.5, 0),
groundBody,
)
const server = new Server(logger, physicsWorld, { /* ServerOptions */ })
server.start()
})
Client
Pass the world as the second argument of Game.
// client/src/main.ts
import RAPIER from '@dimforge/rapier3d-compat'
import Game from '@mavonengine/core/Game'
RAPIER.init().then(() => {
const physicsWorld = new RAPIER.World({ x: 0, y: -9.83, z: 0 })
const game = new Game([/* assets */], physicsWorld)
})
Without a world, physicsWorld and characterController stay undefined and no physics step runs. That is fine for games that don't need physics.
Accessing the physics world
Both sides expose the same two properties on the game instance:
| Property | Type | Description |
|---|---|---|
physicsWorld | RAPIER.World | undefined | The world you passed in |
characterController | KinematicCharacterController | undefined | Created from the world, see Character controller |
From shared code
BaseGame.instance() returns the running game on whichever side the code runs: the server's headless game, or the client's Game (which extends BaseGame). Code that should run on both sides, like the template's Base/ entities, uses it:
import type { Vector3 } from 'three'
import RAPIER from '@dimforge/rapier3d-compat'
import BaseGame from '@mavonengine/core/BaseGame'
import GameObject from '@mavonengine/core/World/GameObject'
export default class Crate extends GameObject {
collider?: RAPIER.Collider
constructor(position: Vector3) {
super(undefined, position)
const physicsWorld = BaseGame.instance().physicsWorld
if (physicsWorld) {
this.collider = physicsWorld.createCollider(
RAPIER.ColliderDesc.cuboid(0.5, 0.5, 0.5).setTranslation(position.x, position.y, position.z),
)
}
}
destroy(): void {
if (this.collider)
BaseGame.instance().physicsWorld?.removeCollider(this.collider, false)
super.destroy()
}
update(_delta: number): void {}
}
Check for physicsWorld rather than assuming it, so the class also works in a game without physics.
On the server
Inside your Server subclass the game is this.game. From anywhere else, use Server.instance().game or BaseGame.instance(); both return the same headless game.
import BaseServer from '@mavonengine/core/Networking/Server/Server'
export default class Server extends BaseServer<Player> {
protected onTick(delta: number): void {
const world = this.game.physicsWorld
// e.g. authoritative ray casts against the server's colliders
}
}
import Server from '@mavonengine/core/Networking/Server/Server'
const physicsWorld = Server.instance().game.physicsWorld
On the client
import Game from '@mavonengine/core/Game'
const game = Game.instance()
game.physicsWorld // RAPIER.World
game.characterController // KinematicCharacterController
game.physicsDebugRender // RapierDebugRenderer, see below
Stepping
You never call physicsWorld.step() yourself. BaseGame.update() sets physicsWorld.timestep to the tick's delta, steps the world, then updates the world's entities and your onUpdate() callbacks. It runs on the fixed tick (tickRate, 30 per second by default) on both sides.
On the client the physics tick is separate from rendering: capping the frame rate with game.renderer.maxFps doesn't slow physics down.
Character controller
When a world is passed in, BaseGame creates a KinematicCharacterController with:
- an offset of
0.01 - impulses applied to dynamic bodies it pushes
- snap to ground up to
30 - a max slope climb angle of 45°
- automatic sliding on slopes over 30°
Change it after the game is created, e.g. game.characterController?.setMaxSlopeClimbAngle(60 * Math.PI / 180).
The template's shared Player moves a kinematic body with it on every update:
const cc = BaseGame.instance().characterController
if (cc) {
cc.computeColliderMovement(this.characterCollider, moveIntent)
const move = cc.computedMovement()
const pos = this.rigidBody.translation()
this.rigidBody.setNextKinematicTranslation({
x: pos.x + move.x,
y: pos.y + move.y,
z: pos.z + move.z,
})
}
Because the same class runs on both sides, the server moves the authoritative player and the client predicts its own player with identical physics. See Networking for how the server's state then corrects the client.
Server and client worlds
The server and each client have their own physics world; they are not synced. Colliders exist on a side when that side's code creates them:
- Shared classes (the template's
server/src/Base/, imported by the client as@template/server/Base/...) create the same colliders on both sides, e.g. the player's capsule and the trees. - Scene objects are built per side. Give a scene object a
serverfactory to build its colliders on the server, and aclientfactory for the client. See Scenes. - The server stays authoritative: only what the server simulates is sent to clients in the state sync.
Remove what you create in destroy(). Removing a rigid body with removeRigidBody() also removes the colliders attached to it.
Debugging
Open the game with #debug (e.g. http://localhost:8050/#debug) to draw the client's physics world as wireframe lines. The Physics folder of the debug panel shows the number of bodies, colliders and active (awake) bodies.
game.physicsDebugRender?.toggleVisible(false) // hide the collider overlay
On the server, the dashboard and the /api/game/metrics endpoint report the server world's bodies and colliders.