Skip to content

PhysicsWorld

Defined in: physics/src/PhysicsWorld.ts:92

Central Rapier2D wrapper. All public API values are in pixels. Pixel-to-meter conversion is handled internally.

new PhysicsWorld(config?): PhysicsWorld

Defined in: physics/src/PhysicsWorld.ts:126

PhysicsConfig

PhysicsWorld

readonly _colliderComponents: Map<number, ColliderComponent>

Defined in: physics/src/PhysicsWorld.ts:102

Internal

Map from collider handle to ColliderComponent.


readonly _jointsByBody: Map<number, Set<JointRecord>>

Defined in: physics/src/PhysicsWorld.ts:104

Internal

Joint records indexed by each attached body handle.


readonly bodyMap: Map<number, Entity>

Defined in: physics/src/PhysicsWorld.ts:97

Map from Rapier body handle to Entity.


readonly colliderMap: Map<number, Entity>

Defined in: physics/src/PhysicsWorld.ts:99

Map from Rapier collider handle to Entity.


readonly pixelsPerMeter: number

Defined in: physics/src/PhysicsWorld.ts:94

Pixels per meter conversion factor.

get elapsed(): number

Defined in: physics/src/PhysicsWorld.ts:151

Total simulated time in seconds — the sum of every step’s dt. Tracks the simulation, not the wall clock, so it respects pause and timeScale.

number

_detachJointsForBody(handle): void

Defined in: physics/src/PhysicsWorld.ts:1168

Internal

Detach every joint touching this body. Called when the body is disabled: a dormant entity may be reused as something else, and a joint is cross-life state the same way momentum and landed contacts are — the next life must not wake up tethered to the old partner. Unlike removal, disabling leaves the body in Rapier, so the joints are freed here.

number

void


_forgetColliderContacts(handle): void

Defined in: physics/src/PhysicsWorld.ts:751

Internal

Drop any landed-rider state naming this collider. Rapier reuses collider handles, so a handle leaving the simulation (removed or disabled) must not stay remembered as “landed” on a one-way platform — the next collider with the same handle would inherit it.

number

void


_removeJoint(record): void

Defined in: physics/src/PhysicsWorld.ts:1153

Internal

Remove a live joint and unlink its record.

JointRecord

void


_setContactFilterEnabled(handle, enabled): void

Defined in: physics/src/PhysicsWorld.ts:245

Internal

Toggle contact filtering for a live collider — flips Rapier’s hook flag and the set that decides whether step passes hooks at all.

number

boolean

void


addJoint(bodyA, bodyB, config): JointHandle

Defined in: physics/src/PhysicsWorld.ts:506

Connect two live rigid bodies with a spring or rope joint. All lengths and anchors are in pixels.

RigidBodyComponent

RigidBodyComponent

JointConfig

JointHandle


castShape(shape, origin, direction, maxDistance, options?): RaycastHit | null

Defined in: physics/src/PhysicsWorld.ts:879

Sweep shape from origin along direction and return the first thing it would hit, or null if nothing is struck within maxDistance. All values in pixels.

This is the swept counterpart to queryShape, which only reports what a shape overlaps where it already stands. Use it to test a move before committing to it: carrying a rider on a moving platform, spotting a closing platform before it traps the player, or checking clearance for a fast fall.

distance is how far the shape travelled, point the world contact point, and normal the surface normal on the entity that was hit. A shape already overlapping something at origin reports that hit at distance: 0. The direction is normalized internally, so any non-zero vector works; a zero-length direction throws. excludeEntity skips every collider of that entity — pass the mover when the sweep starts inside its own collider.

ColliderShape

Vec2Like

Vec2Like

number

Entity

number

number

RaycastHit | null


createBody(entity, config): number

Defined in: physics/src/PhysicsWorld.ts:464

Create a rigid body and register it. Returns the Rapier handle.

Entity

RigidBodyConfig

number


createCollider(entity, bodyHandle, config, component): number

Defined in: physics/src/PhysicsWorld.ts:555

Create a collider attached to a body. Returns the Rapier collider handle.

Entity

number

ColliderConfig

ColliderComponent

number


destroy(): void

Defined in: physics/src/PhysicsWorld.ts:1097

Destroy the physics world and free resources.

void


getBody(handle): RigidBody | undefined

Defined in: physics/src/PhysicsWorld.ts:791

Get a Rapier rigid body by handle.

number

RigidBody | undefined


getCollider(handle): Collider | undefined

Defined in: physics/src/PhysicsWorld.ts:800

Get a Rapier collider by handle.

number

Collider | undefined


processCollisionEvents(): void

Defined in: physics/src/PhysicsWorld.ts:272

Drain collision events and dispatch them to their ColliderComponents.

Draining and dispatching are separate passes. A handler can retire an entity, and a pooled one goes straight back out of its pool as something else, so both sides of every pair are captured with the life they were queued for before any handler runs. Each side is re-checked immediately before its own callback: the first handler can retire the second side’s receiver. A pair naming a life that has ended, or a collider that was removed mid-drain, is dropped rather than delivered against the changed state.

void


queryOverlapping(colliderHandle): Entity[]

Defined in: physics/src/PhysicsWorld.ts:994

Return all entities whose colliders currently overlap the given collider.

number

Entity[]


queryRadius(center, radius, options?): Entity[]

Defined in: physics/src/PhysicsWorld.ts:945

Return all entities with a collider overlapping the circle around center (pixels). Sugar over queryShape with a circle.

Vec2Like

number

Entity

number

Entity[]


queryShape(shape, position, options?): Entity[]

Defined in: physics/src/PhysicsWorld.ts:959

Return all entities with a collider overlapping shape placed at position (pixels, rotation in radians). excludeEntity skips every collider of that entity — pass the querying entity for “what’s around me” queries.

ColliderShape

Vec2Like

Entity

number

number

Entity[]


raycast(origin, direction, maxDistance, options?): RaycastHit | null

Defined in: physics/src/PhysicsWorld.ts:816

Cast a ray and return the first hit. All values in pixels.

The direction is normalized internally, so any non-zero vector works — e.g. target.sub(origin). Throws on a zero-length direction. excludeEntity skips every collider of that entity — pass the caster when the ray starts inside its own collider.

Vec2Like

Vec2Like

number

Entity

number

RaycastHit | null


removeBody(handle): void

Defined in: physics/src/PhysicsWorld.ts:695

Remove a rigid body and all its colliders from the world.

number

void


removeCollider(handle): void

Defined in: physics/src/PhysicsWorld.ts:731

Remove a single collider from the world, leaving its body (if any) intact. No-ops if the handle is already gone — removing a body already tears down its colliders, so this covers the case where that ran first.

number

void


setColliderShape(handle, config, options?): void

Defined in: physics/src/PhysicsWorld.ts:767

Replace a live collider’s shape, keeping its handle, body attachment, collision groups, and event subscriptions. config is the owning component’s config, already carrying the new shape.

The body keeps its mass unless recomputeMass asks for it back from density × the new shape.

number

ColliderConfig

boolean

void


setGravity(x, y): void

Defined in: physics/src/PhysicsWorld.ts:459

Set gravity in pixels/s².

number

number

void


snapshot(): object

Defined in: physics/src/PhysicsWorld.ts:1016

Inspector-facing snapshot of the current rigid bodies and active contacts.

object

bodies: object[]

contacts: object[]


step(dt): void

Defined in: physics/src/PhysicsWorld.ts:156

Step the physics simulation. dt is in seconds.

number

void


toMeters(pixels): number

Defined in: physics/src/PhysicsWorld.ts:138

Convert pixels to meters.

number

number


toPixels(meters): number

Defined in: physics/src/PhysicsWorld.ts:143

Convert meters to pixels.

number

number