summaryrefslogtreecommitdiff
path: root/engine/components/BoundingBox.ts
diff options
context:
space:
mode:
authorElizabeth (Lizzy) Hunt <elizabeth.hunt@simponic.xyz>2023-08-26 17:57:05 -0600
committerGitHub <noreply@github.com>2023-08-26 17:57:05 -0600
commit8a4ab8d79b5ce1dabb431168398b5d5111fe326c (patch)
treee60767dc5295edf379cf421e20171dc418e548b7 /engine/components/BoundingBox.ts
parentc6e9baa0009f7cce0f6ff156a3957ef04a8cb684 (diff)
parent6ce6946a4401d2ee6fa5cb747fab7d4c658a63c8 (diff)
downloadjumpstorm-8a4ab8d79b5ce1dabb431168398b5d5111fe326c.tar.gz
jumpstorm-8a4ab8d79b5ce1dabb431168398b5d5111fe326c.zip
Merge pull request #1 from Simponic/network
Network
Diffstat (limited to 'engine/components/BoundingBox.ts')
-rw-r--r--engine/components/BoundingBox.ts61
1 files changed, 49 insertions, 12 deletions
diff --git a/engine/components/BoundingBox.ts b/engine/components/BoundingBox.ts
index 5e21b2f..921feb9 100644
--- a/engine/components/BoundingBox.ts
+++ b/engine/components/BoundingBox.ts
@@ -1,6 +1,6 @@
-import { Component, ComponentNames } from ".";
-import type { Coord2D, Dimension2D } from "../interfaces";
-import { dotProduct, rotateVector } from "../utils";
+import { Component, ComponentNames } from '.';
+import type { Coord2D, Dimension2D } from '../interfaces';
+import { dotProduct, rotateVector } from '../utils';
export class BoundingBox extends Component {
public center: Coord2D;
@@ -15,8 +15,27 @@ export class BoundingBox extends Component {
this.rotation = rotation ?? 0;
}
- // 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;
+ }
+
+ // https://en.wikipedia.org/wiki/Hyperplane_separation_theorem
const boxes = [this.getVertices(), box.getVertices()];
for (const poly of boxes) {
for (let i = 0; i < poly.length; i++) {
@@ -29,8 +48,8 @@ export class BoundingBox extends Component {
const projection = dotProduct(normal, vertex);
return [Math.min(min, projection), Math.max(max, projection)];
},
- [Infinity, -Infinity],
- ),
+ [Infinity, -Infinity]
+ )
);
if (maxThis < minBox || maxBox < minThis) return false;
@@ -45,20 +64,22 @@ export class BoundingBox extends Component {
{ x: -this.dimension.width / 2, y: -this.dimension.height / 2 },
{ x: -this.dimension.width / 2, y: this.dimension.height / 2 },
{ x: this.dimension.width / 2, y: this.dimension.height / 2 },
- { x: this.dimension.width / 2, y: -this.dimension.height / 2 },
+ { x: this.dimension.width / 2, y: -this.dimension.height / 2 }
]
- .map((vertex) => rotateVector(vertex, this.rotation))
+ .map((vertex) => rotateVector(vertex, this.rotation)) // rotate
.map((vertex) => {
+ // translate
return {
x: vertex.x + this.center.x,
- y: vertex.y + this.center.y,
+ y: vertex.y + this.center.y
};
});
}
- public getRotationInPiOfUnitCircle() {
+ public getRotationInPiOfUnitCircle(): number {
let rads = this.rotation * (Math.PI / 180);
if (rads >= Math.PI) {
+ // Physics system guarantees rotation \in [0, 360)
rads -= Math.PI;
}
return rads;
@@ -68,17 +89,33 @@ export class BoundingBox extends Component {
let rads = this.getRotationInPiOfUnitCircle();
const { width, height } = this.dimension;
+ if (rads == 0) return this.dimension;
+
if (rads <= Math.PI / 2) {
return {
width: Math.abs(height * Math.sin(rads) + width * Math.cos(rads)),
- height: Math.abs(width * Math.sin(rads) + height * Math.cos(rads)),
+ height: Math.abs(width * Math.sin(rads) + height * Math.cos(rads))
};
}
rads -= Math.PI / 2;
return {
width: Math.abs(height * Math.cos(rads) + width * Math.sin(rads)),
- height: Math.abs(width * Math.cos(rads) + height * Math.sin(rads)),
+ 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
};
}
}