blaze-2d
Version:
A fast and simple WebGL 2 2D game engine written in TypeScript
71 lines • 2.02 kB
JavaScript
import AABB from "./aabb";
export default class AABBNode {
constructor(left, right, margin) {
this.childrenCrossed = false;
if (left instanceof AABB) {
this.aabb = left;
}
else {
left.parent = this;
right.parent = this;
this.left = left;
this.right = right;
this.updateAABB(margin);
}
}
/**
* Determines wether or not this node is a leaf node.
*
* @returns Wether or not this node is a leaf node
*/
isLeaf() {
return !this.left;
}
/**
* Updates the node's {@link AABB}.
*
* If the node is a leaf node the {@link AABB} is created from the current `aabb`'s `obj`.
*
* Otherwise the {@link AABB} is the union of the node's children's AABBs.
*
* @param margin The margin to use when creating the fat AABB
*/
updateAABB(margin) {
if (this.isLeaf()) {
this.aabb.setMinMaxFromCollisionObj(this.aabb.obj, margin);
}
else {
if (this.aabb)
this.aabb.union(this.left.aabb, this.right.aabb, margin);
else
this.aabb = new AABB(this.left.aabb, this.right.aabb, margin);
}
}
/**
* Replaces one of the node's children with a new node.
*
* @param oldChild The old child to replace
* @param newChild The new child
*/
replaceChild(oldChild, newChild) {
if (this.left === oldChild) {
newChild.parent = this;
this.left = newChild;
}
else if (this.right === oldChild) {
newChild.parent = this;
this.right = newChild;
}
}
/**
* Gets this node's sibling.
*
* @returns this node's sibling
*/
sibling() {
if (!this.parent)
return undefined;
return this === this.parent.left ? this.parent.right : this.parent.left;
}
}
//# sourceMappingURL=aabbNode.js.map