Skip to content

1681-engine / index / PhysicsWorld

Class: PhysicsWorld ​

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

A rigid-body world for platformers and physics toys: dynamic, static and kinematic circles, boxes and convex polygons, turned by contacts when made with rotates: true, stepped at a fixed dt (physicsSystem runs it as a fixed system). Each step: forces and gravity (semi-implicit Euler), broad phase, narrow phase, sequential impulses with warm starting, then positions, then overlap removed by moving positions. Fast bodies are handled by sub-stepping, not swept tests.

Not deterministic across machines: the solver's results depend on body order and floating point, which is fine for a single player's world but not for one a server must replay (use net/movement for that).

Constructors ​

Constructor ​

ts
new PhysicsWorld(options?): PhysicsWorld;

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

Parameters ​

ParameterType
optionsPhysicsWorldOptions

Returns ​

PhysicsWorld

Properties ​

events ​

ts
readonly events: EventBus<PhysicsEvents>;

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


gravityX ​

ts
gravityX: number;

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


gravityY ​

ts
gravityY: number;

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


preSolve ​

ts
preSolve: ((contact) => boolean) | null = null;

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

Called for each touching solid pair every sub-step; returning false skips its response this sub-step (one-way platforms).

Accessors ​

bodies ​

Get Signature ​

ts
get bodies(): readonly Body[];

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

Every body in the world, in the order added: for tools (a debug drawing, an inspector) that would otherwise keep a second list beside the world's. Read it, never hold it across a step: adding and removing change it in place.

Returns ​

readonly Body[]


bodyCount ​

Get Signature ​

ts
get bodyCount(): number;

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

Returns ​

number

Methods ​

add() ​

ts
add(body): Body;

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

Adds a body (or makes one from options) and returns it.

Parameters ​

ParameterType
bodyBodyOptions | Body

Returns ​

Body


addAll() ​

ts
addAll(bodies): void;

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

Parameters ​

ParameterType
bodiesIterable<Body>

Returns ​

void


contactsOf() ​

ts
contactsOf(body, out?): ContactInfo[];

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

The pairs touching body after the last step (sensors' overlaps included).

Parameters ​

ParameterTypeDefault value
bodyBodyundefined
outContactInfo[][]

Returns ​

ContactInfo[]


isGrounded() ​

ts
isGrounded(body): boolean;

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

Whether body stood on something at the end of the last step (also body.grounded).

Parameters ​

ParameterType
bodyBody

Returns ​

boolean


raycast() ​

ts
raycast(
   x0, 
   y0, 
   x1, 
   y1, 
   options?
): RayHit | null;

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

The first body on the segment from (x0, y0) to (x1, y1). Bodies the ray starts inside are skipped.

Parameters ​

ParameterType
x0number
y0number
x1number
y1number
options?RaycastOptions

Returns ​

RayHit | null


remove() ​

ts
remove(body): void;

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

Takes a body out; its contacts end (with end events). During a step it waits until the step is over.

Parameters ​

ParameterType
bodyBody

Returns ​

void


staticsChanged() ​

ts
staticsChanged(): void;

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

Call after moving, resizing or retyping a static body by hand.

Returns ​

void


step() ​

ts
step(dt): void;

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

Advances the world by dt seconds; call it with the fixed step (FixedLoop.stepSeconds).

Parameters ​

ParameterType
dtnumber

Returns ​

void

Released under the MIT License.