summaryrefslogtreecommitdiff
path: root/engine/components/BoundingBox.ts
diff options
context:
space:
mode:
authorElizabeth Hunt <elizabeth.hunt@simponic.xyz>2023-08-12 13:49:16 -0600
committerElizabeth Hunt <elizabeth.hunt@simponic.xyz>2023-08-12 13:49:16 -0600
commitc6e9baa0009f7cce0f6ff156a3957ef04a8cb684 (patch)
tree9766a4a33ad1c86aa71a5f92daf8917f3e5f5eed /engine/components/BoundingBox.ts
parentb67ffb57c1bf6e9628339a3f43c71ccebdb46136 (diff)
downloadjumpstorm-c6e9baa0009f7cce0f6ff156a3957ef04a8cb684.tar.gz
jumpstorm-c6e9baa0009f7cce0f6ff156a3957ef04a8cb684.zip
the great engine refactor
Diffstat (limited to 'engine/components/BoundingBox.ts')
-rw-r--r--engine/components/BoundingBox.ts57
1 files changed, 22 insertions, 35 deletions
diff --git a/engine/components/BoundingBox.ts b/engine/components/BoundingBox.ts
index 2b1d648..5e21b2f 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, normalizeVector } from "../utils";
+import { dotProduct, rotateVector } from "../utils";
export class BoundingBox extends Component {
public center: Coord2D;
@@ -15,10 +15,11 @@ export class BoundingBox extends Component {
this.rotation = rotation ?? 0;
}
+ // https://en.wikipedia.org/wiki/Hyperplane_separation_theorem
public isCollidingWith(box: BoundingBox): boolean {
const boxes = [this.getVertices(), box.getVertices()];
for (const poly of boxes) {
- for (let i = 0; i < poly.length; ++i) {
+ for (let i = 0; i < poly.length; i++) {
const [A, B] = [poly[i], poly[(i + 1) % poly.length]];
const normal: Coord2D = { x: B.y - A.y, y: A.x - B.x };
@@ -28,8 +29,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;
@@ -55,43 +56,29 @@ export class BoundingBox extends Component {
});
}
- private getAxes() {
- const corners: Coord2D[] = this.getVerticesRelativeToCenter();
- const axes: Coord2D[] = [];
-
- for (let i = 0; i < corners.length; ++i) {
- const [cornerA, cornerB] = [
- corners[i],
- corners[(i + 1) % corners.length],
- ].map((corner) => rotateVector(corner, this.rotation));
-
- axes.push(
- normalizeVector({
- x: cornerB.y - cornerA.y,
- y: -(cornerB.x - cornerA.x),
- })
- );
+ public getRotationInPiOfUnitCircle() {
+ let rads = this.rotation * (Math.PI / 180);
+ if (rads >= Math.PI) {
+ rads -= Math.PI;
}
-
- return axes;
+ return rads;
}
- private project(axis: Coord2D): [number, number] {
- const corners = this.getCornersRelativeToCenter();
- let [min, max] = [Infinity, -Infinity];
+ public getOutscribedBoxDims(): Dimension2D {
+ let rads = this.getRotationInPiOfUnitCircle();
+ const { width, height } = this.dimension;
- for (const corner of corners) {
- const rotated = rotateVector(corner, this.rotation);
- const translated = {
- x: rotated.x + this.center.x,
- y: rotated.y + this.center.y,
+ 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)),
};
- const projection = dotProduct(translated, axis);
-
- min = Math.min(projection, min);
- max = Math.max(projection, max);
}
- return [min, max];
+ 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)),
+ };
}
}