summaryrefslogtreecommitdiff
path: root/src/engine/components/BoundingBox.ts
blob: d64041f560ea0a45fd6e11192bee893c6c3472af (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
import { Component, ComponentNames } from ".";
import type { Coord2D, Dimension2D } from "../interfaces";
import { dotProduct, rotateVector } from "../utils";

export class BoundingBox extends Component {
  public center: Coord2D;
  public dimension: Dimension2D;
  public rotation: number;

  constructor(center: Coord2D, dimension: Dimension2D, rotation?: number) {
    super(ComponentNames.BoundingBox);

    this.center = center;
    this.dimension = dimension;
    this.rotation = rotation ?? 0;
  }

  public isCollidingWith(box: BoundingBox): boolean {
    // optimization; when neither rotates just check if they overlap
    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++) {
        const [A, B] = [poly[i], poly[(i + 1) % poly.length]];
        const normal: Coord2D = { x: B.y - A.y, y: A.x - B.x };

        const [[minThis, maxThis], [minBox, maxBox]] = boxes.map((box) =>
          box.reduce(
            ([min, max], vertex) => {
              const projection = dotProduct(normal, vertex);
              return [Math.min(min, projection), Math.max(max, projection)];
            },
            [Infinity, -Infinity],
          ),
        );

        if (maxThis < minBox || maxBox < minThis) return false;
      }
    }

    return true;
  }

  public getVertices(): Coord2D[] {
    return [
      { 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)) // rotate
      .map((vertex) => {
        // translate
        return {
          x: vertex.x + this.center.x,
          y: vertex.y + this.center.y,
        };
      });
  }

  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;
  }

  public getOutscribedBoxDims(): Dimension2D {
    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)),
      };
    }

    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)),
    };
  }

  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,
    };
  }
}