-
Notifications
You must be signed in to change notification settings - Fork 0
/
box.js
41 lines (41 loc) · 1.68 KB
/
box.js
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
class Box {
constructor(x, y, w, h, showConstrains = false, showVertices = false) {
this.vertices = [];
this.vertices.push(new Vertex(x, y, 3));
this.vertices.push(new Vertex(x + w, y, 3));
this.vertices.push(new Vertex(x + w, y + h, 3));
this.vertices.push(new Vertex(x, y + h, 3));
this.constrains = [];
this.constrains.push(new Constraint(this.vertices[0], this.vertices[1], w));
this.constrains.push(new Constraint(this.vertices[1], this.vertices[2], h));
this.constrains.push(new Constraint(this.vertices[2], this.vertices[3], w));
this.constrains.push(new Constraint(this.vertices[3], this.vertices[0], h));
this.constrains.push(new Constraint(this.vertices[0], this.vertices[2], Math.sqrt(w ** 2 + h ** 2)));
this.constrains.push(new Constraint(this.vertices[1], this.vertices[3], Math.sqrt(w ** 2 + h ** 2)));
this.showConstrains = showConstrains;
this.showVertices = showVertices;
}
update() {
for (let vertex of this.vertices) {
vertex.update();
}
for (let i = 0; i < 5; i++) {
for (let constrain of this.constrains) {
constrain.update();
}
for (let vertex of this.vertices) {
if (vertex.worldBound) { vertex.worldBoundCheck(); }
}
}
if (this.showConstrains) {
for (let constrain of this.constrains) {
constrain.draw();
}
}
if (this.showVertices) {
for (let vertex of this.vertices) {
vertex.draw();
}
}
}
}