-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathparticle.js
More file actions
105 lines (94 loc) · 1.89 KB
/
particle.js
File metadata and controls
105 lines (94 loc) · 1.89 KB
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
class Particle
{
constructor()
{
this.fov = 45;
this.pos = createVector(width/4, height/2);
this.rays = [];
this.offset = 0;
this.heading = 0;
//for all the angles in 360 degree rotation
for(let i = -this.fov/2; i < this.fov/2; i +=1)
{
this.rays.push(new Ray(this.pos, radians(i)));
}
}
updateFOV(fov)
{
this.fov = fov;
this.rays = [];
for(let i = -this.fov/2; i<this.fov/2; i +=1)
{
this.rays.push(new Ray(this.pos, radians(i) + this.heading));
}
}
rotate(angle)
{
this.heading += angle;
let index = 0;
//careful. This loop should have be the same as the loop
//that draws the particle above
for(let i = -this.fov/2; i < this.fov/2; i+=1)
{
this.rays[index].setAngle(radians(i) + this.heading);
index++;
}
}
move(amount)
{
const vel = p5.Vector.fromAngle(this.heading);
vel.setMag(amount);
this.pos.add(vel);
}
update(x, y)
{
this.pos.set(x, y);
}
look(walls)
{
const scene = [];
for(let i = 0; i < this.rays.length; i++)
{
const ray = this.rays[i];
let closest = null;
let record = Infinity;
for(let wall of walls)
{
const pt = ray.cast(wall);
if(pt)
{
let d = p5.Vector.dist(this.pos, pt);
const a = ray.dir.heading() - this.heading;
d *= cos(a);
if(d<record)
{
record = d;
closest = pt;
}
}
}
if(closest)
{
stroke(255, 100);
line(this.pos.x, this.pos.y, closest.x, closest.y);
}
scene[i] = record;
// else
// {
// {
// scene[i] = Infinity;
// }
// }
}
return scene;
}
show()
{
fill(255);
ellipse(this.pos.x, this.pos.y, 4);
for(let ray of this.rays)
{
ray.show();
}
}
}