Class: Ninja

Phaser.Physics. Ninja

Ninja Physics Constructor

new Ninja(game)

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:

  • AABB to AABB collision
  • AABB to Circle collision
  • AABB and Circle 'immovable' property support
  • n-way collision, so an AABB/Circle could pass through a tile from below and land upon it.
  • QuadTree or spatial grid for faster Body vs. Tile Group look-ups.
  • Optimise the internal vector math and reduce the quantity of temporary vars created.
  • Expand Gravity and Bounce to allow for separate x/y axis values.
  • Support Bodies linked to Sprites that don't have anchor set to 0.5

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.

Parameters:
Name Type Description
game Phaser.Game

reference to the current game instance.

Source:

Classes

AABB
Body
Circle
Tile

Members

bounds

Properties:
Name Type Description
bounds Phaser.Rectangle

The bounds inside of which the physics world exists. Defaults to match the world bounds.

Source:

game

Properties:
Name Type Description
game Phaser.Game

Local reference to game.

Source:

gravity

Properties:
Name Type Description
gravity number

The World gravity setting.

Source:

maxLevels

Properties:
Name Type Description
maxLevels number

Used by the QuadTree to set the maximum number of iteration levels.

Source:

maxObjects

Properties:
Name Type Description
maxObjects number

Used by the QuadTree to set the maximum number of objects per quad.

Source:

quadTree

Properties:
Name Type Description
quadTree Phaser.QuadTree

The world QuadTree.

Source:

time

Properties:
Name Type Description
time Phaser.Time

Local reference to game.time.

Source:

Methods

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.

Source:

collide(object1, object2, collideCallback, processCallback, callbackContext) → {boolean}

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.

Parameters:
Name Type Argument Default Description
object1 Phaser.Sprite | Phaser.Group | Phaser.Particles.Emitter | Phaser.Tilemap

The first object to check. Can be an instance of Phaser.Sprite, Phaser.Group, Phaser.Particles.Emitter, or Phaser.Tilemap.

object2 Phaser.Sprite | Phaser.Group | Phaser.Particles.Emitter | Phaser.Tilemap | array

The second object or array of objects to check. Can be Phaser.Sprite, Phaser.Group, Phaser.Particles.Emitter or Phaser.Tilemap.

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.

Source:
Returns:

True if a collision occured otherwise false.

Type
boolean

convertTilemap(map, layer, slopeMap) → {array}

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.

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.

slopeMap object <optional>

The tilemap index to Tile ID map.

Source:
Returns:

An array of the Phaser.Physics.Ninja.Tile objects that were created.

Type
array

enable(object, type, id, radius, children)

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.

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 body property.

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.

Source:

enableAABB(object, children)

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.

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 body property.

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.

Source:

enableBody(object)

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.

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 body property.

Source:

enableCircle(object, radius, children)

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.

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 body property.

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.

Source:

enableTile(object, id, children)

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.

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 body property.

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.

Source:

overlap(object1, object2, overlapCallback, processCallback, callbackContext) → {boolean}

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.

Parameters:
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.

Source:
Returns:

True if an overlap occured otherwise false.

Type
boolean

separate(body1, body2) → {boolean}

The core separation function to separate two physics bodies.

Parameters:
Name Type Description
body1 Phaser.Physics.Ninja.Body

The Body object to separate.

body2 Phaser.Physics.Ninja.Body

The Body object to separate.

Source:
Returns:

Returns true if the bodies collided, otherwise false.

Type
boolean

setBounds(x, y, width, height)

Updates the size of this physics world.

Parameters:
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.

Source:

setBoundsToWorld()

Updates the size of this physics world to match the size of the game world.

Source:
Phaser Copyright © 2012-2014 Photon Storm Ltd.
Documentation generated by JSDoc 3.3.0-dev on Tue May 20 2014 10:06:01 GMT+0100 (BST) using the DocStrap template.