World Class
The dynamics world, where all bodies and constraints lives.
Constructor
World
-
[options]
Parameters:
-
[options]
Object optional-
solver
SolverDefaults to GSSolver.
-
gravity
ArrayDefaults to [0,-9.78]
-
broadphase
BroadphaseDefaults to NaiveBroadphase
-
islandSplit
Boolean -
doProfiling
Boolean
-
Item Index
Methods
- addBody
- addConstraint
- addContactMaterial
- addSpring
- clear
- clone
- disableCollision
- emit
- enableCollision
- fromJSON
- getBodyById
- getContactMaterial
- has
- hitTest
- integrateBody static
- internalStep
- off
- on
- removeBody
- removeConstraint
- removeContactMaterial
- removeSpring
- runNarrowphase
- setGlobalEquationParameters
- setGlobalRelaxation
- setGlobalStiffness
- step
- toJSON deprecated
Properties
- allowSleep
- applyDamping
- applyGravity
- applySpringForces
- bodies
- bodiesToBeRemoved
- broadphase
- constraints
- contactMaterials
- defaultContactMaterial
- defaultMaterial
- disabledBodyCollisionPairs
- doPofiling
- emitImpactEvent
- enableIslandSleeping
- frictionGravity
- gravity
- islandManager
- islandSplit
- lastStepTime
- lastTimeStep
- narrowphase
- solveConstraints
- solver
- springs
- stepping
- time
- useFrictionGravityOnZeroGravity
- useWorldGravityAsFrictionGravity
Methods
addBody
-
body
Add a body to the simulation
Parameters:
-
body
Body
Example:
var world = new World(),
body = new Body();
world.addBody(body);
addContactMaterial
-
contactMaterial
Add a ContactMaterial to the simulation.
Parameters:
-
contactMaterial
ContactMaterial
clear
()
Resets the World, removes all bodies, constraints and springs.
disableCollision
-
bodyA
-
bodyB
Disable collision between two bodies
emit
-
event
Emit an event.
Parameters:
-
event
Object-
type
String
-
Returns:
The self object, for chainability.
enableCollision
-
bodyA
-
bodyB
Enable collisions between the given two bodies
fromJSON
-
json
Load a scene from a serialized state in JSON format.
Parameters:
-
json
Object
Returns:
True on success, else false.
getBodyById
()
Body | Boolean
Get a body by its id.
Returns:
The body, or false if it was not found.
getContactMaterial
-
materialA
-
materialB
Get a contact material given two materials
Returns:
The matching ContactMaterial, or false on fail.
has
-
type
-
listener
Check if an event listener is added
Parameters:
-
type
String -
listener
Function
Returns:
hitTest
-
worldPoint
-
bodies
-
precision
Test if a world point overlaps bodies
Parameters:
-
worldPoint
ArrayPoint to use for intersection tests
-
bodies
ArrayA list of objects to check for intersection
-
precision
NumberUsed for matching against particles and lines. Adds some margin to these infinitesimal objects.
Returns:
Array of bodies that overlap the point
internalStep
-
dt
Make a fixed step.
Parameters:
-
dt
Number
off
-
type
-
listener
Remove an event listener
Parameters:
-
type
String -
listener
Function
Returns:
The self object, for chainability.
on
-
type
-
listener
Add an event listener
Parameters:
-
type
String -
listener
Function
Returns:
The self object, for chainability.
removeBody
-
body
Remove a body from the simulation. If this method is called during step(), the body removal is scheduled to after the step.
Parameters:
-
body
Body
runNarrowphase
-
np
-
bi
-
si
-
xi
-
ai
-
bj
-
sj
-
xj
-
aj
-
mu
Runs narrowphase for the shape pair i and j.
Parameters:
-
np
Narrowphase -
bi
Body -
si
Shape -
xi
Array -
ai
Number -
bj
Body -
sj
Shape -
xj
Array -
aj
Number -
mu
Number
setGlobalEquationParameters
-
[parameters]
Sets the Equation parameters for all constraints and contact materials.
Parameters:
-
[parameters]
Object optional-
[relaxation]
Number optional -
[stiffness]
Number optional
-
setGlobalRelaxation
-
relaxation
Set the relaxation for all equations and contact materials.
Parameters:
-
relaxation
Number
setGlobalStiffness
-
stiffness
Set the stiffness for all equations and contact materials.
Parameters:
-
stiffness
Number
step
-
dt
-
[timeSinceLastCalled=0]
-
[maxSubSteps=10]
Step the physics world forward in time.
There are two modes. The simple mode is fixed timestepping without interpolation. In this case you only use the first argument. The second case uses interpolation. In that you also provide the time since the function was last used, as well as the maximum fixed timesteps to take.
Parameters:
-
dt
NumberThe fixed time step size to use.
-
[timeSinceLastCalled=0]
Number optionalThe time elapsed since the function was last called.
-
[maxSubSteps=10]
Number optionalMaximum number of fixed steps to take per function call.
Example:
// fixed timestepping without interpolation
var world = new World();
world.step(0.01);
toJSON
()
Object
deprecated
Convert the world to a JSON-serializable Object.
Returns:
Properties
allowSleep
Boolean
Enable / disable automatic body sleeping. Sleeping can improve performance. You might need to wake up the bodies if they fall asleep when they shouldn't. If you want to enable sleeping in the world, but want to disable it for a particular body, see Body.allowSleep.
applyDamping
Boolean
Enable to automatically apply body damping each step.
applyGravity
Boolean
Enable to automatically apply gravity each step.
applySpringForces
Boolean
Enable to automatically apply spring forces each step.
bodiesToBeRemoved
Array
private
Bodies that are scheduled to be removed at the end of the step.
constraints
Array
User-added constraints.
contactMaterials
Array
The ContactMaterials added to the World.
defaultContactMaterial
ContactMaterial
The default contact material to use, if no contact material was set for the colliding materials.
disabledBodyCollisionPairs
Array
private
Disabled body collision pairs. See {{#crossLink "World/disableBodyCollision:method"}}.
doPofiling
Boolean
Whether to do timing measurements during the step() or not.
emitImpactEvent
Boolean
Set to true if you want to the world to emit the "impact" event. Turning this off could improve performance.
enableIslandSleeping
Boolean
Enable or disable island sleeping. Note that you must enable .islandSplit for this to work.
frictionGravity
Number
Gravity to use when approximating the friction max force (mumassgravity).
gravity
Array
Gravity in the world. This is applied on all bodies in the beginning of each step().
islandSplit
Boolean
Whether to enable island splitting. Island splitting can be an advantage for many things, including solver performance. See IslandManager.
lastStepTime
Number
How many millisecconds the last step() took. This is updated each step if .doProfiling is set to true.
lastTimeStep
Number
For keeping track of what time step size we used last step
solveConstraints
Boolean
Enable/disable constraint solving in each step.
stepping
Boolean
Is true during the step().
time
Number
World time.
useFrictionGravityOnZeroGravity
Boolean
If the length of .gravity is zero, and .useWorldGravityAsFrictionGravity=true, then switch to using .frictionGravity for friction instead. This fallback is useful for gravityless games.
useWorldGravityAsFrictionGravity
Boolean
Set to true if you want .frictionGravity to be automatically set to the length of .gravity.
Events
beginContact
Fired when two shapes starts start to overlap. Fired in the narrowphase, during step.
endContact
Fired when two shapes stop overlapping, after the narrowphase (during step).
impact
Fired when a first contact is created between two bodies. This event is fired after the step has been done.
postBroadphase
Fired after the Broadphase has collected collision pairs in the world. Inside the event handler, you can modify the pairs array as you like, to prevent collisions between objects that you don't want.
Event Payload:
-
pairs
ArrayAn array of collision pairs. If this array is [body1,body2,body3,body4], then the body pairs 1,2 and 3,4 would advance to narrowphase.
postStep
Fired after the step().
preSolve
Fired just before equations are added to the solver to be solved. Can be used to control what equations goes into the solver.
Event Payload:
-
contactEquations
ArrayAn array of contacts to be solved.
-
frictionEquations
ArrayAn array of friction equations to be solved.