Skip to content

eulerjs API / @eulerjs/core / World

World ​

Defined in: packages/core/src/World.ts:60

The simulation: bodies, joints and plugins, stepped at a fixed timestep by its backend. Make one next to your three.js scene, add bodies bound to your objects, and call update with each frame's time to step it and move the objects to their bodies.

Implements ​

Constructors ​

Constructor ​

ts
new World(options?): World;

Defined in: packages/core/src/World.ts:99

Parameters ​

ParameterType
options?WorldOptions

Returns ​

World

Properties ​

PropertyModifierTypeDescriptionDefined in
backendreadonlyPhysicsBackend-packages/core/src/World.ts:69
gravityreadonlyVector3-packages/core/src/World.ts:61
maxStepsPerFramepublicnumber-packages/core/src/World.ts:63
sleepSpeedpublicnumberBodies slower than this (m/s) and sleepSpin (rad/s) for timeToSleep seconds fall asleep.packages/core/src/World.ts:65
sleepSpinpublicnumber-packages/core/src/World.ts:66
substepspublicnumberThe backend's accuracy setting: how many smaller steps it splits a step into, or its nearest equivalent.packages/core/src/World.ts:64
timestepreadonlynumberLength of the step in seconds.packages/core/src/World.ts:62
timeToSleeppublicnumber-packages/core/src/World.ts:67

Accessors ​

bodies ​

Get Signature ​

ts
get bodies(): readonly Body[];

Defined in: packages/core/src/World.ts:138

Returns ​

readonly Body[]


contacts ​

Get Signature ​

ts
get contacts(): readonly Contact[];

Defined in: packages/core/src/World.ts:151

The contacts that pushed bodies apart during the last step, one for each touching point.

Returns ​

readonly Contact[]


interpolationAlpha ​

Get Signature ​

ts
get interpolationAlpha(): number;

Defined in: packages/core/src/World.ts:126

How far rendering is between the last two steps, from 0 to 1.

Returns ​

number


joints ​

Get Signature ​

ts
get joints(): readonly Joint[];

Defined in: packages/core/src/World.ts:146

Returns ​

readonly Joint[]


plugins ​

Get Signature ​

ts
get plugins(): readonly Plugin[];

Defined in: packages/core/src/World.ts:142

Returns ​

readonly Plugin[]


renderTime ​

Get Signature ​

ts
get renderTime(): number;

Defined in: packages/core/src/World.ts:134

The moment bodies are drawn at after syncObjects: alpha of the way through the last step. Animate anything that should match the bodies, such as water, with this time.

Returns ​

number


stepCount ​

Get Signature ​

ts
get stepCount(): number;

Defined in: packages/core/src/World.ts:121

Returns ​

number


time ​

Get Signature ​

ts
get time(): number;

Defined in: packages/core/src/World.ts:117

Simulated time in seconds: the number of completed steps times the timestep.

Returns ​

number

Methods ​

addBody() ​

Call Signature ​

ts
addBody<SomeBody>(body): SomeBody;

Defined in: packages/core/src/World.ts:249

Adds a body. Given a three.js object instead, makes a body for it first: mass properties from its meshes (water density unless you give a mass or density), starting pose from where the object is. It collides as options.collider says and only then: without it the body has no colliders and falls through everything, on purpose, since the mesh drawn is seldom the shape to collide as.

Type Parameters ​
Type Parameter
SomeBody extends Body
Parameters ​
ParameterType
bodySomeBody
Returns ​

SomeBody

Call Signature ​

ts
addBody(object, options?): Body;

Defined in: packages/core/src/World.ts:250

Adds a body. Given a three.js object instead, makes a body for it first: mass properties from its meshes (water density unless you give a mass or density), starting pose from where the object is. It collides as options.collider says and only then: without it the body has no colliders and falls through everything, on purpose, since the mesh drawn is seldom the shape to collide as.

Parameters ​
ParameterType
objectObject3D
options?ObjectBodyOptions
Returns ​

Body


addInstances() ​

ts
addInstances(mesh, options?): Body[];

Defined in: packages/core/src/World.ts:287

Adds a body for every instance of an InstancedMesh, each drawn as its instance: posed where the instance is, colliding as options.collider says, and weighed from the mesh's geometry (water density unless you give a mass or density, which is each body's). Returns them in the instances' order.

Parameters ​

ParameterType
meshInstancedMesh
options?InstancedBodyOptions

Returns ​

Body[]


addJoint() ​

ts
addJoint<SomeJoint>(joint): SomeJoint;

Defined in: packages/core/src/World.ts:207

Adds a joint between bodies already in this world.

Type Parameters ​

Type Parameter
SomeJoint extends Joint

Parameters ​

ParameterType
jointSomeJoint

Returns ​

SomeJoint


bodyOf() ​

ts
bodyOf(object, instanceId?): Body | undefined;

Defined in: packages/core/src/World.ts:341

The body that moves object, or one of its ancestors: e.g. the body of a mesh a raycast hit. For an InstancedMesh, give the instance too, as a three.js raycast's instanceId gives it.

Parameters ​

ParameterType
objectObject3D
instanceId?number

Returns ​

Body | undefined


castRay() ​

ts
castRay(
   origin, 
   direction, 
   options?
): QueryHit | undefined;

Defined in: packages/core/src/World.ts:160

The first collider a ray from origin along direction meets, with where and how far away: for shooting, picking, line of sight and checking for ground. Sensors are skipped unless the options include them, and a ray starting inside a solid meets it at once.

Parameters ​

ParameterType
originVector3
directionVector3
options?RayCastOptions

Returns ​

QueryHit | undefined


castRayAll() ​

ts
castRayAll(
   origin, 
   direction, 
   options?
): QueryHit[];

Defined in: packages/core/src/World.ts:165

Every collider a ray from origin along direction meets, nearest first.

Parameters ​

ParameterType
originVector3
directionVector3
options?RayCastOptions

Returns ​

QueryHit[]


castShape() ​

ts
castShape(
   shape, 
   position, 
   direction, 
   options?
): QueryHit | undefined;

Defined in: packages/core/src/World.ts:174

Where a convex shape (a sphere, capsule, box, hull, cylinder or cone), starting at position and sliding along direction, first touches a collider: the usual way to move a character or check that something fits. The shape needn't belong to any body.

Parameters ​

ParameterType
shapeCollider
positionVector3
directionVector3
options?ShapeCastOptions

Returns ​

QueryHit | undefined


dispose() ​

ts
dispose(): void;

Defined in: packages/core/src/World.ts:535

Removes every plugin (calling their dispose hooks) and body, then disposes of the backend.

Returns ​

void


fixedStep() ​

ts
fixedStep(): void;

Defined in: packages/core/src/World.ts:392

Runs exactly one fixed step. Useful for tests and for driving the clock yourself.

Changes you make to bodies between steps reach the backend before the behaviours run; changes made by behaviours and plugins reach it before it simulates.

Returns ​

void


inSensor() ​

ts
inSensor(sensor): OverlapHit[];

Defined in: packages/core/src/World.ts:202

Every collider in a sensor as of the last step, and its body: what sensorEnter brought in and sensorExit hasn't taken out, including bodies asleep in it.

Parameters ​

ParameterType
sensorCollider

Returns ​

OverlapHit[]


off() ​

ts
off<Name>(name, listener): void;

Defined in: packages/core/src/World.ts:194

Type Parameters ​

Type Parameter
Name extends keyof WorldEvents

Parameters ​

ParameterType
nameName
listenerWorldEventListener<Name>

Returns ​

void


on() ​

ts
on<Name>(name, listener): () => void;

Defined in: packages/core/src/World.ts:190

Listens for an event, sent after each step: contacts beginning and ending, things entering and leaving sensors, joints breaking. Returns a function that stops listening.

Type Parameters ​

Type Parameter
Name extends keyof WorldEvents

Parameters ​

ParameterType
nameName
listenerWorldEventListener<Name>

Returns ​

() => void


overlap() ​

ts
overlap(
   shape, 
   position, 
   options?
): OverlapHit[];

Defined in: packages/core/src/World.ts:181

Every collider a convex shape at position overlaps or touches: what is inside an explosion, say.

Parameters ​

ParameterType
shapeCollider
positionVector3
options?OverlapOptions

Returns ​

OverlapHit[]


removeBody() ​

ts
removeBody(body): void;

Defined in: packages/core/src/World.ts:292

Removes the body, and the joints that tie it to others.

Parameters ​

ParameterType
bodyBody

Returns ​

void


removeJoint() ​

ts
removeJoint(joint): void;

Defined in: packages/core/src/World.ts:235

Parameters ​

ParameterType
jointJoint

Returns ​

void


removePlugin() ​

ts
removePlugin(plugin): void;

Defined in: packages/core/src/World.ts:357

Parameters ​

ParameterType
pluginPlugin

Returns ​

void


setCollisionsBetween() ​

ts
setCollisionsBetween(
   bodyA, 
   bodyB, 
   enabled
): void;

Defined in: packages/core/src/World.ts:312

Stops two bodies in this world colliding with each other, or lets them again: say, parts of a ragdoll near each other that touch when it folds up. Bodies tied by a joint can instead have it set collideConnected: false.

Parameters ​

ParameterType
bodyABody
bodyBBody
enabledboolean

Returns ​

void


step() ​

ts
step(frameDelta): number;

Defined in: packages/core/src/World.ts:368

Advances the simulation by a frame's worth of real time, in as many fixed steps as fit. Returns the interpolation alpha to pass to syncObjects.

Parameters ​

ParameterType
frameDeltanumber

Returns ​

number


syncObjects() ​

ts
syncObjects(alpha?): void;

Defined in: packages/core/src/World.ts:530

Moves every bound three.js object to its body's pose, interpolated between the last two steps.

Parameters ​

ParameterType
alpha?number

Returns ​

void


update() ​

ts
update(frameDelta): number;

Defined in: packages/core/src/World.ts:380

step then syncObjects: everything a render loop needs. Returns the interpolation alpha.

Parameters ​

ParameterType
frameDeltanumber

Returns ​

number


use() ​

ts
use<SomePlugin>(plugin): SomePlugin;

Defined in: packages/core/src/World.ts:350

Type Parameters ​

Type Parameter
SomePlugin extends Plugin

Parameters ​

ParameterType
pluginSomePlugin

Returns ​

SomePlugin

Released under the MIT License.