-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathobstacle.js
148 lines (135 loc) · 3.19 KB
/
obstacle.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
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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
class Wall {
constructor(x1, y1, x2, y2) {
this.a = createVector(x1, y1);
this.b = createVector(x2, y2);
}
split(hit) {
// console.log(hit);
let x, y;
let {left, top, right, bottom} = hit;
if (left.x) {
x = left.x;
y = left.y;
}
if (top.x) {
x = top.x;
y = top.y;
}
if (right.x) {
x = right.x;
y = right.y;
}
if (bottom.x) {
x = bottom.x;
y = bottom.y;
}
if (!x || !y) return;
//the to object is to get the endpoints of the segment off of the boundary edge
let to = {
x: this.a.x - x > 0 ? .001 : -.001,
y: this.a.y - y > 0 ? .001 : -.001,
};
return [
new Wall(this.a.x, this.a.y, x + to.x, y + to.y),
new Wall(this.b.x, this.b.y, x - to.x, y - to.y)
]
}
show() {
stroke(255);
strokeWeight(2);
line(this.a.x, this.a.y, this.b.x, this.b.y);
}
highlight() {
stroke(0, 255, 0);
strokeWeight(2);
line(this.a.x, this.a.y, this.b.x, this.b.y);
}
}
class Obstacle {
constructor() {
this.pos = createVector(random(100, width-100), random(100, height-100));
this.r = random(10, 50);
this.total = random(5, 15);
this.offset = [];
for (let i=0; i<this.total; i++) {
this.offset[i] = random(-10, 10);
}
this.triangles = [];
for (let i=0; i<this.total-1; i++) {
let angle2 = map(i, 0, this.total, 0, TWO_PI);
let r2 = this.r + this.offset[i];
let x2 = r2 * cos(angle2) + this.pos.x;
let y2 = r2 * sin(angle2) + this.pos.y;
let angle3 = map(i+1, 0, this.total, 0, TWO_PI);
let r3 = this.r + this.offset[i+1];
let x3 = r3 * cos(angle3) + this.pos.x;
let y3 = r3 * sin(angle3) + this.pos.y;
this.triangles.push({
x1: this.pos.x,
y1: this.pos.y,
x2,
y2,
x3,
y3
});
}
this.triangles.push({
x1: this.pos.x,
y1: this.pos.y,
x2: this.triangles[this.triangles.length-1].x3,
y2: this.triangles[this.triangles.length-1].y3,
x3: this.triangles[0].x2,
y3: this.triangles[0].y2
})
}
getBoundaries() {
let boundaries = [];
for (let i=0; i<this.total-1; i++) {
let angle1 = map(i, 0, this.total, 0, TWO_PI);
let r1 = this.r + this.offset[i];
let x1 = r1 * cos(angle1) + this.pos.x;
let y1 = r1 * sin(angle1) + this.pos.y;
let angle2 = map(i+1, 0, this.total, 0, TWO_PI);
let r2 = this.r + this.offset[i+1];
let x2 = r2 * cos(angle2) + this.pos.x;
let y2 = r2 * sin(angle2) + this.pos.y;
boundaries.push(new Wall(x1, y1, x2, y2));
}
boundaries.push(new Wall(
boundaries[boundaries.length-1].b.x,
boundaries[boundaries.length-1].b.y,
boundaries[0].a.x,
boundaries[0].a.y
));
return boundaries;
}
// update() {
// }
show() {
beginShape();
for (let i=0; i< this.total; i++) {
let angle = map(i, 0, this.total, 0, TWO_PI);
let r = this.r + this.offset[i];
let x = r * cos(angle) + this.pos.x;
let y = r * sin(angle) + this.pos.y;
vertex(x, y);
}
endShape(CLOSE);
}
highlight(t) {
push();
strokeWeight(1);
fill(0, 255, 0, 50);
stroke(0, 255, 0);
triangle(t.x1, t.y1, t.x2, t.y2, t.x3, t.y3);
pop();
}
showTriangles() {
push();
strokeWeight(1);
fill(0, 255, 0, 50);
stroke(0, 255, 0);
this.triangles.forEach(t => triangle(t.x1, t.y1, t.x2, t.y2, t.x3, t.y3));
pop();
}
}