@timohausmann/quadtree-ts
    Preparing search index...

    Class Quadtree<ObjectsType>

    Class representing a Quadtree node.

    const tree = new Quadtree({
    width: 100,
    height: 100,
    x: 0, // optional, default: 0
    y: 0, // optional, default: 0
    maxObjects: 10, // optional, default: 10
    minLevels: 0, // optional, default: 0
    maxLevels: 4, // optional, default: 4
    });
    class GameEntity extends Rectangle {
    ...
    }
    const tree = new Quadtree<GameEntity>({
    width: 100,
    height: 100,
    });

    Type Parameters

    Index

    Constructors

    Properties

    bounds: NodeGeometry

    The numeric boundaries of this node.

    level: number

    The level of this node.

    0

    maxLevels: number

    Total max nesting levels of the root Quadtree node.

    4

    maxObjects: number

    Max objects this node can hold before it splits.

    10

    minLevels: number

    Minimum nesting levels of the root Quadtree node.

    0

    Subnodes of this node

    []

    objects: ObjectsType[]

    Array of objects in this node.

    []

    Methods

    • Clear the Quadtree.

      Returns void

      const tree = new Quadtree({ width: 100, height: 100 });
      tree.insert(new Circle({ x: 25, y: 25, r: 10 }));
      tree.clear();
      console.log(tree); // tree.objects and tree.nodes are empty
    • Get the quadrant (subnode indexes) an object belongs to.

      Parameters

      Returns number[]

      Array containing indexes of intersecting subnodes (0-3 = top-right, top-left, bottom-left, bottom-right).

      const tree = new Quadtree({ width: 100, height: 100 });
      const rectangle = new Rectangle({ x: 25, y: 25, width: 10, height: 10 });
      const indexes = tree.getIndex(rectangle);
      console.log(indexes); // [1]
    • Insert an object into the node. If the node exceeds the capacity, it will split and add all objects to their corresponding subnodes.

      Parameters

      Returns void

      const tree = new Quadtree({ width: 100, height: 100 });
      tree.insert(new Rectangle({ x: 25, y: 25, width: 10, height: 10, data: 'data' }));
      tree.insert(new Circle({ x: 25, y: 25, r: 10, data: 512 }));
      tree.insert(new Line({ x1: 25, y1: 25, x2: 60, y2: 40, data: { custom: 'property'} }));
    • The opposite of a split: merge and dissolve subnodes (when total object count doesn't exceed maxObjects).

      Returns ObjectsType[]

      The objects from this node and all subnodes combined.

      const tree = new Quadtree({ width: 100, height: 100 });
      tree.split();
      console.log(tree.nodes.length); // 4
      tree.join();
      console.log(tree.nodes.length); // 0
    • Beta

      Remove an object from the tree. If you have to remove many objects, consider clearing the entire tree and rebuilding it or use the fast flag to cleanup after the last removal.

      Parameters

      • obj: ObjectsType

        Object to be removed.

      • fast: boolean = false

        Set to true to increase performance temporarily by preventing cleanup of empty subnodes (optional, default: false).

      Returns boolean

      Weather or not the object was removed from THIS node (no recursive check).

      const tree = new Quadtree({ width: 100, height: 100 });
      const circle = new Circle({ x: 25, y: 25, r: 10, data: 512 });
      tree.insert(circle);
      tree.remove(circle);
      const tree = new Quadtree({ width: 100, height: 100 });
      const rects = [];
      for(let i=0; i<20; i++) {
      rects[i] = new Rectangle({ x: 25, y: 25, width: 50, height: 50 });
      tree.insert(rects[i]);
      }
      for(let i=rects.length-1; i>0; i--) {
      //fast=true – just remove the object (may leaves vacant subnodes)
      //fast=false – cleanup empty subnodes (default)
      const fast = (i !== 0);
      tree.remove(rects[i], fast);
      }
    • Return all objects that could collide with the given geometry.

      Parameters

      Returns ObjectsType[]

      Array containing all detected objects.

      tree.retrieve(new Rectangle({ x: 25, y: 25, width: 10, height: 10, data: 'data' }));
      tree.retrieve(new Circle({ x: 25, y: 25, r: 10, data: 512 }));
      tree.retrieve(new Line({ x1: 25, y1: 25, x2: 60, y2: 40, data: { custom: 'property'} }));
    • Internal

      Split the node into 4 subnodes. Mostly for internal use! You should only call this yourself if you know what you are doing.

      Returns void

      const tree = new Quadtree({ width: 100, height: 100 });
      tree.split();
      console.log(tree); // now tree has four subnodes
    • Beta

      Update an object already in the tree (shorthand for remove and insert). If you have to update many objects, consider clearing and rebuilding the entire tree or use the fast flag to cleanup after the last update.

      Parameters

      • obj: ObjectsType

        Object to be updated.

      • fast: boolean = false

        Set to true to increase performance temporarily by preventing cleanup of empty subnodes (optional, default: false).

      Returns void

      const tree = new Quadtree({ width: 100, height: 100, maxObjects: 1 });
      const rect1 = new Rectangle({ x: 25, y: 25, width: 10, height: 10 });
      const rect2 = new Rectangle({ x: 25, y: 25, width: 10, height: 10 });
      tree.insert(rect1);
      tree.insert(rect2);
      rect1.x = 75;
      rect1.y = 75;
      tree.update(rect1);
      const tree = new Quadtree({ width: 100, height: 100 });
      const rects = [];
      for(let i=0; i<20; i++) {
      rects[i] = new Rectangle({ x: 20, y: 20, width: 20, height: 20 });
      tree.insert(rects[i]);
      }
      for(let i=rects.length-1; i>0; i--) {
      rects[i].x = 20 + Math.random()*60;
      rects[i].y = 20 + Math.random()*60;
      //fast=true – just re-insert the object (may leaves vacant subnodes)
      //fast=false – cleanup empty subnodes (default)
      const fast = (i !== 0);
      tree.update(rects[i], fast);
      }