Physics.P2
Class: Phaser.Physics.P2
Constructor
new P2(game, config)
This is your main access to the P2 Physics World.
From here you can create materials, listen for events and add bodies into the physics simulation.
Parameters
Name | Type | Argument | Description |
---|---|---|---|
game | Phaser.Game | Reference to the current game instance. | |
config | object | <optional> | Physics configuration object passed in from the game constructor. |
- Source code: physics/p2/World.js (Line 20)
Classes
- Body
- BodyDebug
- CollisionGroup
- ContactMaterial
- DistanceConstraint
- FixtureList
- GearConstraint
- InversePointProxy
- LockConstraint
- Material
- PointProxy
- PrismaticConstraint
- RevoluteConstraint
- RotationalSpring
- Spring
Public Properties
- Source code: physics/p2/World.js (Line 1919)
- Source code: physics/p2/World.js (Line 1939)
- Source code: physics/p2/World.js (Line 1899)
- Source code: physics/p2/World.js (Line 232)
- Source code: physics/p2/World.js (Line 222)
- Source code: physics/p2/World.js (Line 170)
- Source code: physics/p2/World.js (Line 212)
- Internal:
- This member is internal (protected) and may be modified or removed in the future.
- Source code: physics/p2/World.js (Line 48)
- Source code: physics/p2/World.js (Line 1879)
- Source code: physics/p2/World.js (Line 1994)
- Source code: physics/p2/World.js (Line 227)
- Source code: physics/p2/World.js (Line 60)
- Source code: physics/p2/World.js (Line 1839)
- Source code: physics/p2/World.js (Line 25)
- Source code: physics/p2/World.js (Line 83)
- array.<Phaser.Physics.P2.Material>
- Internal:
- This member is internal (protected) and may be modified or removed in the future.
- Source code: physics/p2/World.js (Line 78)
- Source code: physics/p2/World.js (Line 217)
- Source code: physics/p2/World.js (Line 182)
- Source code: physics/p2/World.js (Line 97)
- Source code: physics/p2/World.js (Line 106)
- Source code: physics/p2/World.js (Line 133)
- Source code: physics/p2/World.js (Line 142)
- Source code: physics/p2/World.js (Line 151)
- Source code: physics/p2/World.js (Line 160)
- Source code: physics/p2/World.js (Line 194)
- Source code: physics/p2/World.js (Line 115)
- Source code: physics/p2/World.js (Line 124)
- Source code: physics/p2/World.js (Line 72)
- Source code: physics/p2/World.js (Line 165)
- Source code: physics/p2/World.js (Line 1859)
- Source code: physics/p2/World.js (Line 2014)
- Source code: physics/p2/World.js (Line 1959)
- Source code: physics/p2/World.js (Line 1979)
- Source code: physics/p2/World.js (Line 2036)
- Source code: physics/p2/World.js (Line 66)
- Source code: physics/p2/World.js (Line 88)
- Internal:
- This member is internal (protected) and may be modified or removed in the future.
- Source code: physics/p2/World.js (Line 54)
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.
boundsCollidesWith :array
An array of the bodies the world bounds collides with.
boundsCollisionGroup : Phaser.Physics.P2.CollisionGroup
A default collision group.
callbackContext : Object
The context under which the callbacks are fired.
collisionGroups :array
An array containing the collision groups that have been defined in the World.
<internal> config : Object
The p2 World configuration object.
contactMaterial :p2.ContactMaterial
The default Contact Material being used by the World.
emitImpactEvent : boolean
Set to true if you want to the world to emit the "impact" event. Turning this off could improve performance.
everythingCollisionGroup : Phaser.Physics.P2.CollisionGroup
A default collision group.
frameRate : number
The frame rate the world will be stepped at. Defaults to 1 / 60, but you can change here. Also see useElapsedTime property.
friction : number
Friction between colliding bodies. This value is used if no matching ContactMaterial is found for a Material pair.
game : Phaser.Game
Local reference to game.
gravity : Phaser.Physics.P2.InversePointProxy
The gravity applied to all bodies each step.
<internal> materials :array.<Phaser.Physics.P2.Material>
A local array of all created Materials.
Type
nothingCollisionGroup : Phaser.Physics.P2.CollisionGroup
A default collision group.
onBeginContact : Phaser.Signal
This Signal is dispatched when a first contact is created between two bodies. This happens before the step has been done.
It sends 5 arguments: bodyA
, bodyB
, shapeA
, shapeB
and contactEquations
.
It is possible that in certain situations the bodyA
or bodyB
values are null
. You should check for this
in your own code to avoid processing potentially null physics bodies.
onBodyAdded : Phaser.Signal
This signal is dispatched when a new Body is added to the World.
It sends 1 argument: body
which is the Phaser.Physics.P2.Body
that was added to the world.
onBodyRemoved : Phaser.Signal
This signal is dispatched when a Body is removed to the World.
It sends 1 argument: body
which is the Phaser.Physics.P2.Body
that was removed from the world.
onConstraintAdded : Phaser.Signal
This signal is dispatched when a Constraint is added to the World.
It sends 1 argument: constraint
which is the Phaser.Physics.P2.Constraint
that was added to the world.
onConstraintRemoved : Phaser.Signal
This signal is dispatched when a Constraint is removed from the World.
It sends 1 argument: constraint
which is the Phaser.Physics.P2.Constraint
that was removed from the world.
onContactMaterialAdded : Phaser.Signal
This signal is dispatched when a Contact Material is added to the World.
It sends 1 argument: material
which is the Phaser.Physics.P2.ContactMaterial
that was added to the world.
onContactMaterialRemoved : Phaser.Signal
This signal is dispatched when a Contact Material is removed from the World.
It sends 1 argument: material
which is the Phaser.Physics.P2.ContactMaterial
that was removed from the world.
onEndContact : Phaser.Signal
This Signal is dispatched when final contact occurs between two bodies. This happens before the step has been done.
It sends 4 arguments: bodyA
, bodyB
, shapeA
and shapeB
.
It is possible that in certain situations the bodyA
or bodyB
values are null
. You should check for this
in your own code to avoid processing potentially null physics bodies.
onSpringAdded : Phaser.Signal
This signal is dispatched when a Spring is added to the World.
It sends 1 argument: spring
which is either a Phaser.Physics.P2.Spring
, p2.LinearSpring
or p2.RotationalSpring
that was added to the world.
onSpringRemoved : Phaser.Signal
This signal is dispatched when a Spring is removed from the World.
It sends 1 argument: spring
which is either a Phaser.Physics.P2.Spring
, p2.LinearSpring
or p2.RotationalSpring
that was removed from the world.
paused : boolean
The paused state of the P2 World.
postBroadphaseCallback : Function
A postBroadphase callback.
restitution : number
Default coefficient of restitution between colliding bodies. This value is used if no matching ContactMaterial is found for a Material pair.
sleepMode : number
How to deactivate bodies during simulation. Possible modes are: World.NO_SLEEPING, World.BODY_SLEEPING and World.ISLAND_SLEEPING.
If sleeping is enabled, 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.
solveConstraints : boolean
Enable/disable constraint solving in each step.
[readonly] time : boolean
The World time.
[readonly] total : number
The total number of bodies in the world.
useElapsedTime : boolean
If true the frameRate value will be ignored and instead p2 will step with the value of Game.Time.physicsElapsed, which is a delta time value.
walls : Object
An object containing the 4 wall bodies that bound the physics world.
<internal> world :p2.World
The p2 World in which the simulation is run.
Public Methods
- Source code: physics/p2/World.js (Line 887)
- Source code: physics/p2/World.js (Line 1119)
- Source code: physics/p2/World.js (Line 1153)
- Source code: physics/p2/World.js (Line 931)
- Source code: physics/p2/World.js (Line 502)
- Removes all solver equations
- Removes all constraints
- Removes all bodies
- Removes all springs
- Removes all contact materials
- Source code: physics/p2/World.js (Line 800)
- Source code: physics/p2/World.js (Line 1666)
- Source code: physics/p2/World.js (Line 1625)
- Source code: physics/p2/World.js (Line 1688)
- Source code: physics/p2/World.js (Line 1542)
- Source code: physics/p2/World.js (Line 1411)
- Source code: physics/p2/World.js (Line 1246)
- Source code: physics/p2/World.js (Line 979)
- Source code: physics/p2/World.js (Line 1007)
- Source code: physics/p2/World.js (Line 1062)
- Source code: physics/p2/World.js (Line 1219)
- Source code: physics/p2/World.js (Line 1584)
- Source code: physics/p2/World.js (Line 1089)
- Source code: physics/p2/World.js (Line 1033)
- Source code: physics/p2/World.js (Line 1515)
- Source code: physics/p2/World.js (Line 1484)
- Source code: physics/p2/World.js (Line 874)
- Source code: physics/p2/World.js (Line 313)
- Source code: physics/p2/World.js (Line 372)
- Source code: physics/p2/World.js (Line 527)
- Source code: physics/p2/World.js (Line 1266)
- Source code: physics/p2/World.js (Line 1286)
- Source code: physics/p2/World.js (Line 1335)
- Source code: physics/p2/World.js (Line 1187)
- Source code: physics/p2/World.js (Line 1315)
- Source code: physics/p2/World.js (Line 1357)
- Source code: physics/p2/World.js (Line 1777)
- Source code: physics/p2/World.js (Line 1807)
- Source code: physics/p2/World.js (Line 733)
- Source code: physics/p2/World.js (Line 295)
- Source code: physics/p2/World.js (Line 1792)
- Source code: physics/p2/World.js (Line 1822)
- Source code: physics/p2/World.js (Line 911)
- Source code: physics/p2/World.js (Line 283)
- Source code: physics/p2/World.js (Line 1136)
- Source code: physics/p2/World.js (Line 1170)
- Source code: physics/p2/World.js (Line 955)
- Source code: physics/p2/World.js (Line 779)
- Source code: physics/p2/World.js (Line 744)
- Source code: physics/p2/World.js (Line 646)
- Source code: physics/p2/World.js (Line 393)
- Source code: physics/p2/World.js (Line 1201)
- Source code: physics/p2/World.js (Line 413)
- Source code: physics/p2/World.js (Line 1399)
- Source code: physics/p2/World.js (Line 755)
- Source code: physics/p2/World.js (Line 608)
addBody(body) → {boolean}
Add a body to the world.
Parameters
Name | Type | Description |
---|---|---|
body | Phaser.Physics.P2.Body | The Body to add to the World. |
Returns
True if the Body was added successfully, otherwise false.
addConstraint(constraint) → {Phaser.Physics.P2.Constraint}
Adds a Constraint to the world.
Parameters
Name | Type | Description |
---|---|---|
constraint | Phaser.Physics.P2.Constraint | The Constraint to add to the World. |
Returns
The Constraint that was added.
addContactMaterial(material) → {Phaser.Physics.P2.ContactMaterial}
Adds a Contact Material to the world.
Parameters
Name | Type | Description |
---|---|---|
material | Phaser.Physics.P2.ContactMaterial | The Contact Material to be added to the World. |
Returns
The Contact Material that was added.
addSpring(spring) → {Phaser.Physics.P2.Spring}
Adds a Spring to the world.
Parameters
Name | Type | Description |
---|---|---|
spring | Phaser.Physics.P2.Spring | p2.LinearSpring | p2.RotationalSpring | The Spring to add to the World. |
Returns
The Spring that was added.
beginContactHandler(event)
Handles a p2 begin contact event.
Parameters
Name | Type | Description |
---|---|---|
event | object | The event data. |
clear()
Clears all bodies from the simulation, resets callbacks and resets the collision bitmask.
The P2 world is also cleared:
This is called automatically when you switch state.
clearTilemapLayerBodies(map, layer)
Clears all physics bodies from the given TilemapLayer that were created with World.convertTilemap
.
Parameters
Name | Type | Argument | Description |
---|---|---|---|
map | Phaser.Tilemap | The Tilemap to get the map data from. | |
layer | number | string | Phaser.TilemapLayer | <optional> | The layer to operate on. If not given will default to map.currentLayer. |
convertCollisionObjects(map, layer, addToWorld) → {array}
Converts all of the polylines objects inside a Tiled ObjectGroup into physics bodies that are added to the world.
Note that the polylines must be created in such a way that they can withstand polygon decomposition.
Parameters
Name | Type | Argument | Default | Description |
---|---|---|---|---|
map | Phaser.Tilemap | The Tilemap to get the map data from. | ||
layer | number | string | Phaser.TilemapLayer | <optional> | The layer to operate on. If not given will default to map.currentLayer. | |
addToWorld | boolean | <optional> | true | If true it will automatically add each body to the world. |
Returns
An array of the Phaser.Physics.Body objects that have been created.
convertTilemap(map, layer, addToWorld, optimize) → {array}
Goes through all tiles in the given Tilemap and TilemapLayer and converts those set to collide into physics bodies.
Only call this after you have specified all of the tiles you wish to collide with calls like Tilemap.setCollisionBetween, etc.
Every time you call this method it will destroy any previously created bodies and remove them from the world.
Therefore understand it's a very expensive operation and not to be done in a core game update loop.
Parameters
Name | Type | Argument | Default | Description |
---|---|---|---|---|
map | Phaser.Tilemap | The Tilemap to get the map data from. | ||
layer | number | string | Phaser.TilemapLayer | <optional> | The layer to operate on. If not given will default to map.currentLayer. | |
addToWorld | boolean | <optional> | true | If true it will automatically add each body to the world, otherwise it's up to you to do so. |
optimize | boolean | <optional> | true | If true adjacent colliding tiles will be combined into a single body to save processing. However it means you cannot perform specific Tile to Body collision responses. |
Returns
An array of the Phaser.Physics.P2.Body objects that were created.
createBody(x, y, mass, addToWorld, options, points) → {Phaser.Physics.P2.Body}
Creates a new Body and adds it to the World.
Parameters
Name | Type | Argument | Default | Description | ||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
x | number | The x coordinate of Body. | ||||||||||||||||||||||
y | number | The y coordinate of Body. | ||||||||||||||||||||||
mass | number | The mass of the Body. A mass of 0 means a 'static' Body is created. | ||||||||||||||||||||||
addToWorld | boolean | <optional> | false | Automatically add this Body to the world? (usually false as it won't have any shapes on construction). | ||||||||||||||||||||
options | object |
An object containing the build options: Properties
| ||||||||||||||||||||||
points | Array.<number> | number | An array of 2d vectors that form the convex or concave polygon. |
Returns
The body
createCollisionGroup(object)
Creates a new Collision Group and optionally applies it to the given object.
Collision Groups are handled using bitmasks, therefore you have a fixed limit you can create before you need to re-use older groups.
Parameters
Name | Type | Argument | Description |
---|---|---|---|
object | Phaser.Group | Phaser.Sprite | <optional> | An optional Sprite or Group to apply the Collision Group to. If a Group is given it will be applied to all top-level children. |
createContactMaterial(materialA, materialB, options) → {Phaser.Physics.P2.ContactMaterial}
Creates a Contact Material from the two given Materials. You can then edit the properties of the Contact Material directly.
Parameters
Name | Type | Argument | Description |
---|---|---|---|
materialA | Phaser.Physics.P2.Material | <optional> | The first Material to create the ContactMaterial from. If undefined it will create a new Material object first. |
materialB | Phaser.Physics.P2.Material | <optional> | The second Material to create the ContactMaterial from. If undefined it will create a new Material object first. |
options | object | <optional> | Material options object. |
Returns
The Contact Material that was created.
createDistanceConstraint(bodyA, bodyB, distance, localAnchorA, localAnchorB, maxForce) → {Phaser.Physics.P2.DistanceConstraint}
Creates a constraint that tries to keep the distance between two bodies constant.
Parameters
Name | Type | Argument | Description |
---|---|---|---|
bodyA | Phaser.Sprite | Phaser.Physics.P2.Body | p2.Body | First connected body. | |
bodyB | Phaser.Sprite | Phaser.Physics.P2.Body | p2.Body | Second connected body. | |
distance | number | The distance to keep between the bodies. | |
localAnchorA | Array | <optional> | The anchor point for bodyA, defined locally in bodyA frame. Defaults to [0,0]. |
localAnchorB | Array | <optional> | The anchor point for bodyB, defined locally in bodyB frame. Defaults to [0,0]. |
maxForce | number | <optional> | The maximum force that should be applied to constrain the bodies. |
Returns
The constraint
createGearConstraint(bodyA, bodyB, angle, ratio) → {Phaser.Physics.P2.GearConstraint}
Creates a constraint that tries to keep the distance between two bodies constant.
Parameters
Name | Type | Argument | Default | Description |
---|---|---|---|---|
bodyA | Phaser.Sprite | Phaser.Physics.P2.Body | p2.Body | First connected body. | ||
bodyB | Phaser.Sprite | Phaser.Physics.P2.Body | p2.Body | Second connected body. | ||
angle | number | <optional> | 0 | The relative angle |
ratio | number | <optional> | 1 | The gear ratio. |
Returns
The constraint
createLockConstraint(bodyA, bodyB, offset, angle, maxForce) → {Phaser.Physics.P2.LockConstraint}
Locks the relative position between two bodies.
Parameters
Name | Type | Argument | Default | Description |
---|---|---|---|---|
bodyA | Phaser.Sprite | Phaser.Physics.P2.Body | p2.Body | First connected body. | ||
bodyB | Phaser.Sprite | Phaser.Physics.P2.Body | p2.Body | Second connected body. | ||
offset | Array | <optional> | The offset of bodyB in bodyA's frame. The value is an array with 2 elements matching x and y, i.e: [32, 32]. | |
angle | number | <optional> | 0 | The angle of bodyB in bodyA's frame. |
maxForce | number | <optional> | The maximum force that should be applied to constrain the bodies. |
Returns
The constraint
createMaterial(name, body) → {Phaser.Physics.P2.Material}
Creates a Material. Materials are applied to Shapes owned by a Body and can be set with Body.setMaterial().
Materials are a way to control what happens when Shapes collide. Combine unique Materials together to create Contact Materials.
Contact Materials have properties such as friction and restitution that allow for fine-grained collision control between different Materials.
Parameters
Name | Type | Argument | Description |
---|---|---|---|
name | string | <optional> | Optional name of the Material. Each Material has a unique ID but string names are handy for debugging. |
body | Phaser.Physics.P2.Body | <optional> | Optional Body. If given it will assign the newly created Material to the Body shapes. |
Returns
The Material that was created. This is also stored in Phaser.Physics.P2.materials.
createParticle(x, y, mass, addToWorld, options, points)
Creates a new Particle and adds it to the World.
Parameters
Name | Type | Argument | Default | Description | ||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
x | number | The x coordinate of Body. | ||||||||||||||||||||||
y | number | The y coordinate of Body. | ||||||||||||||||||||||
mass | number | The mass of the Body. A mass of 0 means a 'static' Body is created. | ||||||||||||||||||||||
addToWorld | boolean | <optional> | false | Automatically add this Body to the world? (usually false as it won't have any shapes on construction). | ||||||||||||||||||||
options | object |
An object containing the build options: Properties
| ||||||||||||||||||||||
points | Array.<number> | number | An array of 2d vectors that form the convex or concave polygon. |
createPrismaticConstraint(bodyA, bodyB, lockRotation, anchorA, anchorB, axis, maxForce) → {Phaser.Physics.P2.PrismaticConstraint}
Constraint that only allows bodies to move along a line, relative to each other.
See http://www.iforce2d.net/b2dtut/joints-prismatic
Parameters
Name | Type | Argument | Default | Description |
---|---|---|---|---|
bodyA | Phaser.Sprite | Phaser.Physics.P2.Body | p2.Body | First connected body. | ||
bodyB | Phaser.Sprite | Phaser.Physics.P2.Body | p2.Body | Second connected body. | ||
lockRotation | boolean | <optional> | true | If set to false, bodyB will be free to rotate around its anchor point. |
anchorA | Array | <optional> | Body A's anchor point, defined in its own local frame. The value is an array with 2 elements matching x and y, i.e: [32, 32]. | |
anchorB | Array | <optional> | Body A's anchor point, defined in its own local frame. The value is an array with 2 elements matching x and y, i.e: [32, 32]. | |
axis | Array | <optional> | An axis, defined in body A frame, that body B's anchor point may slide along. The value is an array with 2 elements matching x and y, i.e: [32, 32]. | |
maxForce | number | <optional> | The maximum force that should be applied to constrain the bodies. |
Returns
The constraint
createRevoluteConstraint(bodyA, pivotA, bodyB, pivotB, maxForce, worldPivot) → {Phaser.Physics.P2.RevoluteConstraint}
Connects two bodies at given offset points, letting them rotate relative to each other around this point.
The pivot points are given in world (pixel) coordinates.
Parameters
Name | Type | Argument | Default | Description |
---|---|---|---|---|
bodyA | Phaser.Sprite | Phaser.Physics.P2.Body | p2.Body | First connected body. | ||
pivotA | Array | The point relative to the center of mass of bodyA which bodyA is constrained to. The value is an array with 2 elements matching x and y, i.e: [32, 32]. | ||
bodyB | Phaser.Sprite | Phaser.Physics.P2.Body | p2.Body | Second connected body. | ||
pivotB | Array | The point relative to the center of mass of bodyB which bodyB is constrained to. The value is an array with 2 elements matching x and y, i.e: [32, 32]. | ||
maxForce | number | <optional> | 0 | The maximum force that should be applied to constrain the bodies. |
worldPivot | Float32Array | <optional> | null | A pivot point given in world coordinates. If specified, localPivotA and localPivotB are automatically computed from this value. |
Returns
The constraint
createRotationalSpring(bodyA, bodyB, restAngle, stiffness, damping) → {Phaser.Physics.P2.RotationalSpring}
Creates a rotational spring, connecting two bodies. A spring can have a resting length, a stiffness and damping.
Parameters
Name | Type | Argument | Default | Description |
---|---|---|---|---|
bodyA | Phaser.Sprite | Phaser.Physics.P2.Body | p2.Body | First connected body. | ||
bodyB | Phaser.Sprite | Phaser.Physics.P2.Body | p2.Body | Second connected body. | ||
restAngle | number | <optional> | The relative angle of bodies at which the spring is at rest. If not given, it's set to the current relative angle between the bodies. | |
stiffness | number | <optional> | 100 | Stiffness of the spring. A number >= 0. |
damping | number | <optional> | 1 | Damping of the spring. A number >= 0. |
Returns
The spring
createSpring(bodyA, bodyB, restLength, stiffness, damping, worldA, worldB, localA, localB) → {Phaser.Physics.P2.Spring}
Creates a linear spring, connecting two bodies. A spring can have a resting length, a stiffness and damping.
Parameters
Name | Type | Argument | Default | Description |
---|---|---|---|---|
bodyA | Phaser.Sprite | Phaser.Physics.P2.Body | p2.Body | First connected body. | ||
bodyB | Phaser.Sprite | Phaser.Physics.P2.Body | p2.Body | Second connected body. | ||
restLength | number | <optional> | 1 | Rest length of the spring. A number > 0. |
stiffness | number | <optional> | 100 | Stiffness of the spring. A number >= 0. |
damping | number | <optional> | 1 | Damping of the spring. A number >= 0. |
worldA | Array | <optional> | Where to hook the spring to body A in world coordinates. This value is an array by 2 elements, x and y, i.e: [32, 32]. | |
worldB | Array | <optional> | Where to hook the spring to body B in world coordinates. This value is an array by 2 elements, x and y, i.e: [32, 32]. | |
localA | Array | <optional> | Where to hook the spring to body A in local body coordinates. This value is an array by 2 elements, x and y, i.e: [32, 32]. | |
localB | Array | <optional> | Where to hook the spring to body B in local body coordinates. This value is an array by 2 elements, x and y, i.e: [32, 32]. |
Returns
The spring
destroy()
Clears all bodies from the simulation and unlinks World from Game. Should only be called on game shutdown. Call clear
on a State change.
enable(object, debug, children)
This will create a P2 Physics body on the given game object or array of game 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.
Note: When the game object is enabled for P2 physics it has its anchor x/y set to 0.5 so it becomes centered.
Parameters
Name | Type | Argument | Default | Description |
---|---|---|---|---|
object | object | array | Phaser.Group | The game object to create the physics body on. Can also be an array or Group of objects, a body will be created on every child that has a | ||
debug | boolean | <optional> | false | Create a debug object to go with this body? |
children | boolean | <optional> | true | Should a body be created on all children of this object? If true it will recurse down the display list as far as it can go. |
enableBody(object, debug)
Creates a P2 Physics body on the given game object.
A game object can only have 1 physics body active at any one time, and it can't be changed until the body is nulled.
Parameters
Name | Type | Description |
---|---|---|
object | object | The game object to create the physics body on. A body will only be created if this object has a null |
debug | boolean | Create a debug object to go with this body? |
endContactHandler(event)
Handles a p2 end contact event.
Parameters
Name | Type | Description |
---|---|---|
event | object | The event data. |
getBodies() → {array.<Phaser.Physics.P2.Body>}
Populates and returns an array with references to of all current Bodies in the world.
Returns
An array containing all current Bodies in the world.
getBody(object) → {p2.Body}
Checks the given object to see if it has a p2.Body and if so returns it.
Parameters
Name | Type | Description |
---|---|---|
object | object | The object to check for a p2.Body on. |
Returns
The p2.Body, or null if not found.
getConstraints() → {array.<Phaser.Physics.P2.Constraint>}
Populates and returns an array of all current Constraints in the world.
You will get an array of p2 constraints back. This can be of mixed types, for example the array may contain
PrismaticConstraints, RevoluteConstraints or any other valid p2 constraint type.
Returns
An array containing all current Constraints in the world.
getContactMaterial(materialA, materialB) → {Phaser.Physics.P2.ContactMaterial | boolean}
Gets a Contact Material based on the two given Materials.
Parameters
Name | Type | Description |
---|---|---|
materialA | Phaser.Physics.P2.Material | The first Material to search for. |
materialB | Phaser.Physics.P2.Material | The second Material to search for. |
Returns
The Contact Material or false if none was found matching the Materials given.
getSprings() → {array.<Phaser.Physics.P2.Spring>}
Populates and returns an array of all current Springs in the world.
Returns
An array containing all current Springs in the world.
hitTest(worldPoint, bodies, precision, filterStatic) → {Array}
Test if a world point overlaps bodies. You will get an array of actual P2 bodies back. You can find out which Sprite a Body belongs to
(if any) by checking the Body.parent.sprite property. Body.parent is a Phaser.Physics.P2.Body property.
Parameters
Name | Type | Argument | Default | Description |
---|---|---|---|---|
worldPoint | Phaser.Point | Point to use for intersection tests. The points values must be in world (pixel) coordinates. | ||
bodies | Array.<(Phaser.Physics.P2.Body | Phaser.Sprite | p2.Body)> | <optional> | A list of objects to check for intersection. If not given it will check Phaser.Physics.P2.world.bodies (i.e. all world bodies) | |
precision | number | <optional> | 5 | Used for matching against particles and lines. Adds some margin to these infinitesimal objects. |
filterStatic | boolean | <optional> | false | If true all Static objects will be removed from the results array. |
Returns
Array of bodies that overlap the point.
mpx(v) → {number}
Convert p2 physics value (meters) to pixel scale.
By default Phaser uses a scale of 20px per meter.
If you need to modify this you can over-ride these functions via the Physics Configuration object.
Parameters
Name | Type | Description |
---|---|---|
v | number | The value to convert. |
Returns
The scaled value.
mpxi(v) → {number}
Convert p2 physics value (meters) to pixel scale and inverses it.
By default Phaser uses a scale of 20px per meter.
If you need to modify this you can over-ride these functions via the Physics Configuration object.
Parameters
Name | Type | Description |
---|---|---|
v | number | The value to convert. |
Returns
The scaled value.
pause()
Pauses the P2 World independent of the game pause state.
preUpdate()
Called at the start of the core update loop. Purges flagged bodies from the world.
pxm(v) → {number}
Convert pixel value to p2 physics scale (meters).
By default Phaser uses a scale of 20px per meter.
If you need to modify this you can over-ride these functions via the Physics Configuration object.
Parameters
Name | Type | Description |
---|---|---|
v | number | The value to convert. |
Returns
The scaled value.
pxmi(v) → {number}
Convert pixel value to p2 physics scale (meters) and inverses it.
By default Phaser uses a scale of 20px per meter.
If you need to modify this you can over-ride these functions via the Physics Configuration object.
Parameters
Name | Type | Description |
---|---|---|
v | number | The value to convert. |
Returns
The scaled value.
removeBody(body) → {Phaser.Physics.P2.Body}
Removes a body from the world. This will silently fail if the body wasn't part of the world to begin with.
Parameters
Name | Type | Description |
---|---|---|
body | Phaser.Physics.P2.Body | The Body to remove from the World. |
Returns
The Body that was removed.
removeBodyNextStep(body)
This will add a P2 Physics body into the removal list for the next step.
Parameters
Name | Type | Description |
---|---|---|
body | Phaser.Physics.P2.Body | The body to remove at the start of the next step. |
removeConstraint(constraint) → {Phaser.Physics.P2.Constraint}
Removes a Constraint from the world.
Parameters
Name | Type | Description |
---|---|---|
constraint | Phaser.Physics.P2.Constraint | The Constraint to be removed from the World. |
Returns
The Constraint that was removed.
removeContactMaterial(material) → {Phaser.Physics.P2.ContactMaterial}
Removes a Contact Material from the world.
Parameters
Name | Type | Description |
---|---|---|
material | Phaser.Physics.P2.ContactMaterial | The Contact Material to be removed from the World. |
Returns
The Contact Material that was removed.
removeSpring(spring) → {Phaser.Physics.P2.Spring}
Removes a Spring from the world.
Parameters
Name | Type | Description |
---|---|---|
spring | Phaser.Physics.P2.Spring | The Spring to remove from the World. |
Returns
The Spring that was removed.
reset()
Called by Phaser.Physics when a State swap occurs.
Starts the begin and end Contact listeners again.
resume()
Resumes a paused P2 World.
setBounds(x, y, width, height, left, right, top, bottom, setCollisionGroup)
Sets the bounds of the Physics world to match the given world pixel dimensions.
You can optionally set which 'walls' to create: left, right, top or bottom.
If none of the walls are given it will default to use the walls settings it had previously.
I.e. if you previously told it to not have the left or right walls, and you then adjust the world size
the newly created bounds will also not have the left and right walls.
Explicitly state them in the parameters to override this.
Parameters
Name | Type | Argument | Default | Description |
---|---|---|---|---|
x | number | The x coordinate of the top-left corner of the bounds. | ||
y | number | The y coordinate of the top-left corner of the bounds. | ||
width | number | The width of the bounds. | ||
height | number | The height of the bounds. | ||
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. |
setImpactEvents(state)
Impact event handling is disabled by default. Enable it before any impact events will be dispatched.
In a busy world hundreds of impact events can be generated every step, so only enable this if you cannot do what you need via beginContact or collision masks.
Parameters
Name | Type | Description |
---|---|---|
state | boolean | Set to true to enable impact events, or false to disable. |
setMaterial(material, bodies)
Sets the given Material against all Shapes owned by all the Bodies in the given array.
Parameters
Name | Type | Description |
---|---|---|
material | Phaser.Physics.P2.Material | The Material to be applied to the given Bodies. |
bodies | array.<Phaser.Physics.P2.Body> | An Array of Body objects that the given Material will be set on. |
setPostBroadphaseCallback(callback, context)
Sets a callback to be fired after the Broadphase has collected collision pairs in the world.
Just because a pair exists it doesn't mean they will collide, just that they potentially could do.
If your calback returns false
the pair will be removed from the narrowphase. This will stop them testing for collision this step.
Returning true
from the callback will ensure they are checked in the narrowphase.
Parameters
Name | Type | Description |
---|---|---|
callback | function | The callback that will receive the postBroadphase event data. It must return a boolean. Set to null to disable an existing callback. |
context | object | The context under which the callback will be fired. |
toJSON() → {object}
Converts the current world into a JSON object.
Returns
A JSON representation of the world.
update()
Internal P2 update loop.
updateBoundsCollisionGroup(setCollisionGroup)
By default the World will be set to collide everything with everything. The bounds of the world is a Body with 4 shapes, one for each face.
If you start to use your own collision groups then your objects will no longer collide with the bounds.
To fix this you need to adjust the bounds to use its own collision group first BEFORE changing your Sprites collision group.
Parameters
Name | Type | Argument | Default | Description |
---|---|---|---|---|
setCollisionGroup | boolean | <optional> | true | If true the Bounds will be set to use its own Collision Group. |
© 2016 Richard Davey, Photon Storm Ltd.
Licensed under the MIT License.
http://phaser.io/docs/2.6.2/Phaser.Physics.P2.html