A Quadtree implementation that supports collision detection for rectangles.

The outer bounds of the region

Additional options which configure the Quadtree

The maximum number of objects per node

The maximum number of levels within the root Quadtree

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

The root of the quadtree. For internal use

Hierarchy (View Summary)

Properties

depth: number

The depth of this node within the root Quadtree

maxDepth: number

The maximum number of levels that the base quadtree is allowed

maxObjects: number

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

nodes: Quadtree[]

Children of this node

objects: QuadtreeObject[]

The objects contained at this level of the tree

root: Quadtree

The root Quadtree

_bounds: Rectangle | { height: number; width: number; x: number; y: number }

Bounding rectangle of the quadtree.

INDICES: { bl: number; br: number; tl: number; tr: number } = ...

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

Accessors

  • get all(): QuadtreeObject[]

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

    Returns QuadtreeObject[]

  • get bounds(): Rectangle

    The bounding rectangle of the region

    Returns Rectangle

  • get height(): number

    The height of the bounding rectangle

    Returns number

  • get width(): number

    The width of the bounding rectangle

    Returns number

  • get x(): number

    The x-coordinate of the bounding rectangle

    Returns number

  • get y(): number

    The y-coordinate of the bounding rectangle

    Returns number

Methods

  • Clear the quadtree of all existing contents

    Returns Quadtree

    The cleared Quadtree

  • Identify all nodes which are adjacent to this one within the parent Quadtree.

    Returns Quadtree[]

  • 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

    • rect: Rectangle

      The target rectangle.

    Returns Quadtree[]

    The Quadtree nodes to which the target rectangle belongs

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

    Parameters

    • rect: Rectangle

      The target rectangle.

    Returns Quadtree[]

    The Quadtree nodes to which the target rectangle belongs

  • Get all the objects which could collide with the provided rectangle

    Parameters

    • rect: Rectangle

      The normalized target rectangle

    • Optionaloptions: { _s?: Set<any>; collisionTest?: Function } = {}

      Options affecting the collision test.

      • Optional_s?: Set<any>

        The existing result set, for internal use.

      • OptionalcollisionTest?: Function

        Function to further refine objects to return after a potential collision is found. Parameters are the object and rect, and the function should return true if the object should be added to the result set.

    Returns Set<any>

    The objects in the Quadtree which represent potential collisions

  • Add a rectangle object to the tree

    Parameters

    • obj: QuadtreeObject

      The object being inserted

    Returns Quadtree[]

    The Quadtree nodes the object was added to.

  • Remove an object from the quadtree

    Parameters

    • target: any

      The quadtree target being removed

    Returns Quadtree

    The Quadtree for method chaining

  • Re-dimension the bounding rectangle of this Quadtree, clear existing data, and re-insert all objects. Useful if the underlying canvas or region is resized.

    Parameters

    • width: number

      The new width of the bounding rectangle

    • height: number

      The new height of the bounding rectangle

    Returns Quadtree

    This Quadtree for method chaining

  • Re-position the bounding rectangle of this Quadtree, clear existing data, and re-insert all objects. Useful if the Quadtree needs to move.

    Parameters

    • x: number

      The new x-coordinate of the bounding rectangle

    • y: number

      The new y-coordinate of the bounding rectangle

    Returns Quadtree

    This Quadtree for method chaining

  • Split this node into 4 sub-nodes.

    Returns Quadtree

    The split Quadtree

  • Remove an existing object from the quadtree and re-insert it with a new position

    Parameters

    • obj: QuadtreeObject

      The object being inserted

    Returns Quadtree[]

    The Quadtree nodes the object was added to

  • Visualize the nodes and objects in the quadtree

    Parameters

    • Optionalobjects: boolean = {}

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

    Returns void