Skip to main content

Class: PhysicsWorld

Defined in: physics/physics-world.ts:71

A native 2D physics simulation containing RigidBody instances. Each step integrates motion, applies gravity, and detects and resolves collisions between bodies.

Constructors

Constructor

new PhysicsWorld(options?): PhysicsWorld

Defined in: physics/physics-world.ts:88

Creates a new PhysicsWorld instance.

Parameters

options?

PhysicsWorldOptions = {}

The options for the world.

Returns

PhysicsWorld

Properties

gravity

gravity: Vector2

Defined in: physics/physics-world.ts:72

Accessors

bodies

Get Signature

get bodies(): readonly RigidBody[]

Defined in: physics/physics-world.ts:102

The bodies currently registered in this world.

Returns

readonly RigidBody[]


collisionEnds

Get Signature

get collisionEnds(): readonly BodyCollisionPair[]

Defined in: physics/physics-world.ts:125

The pairs of bodies that stopped colliding during the most recent step.

Returns

readonly BodyCollisionPair[]


collisionStarts

Get Signature

get collisionStarts(): readonly BodyCollisionPair[]

Defined in: physics/physics-world.ts:117

The pairs of bodies that started colliding during the most recent step.

Returns

readonly BodyCollisionPair[]


joints

Get Signature

get joints(): readonly Joint[]

Defined in: physics/physics-world.ts:109

The joints currently registered in this world.

Returns

readonly Joint[]

Methods

addBody()

addBody(body): void

Defined in: physics/physics-world.ts:133

Registers a body with this world.

Parameters

body

RigidBody

The body to add.

Returns

void


addJoint()

addJoint(joint): void

Defined in: physics/physics-world.ts:156

Registers a joint with this world.

Parameters

joint

Joint

The joint to add.

Returns

void


applyExplosiveForce()

applyExplosiveForce(center, force, radius): void

Defined in: physics/physics-world.ts:199

Applies a radial impulse to every non-static body within radius of center, simulating an explosion. The impulse magnitude falls off linearly from force at center to zero at radius, and is applied through each body's center of mass so no torque is imparted. Static bodies and bodies at or beyond radius are unaffected.

Parameters

center

Vector2

The world-space origin of the explosion.

force

number

The impulse magnitude applied to a body located at center.

radius

number

The distance from center beyond which bodies are unaffected.

Returns

void


removeBody()

removeBody(body): void

Defined in: physics/physics-world.ts:142

Removes a body from this world.

Parameters

body

RigidBody

The body to remove.

Returns

void

Throws

An error if the body is not registered in this world.


removeJoint()

removeJoint(joint): void

Defined in: physics/physics-world.ts:165

Removes a joint from this world.

Parameters

joint

Joint

The joint to remove.

Returns

void

Throws

An error if the joint is not registered in this world.


step()

step(deltaTimeInSeconds): void

Defined in: physics/physics-world.ts:181

Advances the simulation by a fixed time step: integrates velocities and positions for dynamic bodies, then detects and resolves collisions.

Parameters

deltaTimeInSeconds

number

The amount of time to step the simulation by, in seconds.

Returns

void