UNPKG

blaze-2d

Version:

A fast and simple WebGL 2 2D game engine written in TypeScript

105 lines (104 loc) 3.19 kB
import { vec2 } from "gl-matrix"; import CollisionObject from "../collisionObject"; import CollisionPair from "../collisionPair"; import Ray from "../ray"; import AABB from "./aabb"; import AABBNode from "./aabbNode"; /** * Represents a tree data structure which contains nodes that store {@link AABB}s. * * This tree is used to speed up the physics engine's broadphase. * * @see [Intro to AABB Trees](https://www.azurefromthetrenches.com/introductory-guide-to-aabb-tree-collision-detection/) * @see [Physics Broadphase - AABB Tree](http://allenchou.net/2014/02/game-physics-broadphase-dynamic-aabb-tree/) */ export default class AABBTree { root: AABBNode; margin: number; leafMarginScale: number; leafMarginSlop: number; leafMarginMin: number; leafMarginMax: number; pairs: CollisionPair[]; /** * The number of nodes which had to be reinserted in the last tree update. */ insertionsLastUpdate: number; /** * Updates the tree. */ update(): void; /** * Updates a leaf node's aabb's margin based on the aabb's bound {@link CollisionObject}. * * @param leaf The leaf node to update * @param force Force the margin to be updated even when there is an insufficient margin difference * @returns The new margin or false if the margin wasn't updated. */ private updateAABBMargin; /** * Obtains all the pairs of {@link CollisionObject}s which could possibly be colliding. * * @returns An array containing the {@link CollisionPair}s */ collectPairs(): CollisionPair[]; /** * Sets the `childrenCrossed` flag to false on all nodes in the tree. */ private clearChildrenCrossedFlag; private crossChildren; /** * Collects {@link CollisionPair}s. * * @param a An {@link AABBNode} to check for pairs * @param b An {@link AABBNode} to check for pairs */ private collectPairsHelper; /** * Picks aabbs in the tree. * * @param point The point to pick {@link AABB}s at. * @returns The picked aabbs */ pick(point: vec2): AABB[]; /** * Raycasts against aabbs in the tree. * * @param ray The ray to cast. */ raycast(ray: Ray): AABB[]; /** * Inserts an aabb into the tree. * * @param aabb The {@link AABB} to insert */ add(aabb: AABB): void; /** * Removes the leaf node containing the {@link AABB} bounding the given {@link CollisionObject}. * * @param obj The {@link CollisionObject} to remove */ remove(obj: CollisionObject): void; /** * Inserts a node into the tree. * * @param node The {@link AABBNode} to insert */ private addNode; /** * Remove the given node and replace the parent with the node's sibling * * @param node The node to remove */ private removeNode; /** * Calculates and returns the height of the root node. (the height of the tree) */ getHeight(): number; /** * Calculates the height of the given node. * * @param node The node to start at */ getHeight(node: AABBNode): number; }