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
new World(options?): World;Defined in: packages/core/src/World.ts:99
Parameters
| Parameter | Type |
|---|---|
options? | WorldOptions |
Returns
World
Properties
| Property | Modifier | Type | Description | Defined in |
|---|---|---|---|---|
backend | readonly | PhysicsBackend | - | packages/core/src/World.ts:69 |
gravity | readonly | Vector3 | - | packages/core/src/World.ts:61 |
maxStepsPerFrame | public | number | - | packages/core/src/World.ts:63 |
sleepSpeed | public | number | Bodies slower than this (m/s) and sleepSpin (rad/s) for timeToSleep seconds fall asleep. | packages/core/src/World.ts:65 |
sleepSpin | public | number | - | packages/core/src/World.ts:66 |
substeps | public | number | The backend's accuracy setting: how many smaller steps it splits a step into, or its nearest equivalent. | packages/core/src/World.ts:64 |
timestep | readonly | number | Length of the step in seconds. | packages/core/src/World.ts:62 |
timeToSleep | public | number | - | packages/core/src/World.ts:67 |
Accessors
bodies
Get Signature
get bodies(): readonly Body[];Defined in: packages/core/src/World.ts:138
Returns
readonly Body[]
contacts
Get Signature
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
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
get joints(): readonly Joint[];Defined in: packages/core/src/World.ts:146
Returns
readonly Joint[]
plugins
Get Signature
get plugins(): readonly Plugin[];Defined in: packages/core/src/World.ts:142
Returns
readonly Plugin[]
renderTime
Get Signature
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
get stepCount(): number;Defined in: packages/core/src/World.ts:121
Returns
number
time
Get Signature
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
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
| Parameter | Type |
|---|---|
body | SomeBody |
Returns
SomeBody
Call Signature
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
| Parameter | Type |
|---|---|
object | Object3D |
options? | ObjectBodyOptions |
Returns
addInstances()
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
| Parameter | Type |
|---|---|
mesh | InstancedMesh |
options? | InstancedBodyOptions |
Returns
Body[]
addJoint()
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
| Parameter | Type |
|---|---|
joint | SomeJoint |
Returns
SomeJoint
bodyOf()
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
| Parameter | Type |
|---|---|
object | Object3D |
instanceId? | number |
Returns
Body | undefined
castRay()
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
| Parameter | Type |
|---|---|
origin | Vector3 |
direction | Vector3 |
options? | RayCastOptions |
Returns
QueryHit | undefined
castRayAll()
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
| Parameter | Type |
|---|---|
origin | Vector3 |
direction | Vector3 |
options? | RayCastOptions |
Returns
QueryHit[]
castShape()
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
| Parameter | Type |
|---|---|
shape | Collider |
position | Vector3 |
direction | Vector3 |
options? | ShapeCastOptions |
Returns
QueryHit | undefined
dispose()
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()
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()
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
| Parameter | Type |
|---|---|
sensor | Collider |
Returns
off()
off<Name>(name, listener): void;Defined in: packages/core/src/World.ts:194
Type Parameters
| Type Parameter |
|---|
Name extends keyof WorldEvents |
Parameters
| Parameter | Type |
|---|---|
name | Name |
listener | WorldEventListener<Name> |
Returns
void
on()
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
| Parameter | Type |
|---|---|
name | Name |
listener | WorldEventListener<Name> |
Returns
() => void
overlap()
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
| Parameter | Type |
|---|---|
shape | Collider |
position | Vector3 |
options? | OverlapOptions |
Returns
removeBody()
removeBody(body): void;Defined in: packages/core/src/World.ts:292
Removes the body, and the joints that tie it to others.
Parameters
| Parameter | Type |
|---|---|
body | Body |
Returns
void
removeJoint()
removeJoint(joint): void;Defined in: packages/core/src/World.ts:235
Parameters
| Parameter | Type |
|---|---|
joint | Joint |
Returns
void
removePlugin()
removePlugin(plugin): void;Defined in: packages/core/src/World.ts:357
Parameters
| Parameter | Type |
|---|---|
plugin | Plugin |
Returns
void
setCollisionsBetween()
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
| Parameter | Type |
|---|---|
bodyA | Body |
bodyB | Body |
enabled | boolean |
Returns
void
step()
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
| Parameter | Type |
|---|---|
frameDelta | number |
Returns
number
syncObjects()
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
| Parameter | Type |
|---|---|
alpha? | number |
Returns
void
update()
update(frameDelta): number;Defined in: packages/core/src/World.ts:380
step then syncObjects: everything a render loop needs. Returns the interpolation alpha.
Parameters
| Parameter | Type |
|---|---|
frameDelta | number |
Returns
number
use()
use<SomePlugin>(plugin): SomePlugin;Defined in: packages/core/src/World.ts:350
Type Parameters
| Type Parameter |
|---|
SomePlugin extends Plugin |
Parameters
| Parameter | Type |
|---|---|
plugin | SomePlugin |
Returns
SomePlugin
