Skip to content

@ue-too/dynamics / index / World

Class: World ​

Defined in: world.ts:65

Main physics world managing rigid body simulation.

Remarks ​

The World class is the main entry point for physics simulation. It manages all rigid bodies, runs collision detection and response, enforces constraints, and provides performance optimizations like sleeping and spatial indexing.

Simulation Loop ​

Call world.step(deltaTime) each frame to advance the simulation. This:

  1. Updates sleeping states
  2. Detects collisions (broad and narrow phase)
  3. Resolves collisions with impulses
  4. Enforces constraints
  5. Updates body positions and velocities

Performance Tuning ​

  • Choose appropriate spatial index for your use case
  • Enable sleeping for better performance with many bodies
  • Use collision filtering to reduce collision checks

Example ​

Basic setup

typescript
const world = new World(2000, 2000, 'dynamictree');

// Add bodies
const ball = new Circle({ x: 0, y: 100 }, 20, 0, 10, false);
world.addRigidBody('ball', ball);

// Simulation loop
function update(dt: number) {
  world.step(dt);
}

Constructors ​

Constructor ​

new World(maxTransWidth, maxTransHeight, spatialIndexType): World

Defined in: world.ts:82

Parameters ​

maxTransWidth ​

number

maxTransHeight ​

number

spatialIndexType ​

SpatialIndexType = 'dynamictree'

Returns ​

World

Properties ​

_context ​

_context: CanvasRenderingContext2D | null = null

Defined in: world.ts:80

Accessors ​

currentSpatialIndexType ​

Get Signature ​

get currentSpatialIndexType(): SpatialIndexType

Defined in: world.ts:284

Returns ​

SpatialIndexType


resolveCollision ​

Get Signature ​

get resolveCollision(): boolean

Defined in: world.ts:231

Returns ​

boolean

Set Signature ​

set resolveCollision(resolveCollision): void

Defined in: world.ts:235

Parameters ​
resolveCollision ​

boolean

Returns ​

void


sleepingEnabled ​

Get Signature ​

get sleepingEnabled(): boolean

Defined in: world.ts:314

Returns ​

boolean

Set Signature ​

set sleepingEnabled(enabled): void

Defined in: world.ts:318

Parameters ​
enabled ​

boolean

Returns ​

void


useLinearCollisionResolution ​

Get Signature ​

get useLinearCollisionResolution(): boolean

Defined in: world.ts:239

Returns ​

boolean

Set Signature ​

set useLinearCollisionResolution(value): void

Defined in: world.ts:243

Parameters ​
value ​

boolean

Returns ​

void

Methods ​

addConstraint() ​

addConstraint(constraint): void

Defined in: world.ts:267

Parameters ​

constraint ​

Constraint

Returns ​

void


addPinJoint() ​

addPinJoint(bodyA, bodyB, anchorA, anchorB): void

Defined in: world.ts:275

Parameters ​

bodyA ​

RigidBody

bodyB ​

RigidBody

anchorA ​

Point

anchorB ​

Point

Returns ​

void


addRigidBody() ​

addRigidBody(ident, body): void

Defined in: world.ts:112

Parameters ​

ident ​

string

body ​

RigidBody

Returns ​

void


getCollisionStats() ​

getCollisionStats(): object

Defined in: world.ts:336

Returns ​

object

activePairs ​

activePairs: number

frameNumber ​

frameNumber: number

sleepingBodies ​

sleepingBodies: number

totalBodies ​

totalBodies: number

totalPairs ​

totalPairs: number


getConstraints() ​

getConstraints(): Constraint[]

Defined in: world.ts:271

Returns ​

Constraint[]


getPairManager() ​

getPairManager(): PairManager

Defined in: world.ts:331

Returns ​

PairManager


getRigidBodyList() ​

getRigidBodyList(): RigidBody[]

Defined in: world.ts:247

Returns ​

RigidBody[]


getRigidBodyMap() ​

getRigidBodyMap(): Map<string, RigidBody>

Defined in: world.ts:255

Returns ​

Map<string, RigidBody>


getSpatialIndexStats() ​

getSpatialIndexStats(): any

Defined in: world.ts:301

Returns ​

any


removeRigidBody() ​

removeRigidBody(ident): void

Defined in: world.ts:122

Parameters ​

ident ​

string

Returns ​

void


resolveCollisionPhase() ​

resolveCollisionPhase(): Point[]

Defined in: world.ts:175

Returns ​

Point[]


setMaxTransHeight() ​

setMaxTransHeight(height): void

Defined in: world.ts:259

Parameters ​

height ​

number

Returns ​

void


setMaxTransWidth() ​

setMaxTransWidth(width): void

Defined in: world.ts:263

Parameters ​

width ​

number

Returns ​

void


setSpatialIndexType() ​

setSpatialIndexType(type): void

Defined in: world.ts:288

Parameters ​

type ​

SpatialIndexType

Returns ​

void


step() ​

step(deltaTime): void

Defined in: world.ts:144

Parameters ​

deltaTime ​

number

Returns ​

void