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 ​
new PhysicsWorld(options?): PhysicsWorld;Defined in: src/physics/PhysicsWorld.ts:49
Parameters ​
| Parameter | Type |
|---|---|
options | PhysicsWorldOptions |
Returns ​
PhysicsWorld
Properties ​
events ​
readonly events: EventBus<PhysicsEvents>;Defined in: src/physics/PhysicsWorld.ts:25
gravityX ​
gravityX: number;Defined in: src/physics/PhysicsWorld.ts:23
gravityY ​
gravityY: number;Defined in: src/physics/PhysicsWorld.ts:24
preSolve ​
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 ​
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 ​
get bodyCount(): number;Defined in: src/physics/PhysicsWorld.ts:115
Returns ​
number
Methods ​
add() ​
add(body): Body;Defined in: src/physics/PhysicsWorld.ts:72
Adds a body (or makes one from options) and returns it.
Parameters ​
| Parameter | Type |
|---|---|
body | BodyOptions | Body |
Returns ​
addAll() ​
addAll(bodies): void;Defined in: src/physics/PhysicsWorld.ts:83
Parameters ​
| Parameter | Type |
|---|---|
bodies | Iterable<Body> |
Returns ​
void
contactsOf() ​
contactsOf(body, out?): ContactInfo[];Defined in: src/physics/PhysicsWorld.ts:161
The pairs touching body after the last step (sensors' overlaps included).
Parameters ​
| Parameter | Type | Default value |
|---|---|---|
body | Body | undefined |
out | ContactInfo[] | [] |
Returns ​
isGrounded() ​
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 ​
| Parameter | Type |
|---|---|
body | Body |
Returns ​
boolean
raycast() ​
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 ​
| Parameter | Type |
|---|---|
x0 | number |
y0 | number |
x1 | number |
y1 | number |
options? | RaycastOptions |
Returns ​
RayHit | null
remove() ​
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 ​
| Parameter | Type |
|---|---|
body | Body |
Returns ​
void
staticsChanged() ​
staticsChanged(): void;Defined in: src/physics/PhysicsWorld.ts:111
Call after moving, resizing or retyping a static body by hand.
Returns ​
void
step() ​
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 ​
| Parameter | Type |
|---|---|
dt | number |
Returns ​
void