feat: Rectangle.toPoints

This commit is contained in:
cha0s 2019-03-26 17:21:16 -05:00
parent b8b1b98580
commit 906997eeee
3 changed files with 18 additions and 25 deletions

View File

@ -35,19 +35,7 @@ export class Listed extends Trait {
}
const quadTree = list.quadTree();
const aabb = this.entity.graphicalBoundingBox;
// Break into 4 points.
const width = aabb[2] - .0001;
const height = aabb[3] - .0001;
const upperLeft = Rectangle.position(aabb);
const upperRight = Vector.add(upperLeft, [width, 0]);
const lowerLeft = Vector.add(upperLeft, [0, height]);
const lowerRight = Vector.add(upperLeft, [width, height]);
const points = [
upperLeft,
upperRight,
lowerLeft,
lowerRight,
];
const points = Rectangle.toPoints(aabb);
this.quadTreeNodes = points.map((point) => [...point, this.entity]);
// Add points to quad tree.
for (const node of this.quadTreeNodes) {

View File

@ -204,3 +204,19 @@ export function floor(r) {
Math.floor(r[3]),
];
}
export function toPoints(r) {
// Break into 4 points.
const width = r[2] - .0001;
const height = r[3] - .0001;
const upperLeft = position(r);
const upperRight = Vector.add(upperLeft, [width, 0]);
const lowerLeft = Vector.add(upperLeft, [0, height]);
const lowerRight = Vector.add(upperLeft, [width, height]);
return [
upperLeft,
upperRight,
lowerLeft,
lowerRight,
];
}

View File

@ -28,18 +28,7 @@ export class World extends AbstractWorld {
addQuadTreeNodes(body) {
// 4 points.
const aabb = body.aabb;
const width = aabb[2] - .0001;
const height = aabb[3] - .0001;
const upperLeft = Rectangle.position(aabb);
const upperRight = Vector.add(upperLeft, [width, 0]);
const lowerLeft = Vector.add(upperLeft, [0, height]);
const lowerRight = Vector.add(upperLeft, [width, height]);
const points = [
upperLeft,
upperRight,
lowerLeft,
lowerRight,
];
const points = Rectangle.toPoints(aabb);
const nodes = points.map((point) => [...point, body]);
this.quadTreeNodes = this.quadTreeNodes.set(body, nodes);
for (const node of nodes) {