Ninja Physics. The Ninja Physics system was created in Flash by Metanet Software and ported to JavaScript by Richard Davey.
It allows for AABB and Circle to Tile collision. Tiles can be any of 34 different types, including slopes, convex and concave shapes.
It does what it does very well, but is ripe for expansion and optimisation. Here are some features that I'd love to see the community add:
Feel free to attempt any of the above and submit a Pull Request with your code! Be sure to include test cases proving they work.
Name | Type | Description |
---|---|---|
game | Phaser.Game | reference to the current game instance. |
The bounds inside of which the physics world exists. Defaults to match the world bounds.
Local reference to game.
The World gravity setting.
Used by the QuadTree to set the maximum number of iteration levels.
Used by the QuadTree to set the maximum number of objects per quad.
The world QuadTree.
Local reference to game.time.
Clears all physics bodies from the given TilemapLayer that were created with World.convertTilemap
.
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. |
Checks for collision between two game objects. You can perform Sprite vs. Sprite, Sprite vs. Group, Group vs. Group, Sprite vs. Tilemap Layer or Group vs. Tilemap Layer collisions.
The second parameter can be an array of objects, of differing types.
The objects are also automatically separated. If you don't require separation then use ArcadePhysics.overlap instead.
An optional processCallback can be provided. If given this function will be called when two sprites are found to be colliding. It is called before any separation takes place,
giving you the chance to perform additional checks. If the function returns true then the collision and separation is carried out. If it returns false it is skipped.
The collideCallback is an optional function that is only called if two sprites collide. If a processCallback has been set then it needs to return true for collideCallback to be called.
Name | Type | Argument | Default | Description |
---|---|---|---|---|
object1 | Phaser.Sprite | Phaser.Group | Phaser.Particles.Emitter | Phaser.TilemapLayer | The first object to check. Can be an instance of Phaser.Sprite, Phaser.Group, Phaser.Particles.Emitter, or Phaser.TilemapLayer. | ||
object2 | Phaser.Sprite | Phaser.Group | Phaser.Particles.Emitter | Phaser.TilemapLayer | array | The second object or array of objects to check. Can be Phaser.Sprite, Phaser.Group, Phaser.Particles.Emitter or Phaser.TilemapLayer. | ||
collideCallback | function | <optional> | null | An optional callback function that is called if the objects collide. The two objects will be passed to this function in the same order in which you specified them. |
processCallback | function | <optional> | null | A callback function that lets you perform additional checks against the two objects if they overlap. If this is set then collision will only happen if processCallback returns true. The two objects will be passed to this function in the same order in which you specified them. |
callbackContext | object | <optional> | The context in which to run the callbacks. |
True if a collision occured otherwise false.
Goes through all tiles in the given Tilemap and TilemapLayer and converts those set to collide into physics tiles.
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.
In Ninja the Tiles have an ID from 0 to 33, where 0 is 'empty', 1 is a full tile, 2 is a 45-degree slope, etc. You can find the ID
list either at the very bottom of Tile.js
, or in a handy visual reference in the resources/Ninja Physics Debug Tiles
folder in the repository.
The slopeMap parameter is an array that controls how the indexes of the tiles in your tilemap data will map to the Ninja Tile IDs.
For example if you had 6 tiles in your tileset: Imagine the first 4 should be converted into fully solid Tiles and the other 2 are 45-degree slopes.
Your slopeMap array would look like this: [ 1, 1, 1, 1, 2, 3 ]
.
Where each element of the array is a tile in your tilemap and the resulting Ninja Tile it should create.
Name | Type | Description |
---|---|---|
map | Phaser.Tilemap | The Tilemap to get the map data from. |
layer | number | string | Phaser.TilemapLayer | The layer to operate on. If not given will default to map.currentLayer. |
slopeMap | object | The tilemap index to Tile ID map. |
An array of the Phaser.Physics.Ninja.Tile objects that were created.
This will create a Ninja 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.
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 | ||
type | number | <optional> | 1 | The type of Ninja shape to create. 1 = AABB, 2 = Circle or 3 = Tile. |
id | number | <optional> | 1 | If this body is using a Tile shape, you can set the Tile id here, i.e. Phaser.Physics.Ninja.Tile.SLOPE_45DEGpn, Phaser.Physics.Ninja.Tile.CONVEXpp, etc. |
radius | number | <optional> | 0 | If this body is using a Circle shape this controls the radius. |
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. |
This will create a Ninja Physics AABB body on the given game object. Its dimensions will match the width and height of the object at the point it is created.
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.
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 | ||
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. |
Creates a Ninja 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.
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 |
This will create a Ninja Physics Circle 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 object is destroyed.
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 | ||
radius | number | The radius of the Circle. | ||
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. |
This will create a Ninja Physics Tile body on the given game object. There are 34 different types of tile you can create, including 45 degree slopes,
convex and concave circles and more. The id parameter controls which Tile type is created, but you can also change it at run-time.
Note that for all degree based tile types they need to have an equal width and height. If the given object doesn't have equal width and height it will use the width.
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.
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 | ||
id | number | <optional> | 1 | The type of Tile this will use, i.e. Phaser.Physics.Ninja.Tile.SLOPE_45DEGpn, Phaser.Physics.Ninja.Tile.CONVEXpp, etc. |
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. |
Checks for overlaps between two game objects. The objects can be Sprites, Groups or Emitters.
You can perform Sprite vs. Sprite, Sprite vs. Group and Group vs. Group overlap checks.
Unlike collide the objects are NOT automatically separated or have any physics applied, they merely test for overlap results.
The second parameter can be an array of objects, of differing types.
Name | Type | Argument | Default | Description |
---|---|---|---|---|
object1 | Phaser.Sprite | Phaser.Group | Phaser.Particles.Emitter | The first object to check. Can be an instance of Phaser.Sprite, Phaser.Group or Phaser.Particles.Emitter. | ||
object2 | Phaser.Sprite | Phaser.Group | Phaser.Particles.Emitter | array | The second object or array of objects to check. Can be Phaser.Sprite, Phaser.Group or Phaser.Particles.Emitter. | ||
overlapCallback | function | <optional> | null | An optional callback function that is called if the objects overlap. The two objects will be passed to this function in the same order in which you specified them. |
processCallback | function | <optional> | null | A callback function that lets you perform additional checks against the two objects if they overlap. If this is set then overlapCallback will only be called if processCallback returns true. |
callbackContext | object | <optional> | The context in which to run the callbacks. |
True if an overlap occured otherwise false.
The core separation function to separate two physics bodies.
Name | Type | Description |
---|---|---|
body1 | Phaser.Physics.Ninja.Body | The Body object to separate. |
body2 | Phaser.Physics.Ninja.Body | The Body object to separate. |
Returns true if the bodies collided, otherwise false.
Updates the size of this physics world.
Name | Type | Description |
---|---|---|
x | number | Top left most corner of the world. |
y | number | Top left most corner of the world. |
width | number | New width of the world. Can never be smaller than the Game.width. |
height | number | New height of the world. Can never be smaller than the Game.height. |
Updates the size of this physics world to match the size of the game world.
© 2016 Richard Davey, Photon Storm Ltd.
Licensed under the MIT License.
http://phaser.io/docs/2.6.2/Phaser.Physics.Ninja.html