Navigation
These archived docs are for Phaser 2.4.3 Phaser 3 docs can be found on newdocs.phaser.io.
Phaser CE docs can be found on the Phaser CE Documentation site.

Constructor

Phaser. Physics

new Physics(game, physicsConfig)

The Physics Manager is responsible for looking after all of the running physics systems. Phaser supports 4 physics systems: Arcade Physics, P2, Ninja Physics and Box2D via a commercial plugin.

Game Objects (such as Sprites) can only belong to 1 physics system, but you can have multiple systems active in a single game.

For example you could have P2 managing a polygon-built terrain landscape that an vehicle drives over, while it could be firing bullets that use the faster (due to being much simpler) Arcade Physics system.

Parameters
Name Type Argument Default Description
game Phaser.Game

A reference to the currently running game.

physicsConfig object <optional>
null

A physics configuration object to pass to the Physics world on creation.

Source code: physics/Physics.js (Line 21)

Classes

Arcade
Ninja
P2

Public Properties

[static] ARCADE : number

Source code: physics/Physics.js (Line 73)

[static] BOX2D : number

Source code: physics/Physics.js (Line 91)

[static] CHIPMUNK : number

Source code: physics/Physics.js (Line 97)

[static] MATTERJS : number

Source code: physics/Physics.js (Line 103)

[static] NINJA : number

Source code: physics/Physics.js (Line 85)

[static] P2JS : number

Source code: physics/Physics.js (Line 79)

arcade : Phaser.Physics.Arcade

The Arcade Physics system.

Source code: physics/Physics.js (Line 38)

box2d :Phaser.Physics.Box2D

The Box2D Physics system.

Source code: physics/Physics.js (Line 53)

chipmunk :Phaser.Physics.Chipmunk

The Chipmunk Physics system (to be done).

Source code: physics/Physics.js (Line 58)

config : Object

The physics configuration object as passed to the game on creation.

Source code: physics/Physics.js (Line 33)

game : Phaser.Game

Local reference to game.

Source code: physics/Physics.js (Line 28)

matter :Phaser.Physics.Matter

The MatterJS Physics system (coming soon).

Source code: physics/Physics.js (Line 63)

ninja : Phaser.Physics.Ninja

The N+ Ninja Physics system.

Source code: physics/Physics.js (Line 48)

p2 : Phaser.Physics.P2

The P2.JS Physics system.

Source code: physics/Physics.js (Line 43)

Public Methods

<internal> clear()

Clears down all active physics systems. This doesn't destroy them, it just clears them of objects and is called when the State changes.

Internal:
  • This member is internal (protected) and may be modified or removed in the future.
Source code: physics/Physics.js (Line 345)

destroy()

Destroys all active physics systems. Usually only called on a Game Shutdown, not on a State swap.

Source code: physics/Physics.js (Line 395)

enable(object, system, debug)

This will create a default physics body on the given game object or array of objects. A game object can only have 1 physics body active at any one time, and it can't be changed until the object is destroyed. It can be for any of the physics systems that have been started:

Phaser.Physics.Arcade - A light weight AABB based collision system with basic separation. Phaser.Physics.P2JS - A full-body advanced physics system supporting multiple object shapes, polygon loading, contact materials, springs and constraints. Phaser.Physics.NINJA - A port of Metanet Softwares N+ physics system. Advanced AABB and Circle vs. Tile collision. Phaser.Physics.BOX2D - A port of https://code.google.com/p/box2d-html5 Phaser.Physics.MATTER - A full-body and light-weight advanced physics system (still in development) Phaser.Physics.CHIPMUNK is still in development.

If you require more control over what type of body is created, for example to create a Ninja Physics Circle instead of the default AABB, then see the individual physics systems enable methods instead of using this generic one.

Parameters
Name Type Argument Default Description
object object | array

The game object to create the physics body on. Can also be an array of objects, a body will be created on every object in the array.

system number <optional>
Phaser.Physics.ARCADE

The physics system that will be used to create the body. Defaults to Arcade Physics.

debug boolean <optional>
false

Enable the debug drawing for this body. Defaults to false.

Source code: physics/Physics.js (Line 208)

parseConfig()

Parses the Physics Configuration object passed to the Game constructor and starts any physics systems specified within.

Source code: physics/Physics.js (Line 107)

<internal> preUpdate()

preUpdate checks.

Internal:
  • This member is internal (protected) and may be modified or removed in the future.
Source code: physics/Physics.js (Line 256)

<internal> reset()

Resets the active physics system. Called automatically on a Phaser.State swap.

Internal:
  • This member is internal (protected) and may be modified or removed in the future.
Source code: physics/Physics.js (Line 370)

setBoundsToWorld(left, right, top, bottom, setCollisionGroup)

Sets the bounds of the Physics world to match the Game.World dimensions. You can optionally set which 'walls' to create: left, right, top or bottom.

Parameters
Name Type Argument Default Description
left boolean <optional>
true

If true will create the left bounds wall.

right boolean <optional>
true

If true will create the right bounds wall.

top boolean <optional>
true

If true will create the top bounds wall.

bottom boolean <optional>
true

If true will create the bottom bounds wall.

setCollisionGroup boolean <optional>
true

If true the Bounds will be set to use its own Collision Group.

Source code: physics/p2/World.js (Line 552)

<internal> setBoundsToWorld()

Updates the physics bounds to match the world dimensions.

Internal:
  • This member is internal (protected) and may be modified or removed in the future.
Source code: physics/Physics.js (Line 310)

setWorldMaterial(material, left, right, top, bottom)

Sets the given material against the 4 bounds of this World.

Parameters
Name Type Argument Default Description
material Phaser.Physics.P2.Material

The material to set.

left boolean <optional>
true

If true will set the material on the left bounds wall.

right boolean <optional>
true

If true will set the material on the right bounds wall.

top boolean <optional>
true

If true will set the material on the top bounds wall.

bottom boolean <optional>
true

If true will set the material on the bottom bounds wall.

Source code: physics/p2/World.js (Line 569)

startSystem(system)

This will create an instance of the requested physics simulation. Phaser.Physics.Arcade is running by default, but all others need activating directly.

You can start the following physics systems:

Phaser.Physics.P2JS - A full-body advanced physics system by Stefan Hedman. Phaser.Physics.NINJA - A port of Metanet Softwares N+ physics system. Phaser.Physics.BOX2D - A commercial Phaser Plugin (see http://phaser.io)

Both Ninja Physics and Box2D require their respective plugins to be loaded before you can start them. They are not bundled into the core Phaser library.

If the physics world has already been created (i.e. in another state in your game) then calling startSystem will reset the physics world, not re-create it. If you need to start them again from their constructors then set Phaser.Physics.p2 (or whichever system you want to recreate) to null before calling startSystem.

Parameters
Name Type Description
system number

The physics system to start: Phaser.Physics.ARCADE, Phaser.Physics.P2JS, Phaser.Physics.NINJA or Phaser.Physics.BOX2D.

Source code: physics/Physics.js (Line 142)

<internal> update()

Updates all running physics systems.

Internal:
  • This member is internal (protected) and may be modified or removed in the future.
Source code: physics/Physics.js (Line 283)