-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathray.js
61 lines (55 loc) · 938 Bytes
/
ray.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
class Ray {
constructor(pos, angle) {
this.pos = pos;
this.dir = p5.Vector.fromAngle(angle).setMag(collisionPerception);
}
lookAt(x, y) {
this.dir.x = x - this.pos.x;
this.dir.y = y - this.pos.y;
this.dir.setMag(collisionPerception);
}
cast(wall) {
let hit = collideLineLineVector(
this.pos,
p5.Vector.add(this.pos, this.dir),
wall.a,
wall.b,
true
);
return createVector(hit.x, hit.y);
}
rotate(angle) {
this.dir.rotate(angle);
return this;
}
getLine() {
return {
a: {
x: this.pos.x,
y: this.pos.y
},
b: {
x: this.pos.x + this.dir.x,
y: this.pos.y + this.dir.y
}
}
}
copy() {
return new Ray(this.pos.copy(), this.dir.heading());
}
show(pt) {
if (!pt)
pt = {
x: this.pos.x + this.dir.x,
y: this.pos.y + this.dir.y
};
point(pt.x, pt.y);
strokeWeight(1);
line(
this.pos.x,
this.pos.y,
pt.x,
pt.y
);
}
}