-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrocket.js
More file actions
81 lines (65 loc) · 2.1 KB
/
Copy pathrocket.js
File metadata and controls
81 lines (65 loc) · 2.1 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
class Rocket {
constructor(dna) {
this.pos = startingPoint.copy();
this.vel = createVector();
this.acc = createVector();
this.time = lifespan;
this.completed = false;
this.obstaclestuck = false;
this.wallstuck = false;
if(dna) this.dna = dna;
else this.dna = new DNA();
this.fitness = 0;
}
applyForce(force) {
this.acc.add(force);
}
update() {
let d = dist(this.pos.x, this.pos.y, target.x, target.y);
if(d < 30) {
this.completed = true;
this.time = cnt;
this.pos = target.copy();
}
for(let obstacle of obstacles) {
if(this.pos.x > obstacle[0] && this.pos.x < obstacle[0] + obstacle[2] && this.pos.y > obstacle[1] && this.pos.y < obstacle[1] + obstacle[3]) {
this.obstaclestuck = true;
break;
}
}
if(this.pos.x < 0 || this.pos.x > width || this.pos.y < 0 || this.pos.y > height) {
this.wallstuck = true;
}
this.applyForce(this.dna.genes[cnt]);
if(!this.completed && !this.obstaclestuck && !this.wallstuck) {
this.vel.add(this.acc);
this.pos.add(this.vel);
this.acc.mult(0);
}
}
calcFitness() {
let d = dist(this.pos.x, this.pos.y, target.x, target.y);
let d2 = dist(this.pos.x, this.pos.y, startingPoint.x, startingPoint.y);
let reward = 0;
if(this.completed) reward = 1000;
this.fitness = map(d, 0, 800, 800, 0) + 1 / (d+0.01) + (lifespan - this.time)**3 + reward;
if(this.obstaclestuck) {
this.fitness /= 3;
}
if(this.wallstuck) {
// this.fitness /= map(d, 0, 800, 1.01, 3);
this.fitness /= 2;
}
this.fitness += d2;
}
show(color = [255, 255, 255]) {
push();
translate(this.pos.x, this.pos.y);
rotate(this.vel.heading());
noStroke();
fill(...color, 150);
rectMode(CENTER);
rect(0, 0, 20, 7);
pop();
}
}