A subclass of Quadtree specifically intended for classifying the location of objects on the game canvas.

Hierarchy (View Summary)

Constructors

  • Create a CanvasQuadtree which references canvas.dimensions.rect. We pass an empty object to the parent, then override _bounds.

    Parameters

    • Optionaloptions: object = {}

      Additional options passed to the parent Quadtree.

    Returns CanvasQuadtree

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

  • 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

  • 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