diff --git a/index.js b/index.js index 7fb3a63..0d519a4 100644 --- a/index.js +++ b/index.js @@ -21,7 +21,7 @@ const grid = new HashGrid( new Vector2(0, 0) ) -demoGrid(grid, renderer, 30) +demoGrid(aabbtree, renderer, 30) renderer.update() function demoGrid(grid, renderer, number = 10) { diff --git a/src/aabbtree.js b/src/aabbtree.js index fd75af8..9977bdb 100644 --- a/src/aabbtree.js +++ b/src/aabbtree.js @@ -33,6 +33,9 @@ class AabbTreeNode { * @param {AabbTreeNode} node2 * @param {AabbTreeNode} out */ + hasChildren() { + return this.right && this.left + } static union(node1, node2, out = new AabbTreeNode()) { BoundingBox.union(node1.bounds, node2.bounds, out.bounds) return out @@ -248,6 +251,57 @@ export class AabbTree { this._pool.take(this._resetNode(current)) } } + /** + * @ignore + */ + getCollisionPairs(func, clients) { + return AabbTree.getCollisionPairUsingClients(this, func, clients, []) + } + /** + * @template T + * @template U + * @param {AabbTree} tree + * @param {CollisionChecker} func + * @param {U[]} target + */ + static getCollisionPairs(tree, func, target = []) {} + /** + * @template T + * @template U + * @param {AabbTree} tree + * @param {CollisionChecker} func + * @param {Client} + * @param {U[]} target + */ + static getCollisionPairUsingClients(tree, func, clients, target = []) { + for (let i = 0; i < clients.length; i++) { + AabbTree.getCollisionPairUsingClient(tree, clients[i], func, target) + } + return target + } + /** + * @template T + * @template U + * @param {Client>} client + * @param {CollisionChecker,U>} func + * @param {U[]} target + */ + static getCollisionPairUsingClient(tree, client, func, target) { + const nodes = [tree.root] + while (nodes.length) { + const node = nodes.pop() + + if (!node.bounds.intersects(client.node.bounds)) continue + if (node.hasChildren()) { + nodes.push(node.left) + nodes.push(node.right) + } + if (!node.value) continue + if (node.value === client) continue + const t = func(node.value, client) + if (t) target.push(t) + } + } } /** * @param {BoundingBox} bounds @@ -278,4 +332,10 @@ function calcArea(bounds) { * @template T * @template U * @typedef {import("./types.js").TraverserFunc} TraverserFunc + */ +/** + * @template T + * @template U + * @template V + * @typedef {import("./types.js").CollisionChecker} CollisionChecker */ \ No newline at end of file