World

World

new World()

Look at the World reference pages before reading these docs. A `world` object is created automatically by p5play. There can only be one world per sketch (instance of p5 or q5). This class extends `planck.World` and adds some p5play specific features.
Source:

Extends

  • planck.World

Classes

World

Members

allowSleeping :Boolean

"Sleeping" sprites get temporarily ignored during physics simulation. A sprite starts "sleeping" when it stops moving and doesn't collide with anything that it wasn't already touching. This is an important performance optimization that you probably shouldn't disable for every sprite in the world.
Default Value:
  • true
Source:
Type:
  • Boolean

gravity :Object

Gravity force vector that affects all dynamic physics colliders.
Properties:
Name Type Description
x Number
y Number
Default Value:
  • { x: 0, y: 0 }
Source:
Type:
  • Object

realTime :Number

The real time in seconds since the world was created, including time spent paused.
Source:
Type:
  • Number

timeScale :Number

A time scale of 1.0 represents real time. Accepts decimal values between 0 and 2.
Default Value:
  • 1.0
Source:
Type:
  • Number

updateRate :Number

The fixed update rate of the physics simulation in hertz. The time step, the amount of time that passes during a physics update, is calculated to be: 1 / updateRate * timeScale Setting the update rate to a value lower than 50hz is not recommended, as simulation quality will degrade.
Default Value:
  • 60
Source:
Type:
  • Number

velocityThreshold :Number

The lowest velocity an object can have before it is considered to be at rest. Adjust the velocity threshold to allow for slow moving objects but don't have it be too low, or else objects will never sleep, which will hurt performance.
Default Value:
  • 0.19
Source:
Type:
  • Number

Methods

extrapolationUpdate(timeStepopt)

Experimental! Visually moves all sprites forward in time by the given time step, based on their current velocity vector and rotation speed. Does not perform any physics calculations. This function may be useful for making extrapolated frames between physics steps, if a frame rate of 100hz or more is desired.
Source:
Parameters:
Name Type Attributes Description
timeStep Number <optional>
time step in seconds

getSpriteAt(x, y, groupopt) → {Sprite}

Returns the sprite at the specified position on the top most layer, drawn when the camera was on. The sprite must have a physics body to be detected.
Source:
Parameters:
Name Type Attributes Description
x Number
y Number
group Group <optional>
the group to search
Returns:
Type:
Sprite
a sprite

getSpritesAt(x, y, groupopt, cameraActiveWhenDrawnopt) → {Array:.<Sprite:>}

Returns the sprites at a position, ordered by layer. Sprites must have a physics body to be detected.
Source:
Parameters:
Name Type Attributes Default Description
x Number x coordinate or position object
y Number
group Group <optional>
limit results to a specific group, allSprites by default
cameraActiveWhenDrawn Boolean <optional>
true limit results to sprites drawn when the camera was active, true by default
Returns:
Type:
Array:.<Sprite:>
an array of sprites

physicsUpdate(timeStepopt, velocityIterationsopt, positionIterationsopt)

Performs a physics simulation step that advances all sprites' forward in time by 1/60th of a second if no timeStep is given. This function is automatically called at the end of the draw loop, unless it was already called inside the draw loop. Setting the timeStep below 1/50th of a second will significantly degrade simulation quality, without improving performance. Decreasing `velocityIterations` and `positionIterations` will improve performance but decrease simulation quality.
Source:
Parameters:
Name Type Attributes Description
timeStep Number <optional>
time step in seconds
velocityIterations Number <optional>
8 by default
positionIterations Number <optional>
3 by default

rayCast(startPos, direction, maxDistance) → {Sprite}

Finds the first sprite (with a physics body) that intersects a ray (line), excluding any sprites that intersect with the starting point. This function can also be given start and end points.
Source:
Parameters:
Name Type Description
startPos Object starting position of the ray cast
direction Number direction of the ray
maxDistance Number max distance the ray should check
Returns:
Type:
Sprite
The first sprite the ray hits or undefined

rayCastAll(startPos, direction, maxDistance, limiteropt) → {Array:.<Sprite:>}

Finds sprites (with physics bodies) that intersect a line (ray), excluding any sprites that intersect the starting point. This function can also be given start and end points.
Source:
Parameters:
Name Type Attributes Description
startPos Object starting position of the ray cast
direction Number direction of the ray
maxDistance Number max distance the ray should check
limiter function <optional>
limiter function that's run each time the ray intersects a sprite, return true to stop the ray
Returns:
Type:
Array:.<Sprite:>
An array of sprites that the ray cast hit, sorted by distance. The sprite closest to the starting point will be at index 0.