API Docs for: v0.1.0
Show:

PhysicsSystem Class

Extends Object

The PhysicsSystem is the wrapper around the chipmunk-js physics library that integrates grapefruit objects into the physics world. It is in charge of managing objects in the physics space. Generally you would not create this yourself and instead would use the .physics property of a State.

Constructor

PhysicsSystem

(
  • state
  • [options]
  • [options.gravity=new
)

Parameters:

  • state State

    The state instance this system belongs to.

  • [options] Object optional

    The options for the physics system.

  • [options.gravity=new Vector

    Vector(0, 9.87)] The gravity of the space

Methods

_createBody

(
  • spr
)
cp.Body private

Creates a physics body for a sprite

Parameters:

  • spr Sprite

    The sprite to create a body for

Returns:

cp.Body:

The chipmunk-js physics body

_createShape

(
  • spr
  • body
  • [poly]
)
cp.Shape private

Creates a collision shape for a sprite

Parameters:

  • spr Sprite

    The sprite to create a shape for

  • body cp.Body

    The body to attach the shape to

  • [poly] Circle | Rectangle | Polygon optional

    The shape to create, defaults to spr.hitArea

Returns:

cp.Shape:

The chipmunk-js collision shape

add

(
  • spr
  • [callback]
)
Sprite async

Adds a sprite to the physics simulation

Parameters:

  • spr Sprite

    The sprite to add

  • [callback] Function optional

    The callback to call once the sprite has been added

Returns:

Sprite:

The sprite that was added

addCustomShape

(
  • spr
  • poly
  • sensor
  • [callback]
)
cp.Shape async

Adds a custom shape to a sprite, useful for a single sprite to have multiple different collision shapes (including sensors).

Parameters:

  • spr Sprite

    The sprite to add the shape to

  • poly Circle | Rectangle | Polygon

    The shape to create

  • sensor Boolean

    Is this a sensor shape, if so you will get a collision callback, but no solve

  • [callback] Function optional

    The callback to call once the shape has been added

Returns:

cp.Shape:

The shape that was created

getCollisionType

(
  • spr
)
Number

Returns the collision type of a sprite

Parameters:

  • spr Sprite

    The sprite to check

Returns:

Number:

The collision type

nextTick

(
  • fn
)
PhysicsSystem chainable async

Registers a callback to be executed on the next frame step

Parameters:

  • fn Function

    The callback to register

Returns:

PhysicsSystem:

Returns itself.

onCollisionBegin

(
  • arbiter
  • space
)
private

Called when a collision begins in the system

Parameters:

  • arbiter cp.Arbiter

    The arbiter of the collision

  • space cp.Space

    The space the collision occurred in

onCollisionEnd

() private

Attempts to perform the postStep actions that have been queued. If the space is currently locked, then it waits until after the step to run the actions.

onCollisionEnd

(
  • arbiter
  • space
)
private

Called after a collision ends in the system (separation)

Parameters:

  • arbiter cp.Arbiter

    The arbiter of the collision

  • space cp.Space

    The space the collision occurred in

onCollisionPostSolve

(
  • arbiter
  • space
)
private

Called after a collision is solved in the system

Parameters:

  • arbiter cp.Arbiter

    The arbiter of the collision

  • space cp.Space

    The space the collision occurred in

onPostStep

() private

Processes the action queue after a step is unlocked.

pause

() PhysicsSystem chainable

Pauses physics simulation

Returns:

PhysicsSystem:

Returns itself.

reindex

(
  • spr
  • [callback]
)
PhysicsSystem chainable async

Reindexes a sprite's shape in the simulation, useful if it looks like changes are being cached.

Parameters:

  • spr Sprite

    The sprite to reindex

  • [callback] Function optional

    The callback to call once the sprite has been reindexed

Returns:

PhysicsSystem:

Returns itself.

reindexStatic

(
  • [callback]
)
PhysicsSystem chainable async

Reindexes all static bodies in the simulation.

Parameters:

  • [callback] Function optional

    The callback to call once reindexing completes

Returns:

PhysicsSystem:

Returns itself.

remove

(
  • spr
  • [callback]
)
Sprite async

Removes a sprite from the physics simulation

Parameters:

  • spr Sprite

    The sprite to remove

  • [callback] Function optional

    The callback to call once the sprite has been removed

Returns:

Sprite:

The sprite that was removed

resume

() PhysicsSystem chainable

Resumes physics simulation

Returns:

PhysicsSystem:

Returns itself.

setMass

(
  • spr
  • mass
)
PhysicsSystem chainable

Sets the mass of a sprite's physics body.

Parameters:

  • spr Sprite

    The sprite to set the mass for

  • mass Number

    The mass to set

Returns:

PhysicsSystem:

Returns itself.

setPosition

(
  • spr
  • position
)
PhysicsSystem chainable

Sets the position of a sprite's physics body.

Parameters:

  • spr Sprite

    The sprite to set the position for

  • position Vector

    The position to set to

Returns:

PhysicsSystem:

Returns itself.

setRotation

(
  • spr
  • rotation
)
PhysicsSystem chainable

Sets the rotation of a sprite's physics body.

Parameters:

  • spr Sprite

    The sprite to set the rotation for

  • rotation Number

    The rotation to set to in radians

Returns:

PhysicsSystem:

Returns itself.

setVelocity

(
  • spr
  • velocity
)
PhysicsSystem chainable

Sets the velocity of a sprite's physics body.

Parameters:

  • spr Sprite

    The sprite to set the velocity for

  • velocity Vector

    The velocity to set to

Returns:

PhysicsSystem:

Returns itself.

skip

(
  • num
)
PhysicsSystem chainable

Skips the specified number of frame steps

Parameters:

  • num Number

    Number of steps to skip

Returns:

PhysicsSystem:

Returns itself.

skipNext

() PhysicsSystem chainable

Skips the next frame step

Returns:

PhysicsSystem:

Returns itself.

update

(
  • dt
)
private

Called each frame by the game state to update the physics simulations

Parameters:

  • dt Number

    The number of seconds passed since the last call

Properties

_skip

Number private

The number of steps to skip, tracks this.skip(num)

actonQueue

Array private

The actions to perform at the next post-step callback. These are for actions (like add, remove) that cannot be performed during a simulation step.

gravity

Vector

The gravity of the physics space

space

cp.Space

The chipmunk space instance that will run all the physics simulations

state

State

The state instance this system belongs to

tickCallbacks

Array private

The callback functions to call on the next frame