-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrocket.js
64 lines (53 loc) · 1.61 KB
/
rocket.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
class Rocket {
constructor(x, y, forces) {
this.pos = createVector(x, y);
this.vel = p5.Vector.random2D();
this.vel.setMag(0.1);
this.acc = createVector();
this.forces = forces;
this.forceCounter = 0;
this.width = 2.5;
this.height = 15;
this.fitness = 0;
this.timeAlive = 0;
this.finished = false;
this.crashed = false;
}
update() {
if(!this.finished && !this.crashed) {
this.vel.add(this.acc);
this.pos.add(this.vel);
this.acc.mult(0);
if(this.forceCounter < this.forces.length) {
this.applyForce(this.forces[this.forceCounter++]);
}
if(this.pos.x > width || this.pos.x < 0 || this.pos.y > height ||
this.pos.y < 0) {
this.crashed = true;
this.fitness *= 0.1;
}
this.timeAlive++;
}
}
applyForce(force) {
this.acc.add(force);
}
collides(boundary) {
if(this.crashed) return;
if(boundary.collidesWith(this.pos.x, this.pos.y)) {
this.finised = true;
this.crashed = true;
this.fitness *= boundary.reward + boundary.reward * (1 / this.timeAlive);
}
}
show(maxFit) {
push();
translate(this.pos.x, this.pos.y);
rotate(this.vel.heading());
rectMode(CENTER);
fill(color(map(this.fitness, 0, maxFit, 0, 255), 50, 100, 150), 150);
noStroke();
rect(0, 0, this.height, this.width);
pop();
}
}