-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathsketch.js
executable file
·172 lines (148 loc) · 3.51 KB
/
sketch.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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
let myCar
const myWalls = []
let showDistances
function setup() {
createCanvas(800, 600)
showDistances = createCheckbox('Distances', false)
myCar = new Car(createVector(width / 2, height / 2))
for (let i = 0; i < 10; i++) {
myWalls.push(new Wall(createVector(random(width), random(height)), createVector(random(width), random(height))))
}
}
function draw() {
background(0)
myCar.update()
myCar.draw()
strokeWeight(2)
stroke(240)
for (let w of myWalls) {
w.draw()
}
}
class Car {
constructor(pos) {
this.pos = pos
this.vel = createVector()
this.acc = createVector()
this.heading = 0
this.lidar = []
for (let angle = 0; angle < TWO_PI; angle += TWO_PI / 8) {
this.lidar.push(new Ray(angle))
}
}
draw() {
stroke(0, 148, 255) // I'm blue
strokeWeight(8)
point(this.pos.x, this.pos.y)
strokeWeight(4)
let vDir = p5.Vector.fromAngle(this.heading)
vDir.setMag(10)
vDir.add(this.pos)
line(this.pos.x, this.pos.y, vDir.x, vDir.y)
}
update() {
let friction = this.vel.copy()
friction.mult(-0.1)
this.acc = createVector(0, 0)
this.acc.add(friction)
if (keyIsPressed) {
if (key == 'ArrowUp') {
this.accelerate()
}
else if (key == 'ArrowDown') {
this.decelerate()
}
else if (key == 'ArrowRight') {
this.rotateRight()
}
else if (key == 'ArrowLeft') {
this.rotateLeft()
}
}
this.vel.add(this.acc)
this.pos.add(this.vel)
// update ray orientation, and cast/draw them !
push()
stroke(255, 120, 120)
fill(255, 120, 120)
strokeWeight(1)
for (let r of this.lidar) {
r.update(this.pos, this.heading)
r.cast()
}
pop()
}
accelerate() {
this.acc = p5.Vector.fromAngle(this.heading)
this.acc.mult(0.1)
}
decelerate() {
this.acc = p5.Vector.fromAngle(this.heading)
this.acc.mult(-0.1)
}
rotateLeft() {
this.rotate(-0.1)
}
rotateRight() {
this.rotate(0.1)
}
rotate(offset) {
this.heading += offset
this.acc.rotate(offset)
this.vel.rotate(offset)
}
}
class Wall {
constructor(start, end) {
this.start = start
this.end = end
}
draw() {
line(this.start.x, this.start.y, this.end.x, this.end.y)
}
}
class Ray {
constructor(angle) {
this.angle = angle
}
update(start, relativeAngle) {
let end = p5.Vector.fromAngle(relativeAngle + this.angle)
end.setMag(50)
end.add(start)
this.start = start
this.end = end
}
draw() {
line(this.start.x, this.start.y, this.end.x, this.end.y)
}
cast() {
const r = p5.Vector.sub(this.end, this.start)
let closest = width
for (let w of myWalls) {
// a + t*r = c + u*s
// a*r = c*r+u*s*r
// u = (a - c)*r /s*r
// a*s + t*r*s = c*s
// t = c-a * s / r*s
const s = p5.Vector.sub(w.end, w.start)
const t = cross2d(p5.Vector.sub(w.start, this.start), s) / cross2d(r, s)
const u = cross2d(p5.Vector.sub(this.start, w.start), r) / cross2d(s, r)
if (t >= 0 && u >= 0 && u < 1 && t < closest) {
closest = t
}
}
r.mult(closest)
const end = p5.Vector.add(this.start, r)
if (showDistances.checked()) {
stroke(255 - r.mag(), r.mag(), r.mag())
}
line(this.start.x, this.start.y, end.x, end.y)
if (showDistances.checked()) {
fill(255 - r.mag(), r.mag(), r.mag())
text(Math.floor(r.mag()), end.x, end.y)
}
}
}
function cross2d(v1, v2) {
return v1.x * v2.y - v2.x * v1.y
}