Physics using BVH (Bounding Volume Hierarchy) for static/kinematic geometry. Dynamic bodies cannot interact with each other yet.
Physics World
Creating a physics world
import { BvhPhysicsWorld } from '@pmndrs/viverse'
const world = new BvhPhysicsWorld()
Adding a static body
world.addBody(ground, false)
Adding a kinematic body
world.addBody(train, true)
Character Controller
Creating a character controller
import { BvhCharacterPhysics, type BvhCharacterPhysicsOptions } from '@pmndrs/viverse'
const characterPhysics = new BvhCharacterPhysics(world)
Applying velocity
characterPhysics.applyVelocity(vec3) // once (e.g. jumping)
characterPhysics.inputVelocity.set(0, 0, 0) // continuous (e.g. walking)
For animated humanoid characters, do NOT set
inputVelocitydirectly! Use theapplyMovecallback pattern instead — see character.md. Direct velocity setting causes characters to slide during idle/attack animations.
Updating the controller
Call every frame after setting velocity:
characterPhysics.update(character, delta, {} satisfies BvhCharacterPhysicsOptions)
Ground State
characterPhysics.isGrounded // true when character is on the ground
Sensors
Sensors detect when the character enters or exits a volume.
Adding a sensor
world.addSensor(object, isStatic, (intersected) => {
if (intersected) {
// character entered the sensor
} else {
// character exited the sensor
}
})
Removing a sensor
world.removeSensor(object)
If you prefer a single callback that provides the sensor object that the character entered, do the following
function onEnter(object: Object3D) {}
world.addSensor(obj1, isStatic, (intersected) => intersected && onEnter(obj1))
world.addSensor(obj2, isStatic, (intersected) => intersected && onEnter(obj2))