-
Notifications
You must be signed in to change notification settings - Fork 0
/
emitter.js
56 lines (55 loc) · 1.66 KB
/
emitter.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
class emitter{
constructor(x, y){
this.pos = createVector(x,y);
this.rays = [];
this.casts = [];
for(let i=0; i < 360; i += 1){
this.rays.push(new Ray(this.pos, p5.Vector.fromAngle(radians(i))))
}
}
update(x,y){
this.pos.set(x,y);
this.casts = [];
}
show(plains){
this.casting(plains);
let last;
this.casts.forEach(cast =>{
fill(255,255,255,100)
stroke(255,255,255,0)
//line(this.pos.x,this.pos.y,cast.x,cast.y)
if (last){
console.log[cast[3]]
triangle(this.pos.x, this.pos.y, cast[0], cast[1], last[0], last[1])
} else {
triangle(this.pos.x, this.pos.y, cast[0], cast[1], this.casts[this.casts.length -1][0], this.casts[this.casts.length -1][1])
}
last = cast;
});
}
casting(plains){
this.casts = [];
//console.log(plains)
for (let i = 0; i < this.rays.length; i++) {
const ray = this.rays[i];
let closest = null;
let angle = null;
let record = Infinity;
for (let plain of plains) {
const pt = ray.cast(plain);
if (pt) {
let d = p5.Vector.dist(this.pos, pt);
if (d < record) {
record = d;
closest = pt;
angle = ray.angle;
}
}
}
if (closest) {
stroke(255, 100);
this.casts.push([closest.x, closest.y, [angle.x, angle.y]]);
}
}
}
}