Class QuadTree

a QuadTree implementation in JavaScript, a 2d spatial subdivision algorithm.

game.world.broadphase

Constructors

  • Parameters

    • world: World

      the physic world this QuadTree belongs to

    • bounds: Bounds

      bounds of the node

    • Optionalmax_objects: number = 4

      max objects a node can hold before splitting into 4 subnodes

    • Optionalmax_levels: number = 4

      total max levels inside root Quadtree

    • Optionallevel: number = 0

      deepth level, required for subnodes

    Returns QuadTree

Properties

bounds: Bounds
level: number
max_levels: number
max_objects: number
nodes: any[]
objects: any[]
world: World

Methods

  • Insert the given object into the node. If the node exceeds the capacity, it will split and add all objects to their corresponding subnodes.

    Parameters

    • item: object

      object to be added

    Returns void

  • Remove the given item from the quadtree. (this function won't recalculate the impacted node)

    Parameters

    • item: object

      object to be removed

    Returns boolean

    true if the item was found and removed.

  • Return all objects that could collide with the given object

    Parameters

    • item: object

      object to be checked against

    • Optionalfn: object

      a sorting function for the returned array

    Returns object[]

    array with all detected objects