World

World the global-state instance of the physics engine that keeps track of all the objects. This provides a higher level of abstraction from the user but may be limiting in some ways. It is a good idea to extend and override this class for any specific properties. With the current SpatialHashing algorithm, the world should have finite bounds.

Constructor

new World(canvas, width, height, xGrids, yGrids, timeStep, iterationPerFrame, constraintIteration)

Instantiates new World instance

Parameters:
NameTypeDefaultDescription
canvasHTMLCanvasElement

HTML canvas where the elements are displayed

widthNumber

width of world

heightNumber

height of world

xGridsNumber

integer number of grid separations in the x direction

yGridsNumbernull

integer number of grid separations in the y direction

timeStepNumber1

change in time per solve iteration

iterationPerFrameNumber1

number of solve iterations per frame

constraintIterationNumber1

number of times constraints are solved per iteration

Methods

addConstraint(c)

Adds a constraint to the world

Parameters:
NameTypeDescription
cConstraint

addGlobalNearBehavior(b)

Adds a NearBehavior to all the particles in the world

Parameters:
NameTypeDescription
bNearBehavior

addGlobalSelfBehavior(b)

Adds a SelfBehavior to all the particles in the world

Parameters:
NameTypeDescription
bSelfBehavior

addParticle(p)

Adds a particle to the world

Parameters:
NameTypeDescription
pParticle

addWall(w)

Adds a wall to the world

Parameters:
NameTypeDescription
wWall

clearConstraints()

Clears all of the constraints

clearParticles()

Clears all of the particles and any associated constraints

clearWalls()

Clears all of the walls

constrainBoundary(x1, x2, y1, y2)

Creates a RectangularWorldBoundary that confines the boundary of the world with rigid collisions. Also sets this.worldConstraint.

Parameters:
NameTypeDescription
x1Number

x value of top-left coordinate (smaller)

x2Number

larger x value of bottom-right coordinate (larger)

y1Number

y value of top-left coordinate (smaller)

y2Number

y value of bottom-right coordinate (larger)

disableChargeInteractions() → {Boolean}

Removes the global ChargeInteraction behavior and sets this.chargeBehavior to null

Returns:

true if charge behavior is successfully disabled

Type: 
Boolean

disableCollisions() → {Boolean}

Removes the global gravity behavior and sets this.collision to null

Returns:

true if collision is successfully disabled

Type: 
Boolean

disableDrag() → {Boolean}

Removes existing drag behavior and sets this.drag to null

Returns:

true if drag is successfully disabled

Type: 
Boolean

disableGravity() → {Boolean}

Removes the global gravity behavior and sets this.gravity to null

Returns:

true if gravity is successfully disabled

Type: 
Boolean

enableChargeInteractions()

Removes existing ChargeInteraction behavior and adds a new global ChargeInteraction behavior to all the particles, while updating this.chargeBehavior

enableCollisions()

Removes existing collision behavior and adds a new global collision behavior to all the particles, while updating this.collision

enableDrag(viscosity)

Removes existing drag behavior and adds a new global drag behavior with a given viscosity to all the particles, while updating this.drag

Parameters:
NameTypeDescription
viscosityNumber

enableGravity(num)

Removes existing gravity and adds a new global gravity behavior to all the particles, while updating this.gravity

Parameters:
NameTypeDescription
numNumber

freePivot(p)

Frees a particle from being a "mass-pivot". This attempts to correct the mass increase from the makePivot() method. Once again, it may not work properly :skull:.

Parameters:
NameTypeDescription
pParticle
Returns:

true if particle is successfully freed from a "mass-pivot".

makePivot(p, pos) → {Boolean}

Make a particle a "mass-pivot" by adding a PositionLock behavior and setting the adds Number.MAX_SAFE_INTEGER / 10 to the particle mass. This is a work around and may cause some other physics to break. This particle pivot will not work with position constraints.

Parameters:
NameTypeDefaultDescription
pParticle
posVector2Dnull
Returns:

true if particle is successfully converted to a "mass-pivot".

Type: 
Boolean

nextFrame()

Progresses world to next state

removeConstraint(c) → {Boolean}

Removes a constraint from the world

Parameters:
NameTypeDescription
cConstraint
Returns:

true if the constraint is removed

Type: 
Boolean

removeGlobalNearBehavior(b)

Removes a NearBehavior to all the particles in the world

Parameters:
NameTypeDescription
bNearBehavior

removeGlobalSelfBehavior(b)

Removes a SelfBehavior from all the particles in the world

Parameters:
NameTypeDescription
bSelfBehavior

removeParticle(p)

Removes a particle from the world

Parameters:
NameTypeDescription
pParticle

removeWall(w) → {boolean}

Removes a wall from the world

Parameters:
NameTypeDescription
wWall
Returns:

true if the wall is removed

Type: 
boolean

setSolverUpdate(update)

Sets the optional update() function for the solver.

Parameters:
NameTypeDescription
updatefunction

updateParticleList()

Update the list of particles. Must be called every time the number of particles change.