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/ @class Phaser.QuadTree @constructor @param {number} x - The top left coordinate of the quadtree. @param {number} y - The top left coordinate of the quadtree. @param {number} width - The width of the quadtree in pixels. @param {number} height - The height of the quadtree in pixels. @param {number} [maxObjects=10] - The maximum number of objects per node. @param {number} [maxLevels=4] - The maximum number of levels to iterate to. @param {number} [level=0] - Which level is this?

Constructor

@:value({ level : 0, maxLevels : 4, maxObjects : 10 })new(?pool:QuadTreePool, x:Float, y:Float, width:Float, height:Float, maxObjects:Int = 10, maxLevels:Int = 4, level:Int = 0)

Variables

@:value(false)busy:Bool = false

A flag used to know if this quad tree is currently busy and should not be used by something else.

@:value(10)maxObjects:Int = 10

@property {number} maxObjects - The maximum number of objects per node. @default

@:value(4)maxLevels:Int = 4

@property {number} maxLevels - The maximum number of levels to break down to. @default

@:value(0)level:Int = 0

@property {number} level - The current level.

@:value(0)boundsX:Float = 0

@:value(0)boundsY:Float = 0

@:value(0)boundsWidth:Float = 0

@:value(0)boundsHeight:Float = 0

@:value(0)boundsSubWidth:Float = 0

@:value(0)boundsSubHeight:Float = 0

@:value(0)boundsRight:Float = 0

@:value(0)boundsBottom:Float = 0

@:value([])objects:Array<Body> = []

@property {array} objects - Array of quadtree children.

@:value([])nodes:Array<QuadTree> = []

@property {array} nodes - Array of associated child nodes.

Methods

@:value({ level : 0, maxLevels : 4, maxObjects : 10 })reset(x:Float, y:Float, width:Float, height:Float, maxObjects:Int = 10, maxLevels:Int = 4, level:Int = 0):Void

Resets the QuadTree.

@method Phaser.QuadTree#reset

Parameters:

{number}

x - The top left coordinate of the quadtree.

{number}

y - The top left coordinate of the quadtree.

{number}

width - The width of the quadtree in pixels.

{number}

height - The height of the quadtree in pixels.

{number}

[maxObjects=10] - The maximum number of objects per node.

{number}

[maxLevels=4] - The maximum number of levels to iterate to.

{number}

[level=0] - Which level is this?

inlinepopulate(group:Group):Void

inlinepopulate(objects:Array<Body>):Void

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

@method Phaser.QuadTree#populate

Parameters:

{Phaser.Group}

group - The Group to add to the quadtree.

split():Void

Split the node into 4 subnodes

@method Phaser.QuadTree#split

insert(body:Body):Void

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

@method Phaser.QuadTree#insert

Parameters:

{Phaser.Physics.Arcade.Body|object}

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

getIndex(left:Float, top:Float, right:Float, bottom:Float):Int

Determine which node the object belongs to.

@method Phaser.QuadTree#getIndex

Parameters:

{Phaser.Rectangle|object}

rect - The bounds in which to check.

Returns:

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

retrieve(left:Float, top:Float, right:Float, bottom:Float):Array<Body>

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

@method Phaser.QuadTree#retrieve

Parameters:

{Phaser.Sprite|Phaser.Rectangle}

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

Returns:

{array} - Array with all detected objects.

clear():Void

Clear the quadtree. @method Phaser.QuadTree#clear