melonJS
    Preparing search index...

    Class QuadTree

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

    game.world.broadphase

    Index

    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.

    • Recursively remove the given container and its descendants from the quadtree. Mirrors insertContainer so the broadphase can be kept in sync when a subtree is detached via Container.removeChildNow between two world.update() rebuilds (pointer events fire async in that window and would otherwise hit destroyed renderables).

      Parameters

      • container: Container

        group of objects to be removed

      Returns void

    • Return all objects that could collide with the given object.

      Re-entrancy contract: when called with no explicit result argument, this method reuses a single root-level scratch array to avoid per-frame allocations. The returned reference is therefore not safe to retain past the next retrieve() call, AND it is not safe to issue another scratch-mode retrieve() while iterating the previous result — the second call clears the scratch and refills it, corrupting the outer iteration. In-engine callers (pointerevent.ts, detector.js) iterate synchronously and never recurse into retrieve(), so they're fine. User-facing portable APIs (adapter.queryAABB, adapter.raycast) pass their own array via the result parameter, which bypasses the scratch entirely and is safe to call from inside collision handlers.

      Parameters

      • item: object

        object to be checked against

      • Optionalfn: object

        a sorting function for the returned array

      • Optionalresult: object[]

        optional caller-supplied result array. Pass an explicit (typically empty) array to sidestep the shared scratch — required for re-entrancy safety.

      Returns object[]

      array with all detected objects