summaryrefslogtreecommitdiff
path: root/engine/components/BoundingBox.ts
diff options
context:
space:
mode:
authorElizabeth Hunt <elizabeth.hunt@simponic.xyz>2023-08-17 22:42:09 -0600
committerElizabeth Hunt <elizabeth.hunt@simponic.xyz>2023-08-17 22:42:09 -0600
commit432ce5428f357f31ae090d55c5183b4eccd5a37c (patch)
treea616ff32a1ac2a3f305c089954cdf6e6432f1ad8 /engine/components/BoundingBox.ts
parent1c28e10b860056d85cc07e5a834c4a54eac14563 (diff)
downloadjumpstorm-432ce5428f357f31ae090d55c5183b4eccd5a37c.tar.gz
jumpstorm-432ce5428f357f31ae090d55c5183b4eccd5a37c.zip
increase collision performance _heavily_
Diffstat (limited to 'engine/components/BoundingBox.ts')
-rw-r--r--engine/components/BoundingBox.ts33
1 files changed, 33 insertions, 0 deletions
diff --git a/engine/components/BoundingBox.ts b/engine/components/BoundingBox.ts
index 19967f7..26b404d 100644
--- a/engine/components/BoundingBox.ts
+++ b/engine/components/BoundingBox.ts
@@ -17,6 +17,25 @@ export class BoundingBox extends Component {
// https://en.wikipedia.org/wiki/Hyperplane_separation_theorem
public isCollidingWith(box: BoundingBox): boolean {
+ if (this.rotation == 0 && box.rotation == 0) {
+ const thisTopLeft = this.getTopLeft();
+ const thisBottomRight = this.getBottomRight();
+
+ const thatTopLeft = box.getTopLeft();
+ const thatBottomRight = box.getBottomRight();
+
+ if (
+ thisBottomRight.x <= thatTopLeft.x ||
+ thisTopLeft.x >= thatBottomRight.x ||
+ thisBottomRight.y <= thatTopLeft.y ||
+ thisTopLeft.y >= thatBottomRight.y
+ ) {
+ return false;
+ }
+
+ return true;
+ }
+
const boxes = [this.getVertices(), box.getVertices()];
for (const poly of boxes) {
for (let i = 0; i < poly.length; i++) {
@@ -83,4 +102,18 @@ export class BoundingBox extends Component {
height: Math.abs(width * Math.cos(rads) + height * Math.sin(rads)),
};
}
+
+ public getTopLeft(): Coord2D {
+ return {
+ x: this.center.x - this.dimension.width / 2,
+ y: this.center.y - this.dimension.height / 2,
+ };
+ }
+
+ public getBottomRight(): Coord2D {
+ return {
+ x: this.center.x + this.dimension.width / 2,
+ y: this.center.y + this.dimension.height / 2,
+ };
+ }
}