Class: QuadTree

Phaser. QuadTree

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

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/

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:
src/math/QuadTree.js line 23

Members

bounds :object

Object that contains the quadtree bounds.

Type:
  • object
Source:
src/math/QuadTree.js line 45

level :number

The current level.

Type:
  • number
Source:
src/math/QuadTree.js line 40

maxLevels :number

The maximum number of levels to break down to.

Type:
  • number
Default Value:
  • 4
Source:
src/math/QuadTree.js line 35

maxObjects :number

The maximum number of objects per node.

Type:
  • number
Default Value:
  • 10
Source:
src/math/QuadTree.js line 29

nodes :array

Array of associated child nodes.

Type:
  • array
Source:
src/math/QuadTree.js line 55

objects :array

Array of quadtree children.

Type:
  • array
Source:
src/math/QuadTree.js line 50

Methods

clear()

Clear the quadtree.

Source:
src/math/QuadTree.js line 295

getIndex(rect)

Determine which node the object belongs to.

Parameters:
Name Type Description
rect Phaser.Rectangle | object

The bounds in which to check.

Source:
src/math/QuadTree.js line 203
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:
src/math/QuadTree.js line 151

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:
src/math/QuadTree.js line 103

populateHandler(sprite)

Handler for the populate method.

Parameters:
Name Type Description
sprite Phaser.Sprite | object

The Sprite to check.

Source:
src/math/QuadTree.js line 115

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:
src/math/QuadTree.js line 69

retrieve(source)

Return all objects that could collide with the given Sprite or Rectangle.

Parameters:
Name Type Description
source Phaser.Sprite | Phaser.Rectangle

The source object to check the QuadTree against. Either a Sprite or Rectangle.

Source:
src/math/QuadTree.js line 247
Returns:
  • Array with all detected objects.
Type
array

split()

Split the node into 4 subnodes

Source:
src/math/QuadTree.js line 130