Class: QuadTree

Phaser. QuadTree

A QuadTree implementation. The original code was a conversion of the Java code posted to GameDevTuts. However I've tweaked it massively to add node indexing, removed lots of temp. var creation and significantly increased performance as a result. Original version at https://github.com/timohausmann/quadtree-js/

new QuadTree(x, y, width, height, maxObjects, maxLevels, level)

QuadTree Constructor

Parameters:
Name Type Argument Default Description
x number

The top left coordinate of the quadtree.

y number

The top left coordinate of the quadtree.

width number

The width of the quadtree in pixels.

height number

The height of the quadtree in pixels.

maxObjects number <optional>
10

The maximum number of objects per node.

maxLevels number <optional>
4

The maximum number of levels to iterate to.

level number <optional>
0

Which level is this?

Source:

Members

bounds

Properties:
Name Type Description
bounds object

Object that contains the quadtree bounds.

Source:

level

Properties:
Name Type Description
level number

The current level.

Source:

maxLevels

Properties:
Name Type Description
maxLevels number

The maximum number of levels to break down to.

Default Value:
  • 4
Source:

maxObjects

Properties:
Name Type Description
maxObjects number

The maximum number of objects per node.

Default Value:
  • 10
Source:

nodes

Properties:
Name Type Description
nodes array

Array of associated child nodes.

Source:

objects

Properties:
Name Type Description
objects array

Array of quadtree children.

Source:

Methods

clear()

Clear the quadtree.

Source:

getIndex(rect) → {number}

Determine which node the object belongs to.

Parameters:
Name Type Description
rect Phaser.Rectangle | object

The bounds in which to check.

Source:
Returns:

index - Index of the subnode (0-3), or -1 if rect cannot completely fit within a subnode and is part of the parent node.

Type
number

insert(body)

Insert the object into the node. If the node exceeds the capacity, it will split and add all objects to their corresponding subnodes.

Parameters:
Name Type Description
body Phaser.Physics.Arcade.Body | object

The Body object to insert into the quadtree. Can be any object so long as it exposes x, y, right and bottom properties.

Source:

populate(group)

Populates this quadtree with the children of the given Group. In order to be added the child must exist and have a body property.

Parameters:
Name Type Description
group Phaser.Group

The Group to add to the quadtree.

Source:

populateHandler(sprite)

Handler for the populate method.

Parameters:
Name Type Description
sprite Phaser.Sprite | object

The Sprite to check.

Source:

reset(x, y, width, height, maxObjects, maxLevels, level)

Resets the QuadTree.

Parameters:
Name Type Argument Default Description
x number

The top left coordinate of the quadtree.

y number

The top left coordinate of the quadtree.

width number

The width of the quadtree in pixels.

height number

The height of the quadtree in pixels.

maxObjects number <optional>
10

The maximum number of objects per node.

maxLevels number <optional>
4

The maximum number of levels to iterate to.

level number <optional>
0

Which level is this?

Source:

retrieve(sprite) → {array}

Return all objects that could collide with the given Sprite.

Parameters:
Name Type Description
sprite Phaser.Sprite

The sprite to check against.

Source:
Returns:
  • Array with all detected objects.
Type
array

split()

Split the node into 4 subnodes

Source:
Phaser Copyright © 2012-2014 Photon Storm Ltd.
Documentation generated by JSDoc 3.3.0-dev on Tue Apr 29 2014 14:51:32 GMT+0100 (BST) using the DocStrap template.