Quadtree

Quadtree

A Quadtree implementation that supports collision detection for rectangles.

Constructor

new Quadtree(The, options)

Source:
Parameters:
Name Type Description
The Rectangle

outer bounds of the region

options object

Additional options which configure the Quadtree

Properties
Name Type Description
maxObjects number

The maximum number of objects per node

maxDepth number

The maximum number of levels within the root Quadtree

_depth number

The depth level of the sub-tree. For internal use only

_depth number

Whether this node represents the root of the tree. For internal use.

Members

(static) INDICES :Object

Source:

A constant that enumerates the index order of the quadtree nodes from top-left to bottom-right.

Type:
  • Object

all

Source:

Return an array of all the objects in the Quadtree (recursive)

bounds :Rectangle

Source:

The bounding rectangle of the region

Type:

depth :number

Source:

The depth of this node within the root Quadtree

Type:
  • number

maxDepth :number

Source:

The maximum number of levels that the base quadtree is allowed

Type:
  • number

maxObjects :number

Source:

The maximum number of objects allowed within this node before it must split

Type:
  • number

nodes :Array.<Quadtree>

Source:

Children of this node

Type:

objects :Array.<QuadtreeObject>

Source:

The objects contained at this level of the tree

Type:

Methods

clear() → {Quadtree}

Source:

Clear the quadtree of all existing contents

Returns:

The cleared Quadtree

Type
Quadtree

getChildNodes(rect) → {Array.<Quadtree>}

Source:

Obtain the child nodes within the current node which a rectangle belongs to. Note that this function is not recursive, it only returns nodes at the current or child level.

Parameters:
Name Type Description
rect Rectangle

The target rectangle.

Returns:

The Quadtree nodes to which the target rectangle belongs

Type
Array.<Quadtree>

getLeafNodes(rect) → {Array.<Quadtree>}

Source:

Obtain the leaf nodes to which a target rectangle belongs. This traverses the quadtree recursively obtaining the final nodes which have no children.

Parameters:
Name Type Description
rect Rectangle

The target rectangle.

Returns:

The Quadtree nodes to which the target rectangle belongs

Type
Array.<Quadtree>

getObjects(rect, _s) → {Set}

Source:

Get all the objects which could collide with the provided rectangle

Parameters:
Name Type Description
rect Rectangle

The target rectangle

_s Set

The existing result set, for internal use.

Returns:

The objects in the Quadtree which represent potential collisions

Type
Set

insert(obj) → {Array.<Quadtree>}

Source:

Add a rectangle object to the tree

Parameters:
Name Type Description
obj QuadtreeObject

The object being inserted

Returns:

The Quadtree nodes the object was added to.

Type
Array.<Quadtree>

remove(The) → {Quadtree}

Source:

Remove an object from the quadtree

Parameters:
Name Type Description
The target

quadtree target being removed

Returns:

The Quadtree for method chaining

Type
Quadtree

split() → {Quadtree}

Source:

Split this node into 4 sub-nodes.

Returns:

The split Quadtree

Type
Quadtree

visualize(objectsopt)

Source:

Visualize the nodes and objects in the quadtree

Parameters:
Name Type Attributes Description
objects boolean <optional>

Visualize the rectangular bounds of objects in the Quadtree. Default is false.