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
Variables
busy:Bool = false
A flag used to know if this quad tree is currently busy and should not be used by something else.
Methods
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. |
---|
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.