Fixes circular SAT

This commit is contained in:
2023-11-05 01:41:21 -06:00
parent e70787260a
commit da77aa10bb
7 changed files with 174 additions and 104 deletions

View File

@@ -33,7 +33,14 @@ export class Polygon {
return center;
}
get circularHitbox(): CircleLike {
_circularBoundingBox?: CircleLike;
get circularBoundingBox(): CircleLike {
this._circularBoundingBox = this.calculateCircularBoundingBox();
return this._circularBoundingBox;
}
private calculateCircularBoundingBox() {
let greatestDistance = 0;
for (const p of this.points) {
greatestDistance = Math.max(
@@ -50,13 +57,11 @@ export class Polygon {
_aabb?: axisAlignedBoundingBox;
get AABB(): axisAlignedBoundingBox {
if (!this._aabb) {
this._aabb = this.recalculateAABB();
}
this._aabb = this.recalculateAABB();
return this._aabb;
}
recalculateAABB(): axisAlignedBoundingBox {
private recalculateAABB(): axisAlignedBoundingBox {
let smallestX, biggestX, smallestY, biggestY;
smallestX =
smallestY =
@@ -74,8 +79,8 @@ export class Polygon {
}
return {
x: smallestX,
y: smallestY,
x: smallestX + this.center.x,
y: smallestY + this.center.y,
w: biggestX - smallestX,
h: biggestY - smallestY,
};
@@ -118,6 +123,6 @@ export class Polygon {
for (const point of this.points) {
if (p.dist(point) < p.dist(nearest)) nearest = point;
}
return nearest;
return nearest.copy().add(this.center);
}
}