Simple QuadTree implementation to assist with broad-phase 2D collisions.

Static methods

@:value({ height : 1, width : 1, y : 0, x : 0 })staticinlineget(x:Float = 0, y:Float = 0, width:Float = 1, height:Float = 1):QuadTree

Gets an Quadtree from the pool, or creates a new one if none are available. Call put() on the Quadtree to place it back in the pool.

Note - The X and Y positions represent the center of the Quadtree. To set the Quadtree from its Top-Left origin, Quadtree.get_from_min_max() is available.

Parameters:

x

The centered X position of the Quadtree.

y

The centered Y position of the Quadtree.

width

The width of the Quadtree.

height

The height of the Quadtree.

Returns:

Quadtree

staticinlineget_from_min_max(min_x:Float, min_y:Float, max_x:Float, max_y:Float):QuadTree

Gets an Quadtree from the pool, or creates a new one if none are available. Call put() on the Quadtree to place it back in the pool.

Parameters:

min_x
min_y
max_x
max_y

Returns:

Quadtree

Variables

children:Vector<QuadTree>

The child QuadTrees contained in the Quadtree. If this Vector is empty, the Quadtree is regarded as a leaf.

contents:Array<QuadTreeData>

The QuadTreeData contained in the Quadtree. If the Quadtree is not a leaf, all of it's contents will be dispersed to it's children QuadTrees (leaving this aryar emyty).

read onlycount:Int

Gets the total amount of QuadTreeData contents in the Quadtree, recursively. To get the non-recursive amount, check quadtree.contents_count.

depth:Int

The QuadTree's branch position in it's collection.

read onlyleaf:Bool

A QuadTree is regarded as a leaf if it has no QuadTree children (ie quadtree.children.length == 0).

@:value(10)max_contents:Int = 10

The maximum amount of QuadTreeData contents that a QuadTree leaf can hold before becoming a branch and splitting it's contents between children Quadtrees.

@:value(5)max_depth:Int = 5

The maximum branch depth for this QuadTree collection. Once the max depth is reached, the QuadTrees at the end of the collection will not spilt.

Methods

inlineclear():Void

Clears the Quadtree's QuadTreeData contents and all children Quadtrees.

insert(data:QuadTreeData):Void

Attempts to insert the QuadTreeData into the QuadTree. If the QuadTreeData already exists in the QuadTree, use quadtree.update(data) instead.

inlineput():Void

Puts the QuadTree back in the pool of available QuadTrees.

query(aabb:AABB, result:Array<QuadTreeData>):Void

Queries the QuadTree for any QuadTreeData that overlaps the AABB.

Parameters:

aabb

The AABB to query.

result

An Array containing all QuadTreeData that collides with the shape.

@:value({ allow_shake : true })remove(data:QuadTreeData, allow_shake:Bool = true):Bool

Attempts to remove the QuadTreeData from the QuadTree.

shake():Bool

If the QuadTree is a branch (not a leaf), this will check if the amount of data from all the child Quadtrees can fit in the Quadtree without exceeding it's max_contents. If all the data can fit, the Quadtree branch will "shake" its child Quadtrees, absorbing all the data and clearing the children (putting all the child Quadtrees back in the pool).

Note - This works recursively.

@:value({ allow_shake : true })update(data:QuadTreeData, allow_shake:Bool = true):Void

Updates the QuadTreeData in the QuadTree by first removing the QuadTreeData from the QuadTree, then inserting it.

Parameters:

null

data

Inherited Variables

Defined by AABB

read onlyheight:Float

@:value(false)read onlypooled:Bool = false

read onlywidth:Float

Inherited Methods

Defined by AABB

inlineadd(aabb:AABB):Void

Adds the bounds of an AABB into this AABB.

Parameters:

null

aabb

inlineclone():AABB

inlinecontains(point:Vector2):Bool

inlineload(aabb:AABB):AABB

inlineoverlaps(other:AABB):Bool

@:value({ height : 1, width : 1, y : 0, x : 0 })inlineset(x:Float = 0, y:Float = 0, width:Float = 1, height:Float = 1):AABB

Sets the values on this AABB.

Note - The X and Y positions represent the center of the AABB. To set the AABB from its Top-Left origin, AABB.set_from_min_max() is available.

Parameters:

x

The centered X position of the AABB.

y

The centered Y position of the AABB.

width

The width of the AABB.

height

The height of the AABB.

Returns:

AABB

inlineset_from_min_max(min_x:Float, min_y:Float, max_x:Float, max_y:Float):AABB

@:value({ put_self : false })inlineto_rect(put_self:Bool = false):Rect