-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBubbles.js
86 lines (66 loc) · 1.88 KB
/
Bubbles.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
class Bubble{
constructor(dna){
this.pos = createVector(30, height/2);
this.vel = createVector();
this.acc = createVector();
this.completed = false;
this.crashed = false;
if(dna){
this.dna = dna;
} else {
this.dna = new DNA();
}
this.fitness = 0;
}
applyForce(force){
this.acc.add(force);
}
calcFitness(){
var d = dist(this.pos.x, this.pos.y, target.x, target.y);
this.fitness = map(d, 0, width, width, 0) - (count / (lifespan * 7));
if(this.completed){
this.fitness *= 3;
}
if(this.crashed){
this.fitness /= 12;
}
}
update(){
var d = dist(this.pos.x, this.pos.y, target.x, target.y);
if(d < 10){
this.completed = true;
this.pos = target.copy();
}
if(this.pos.x > obstacle[0].rx && this.pos.x < obstacle[0].rx + obstacle[0].rw && this.pos.y > obstacle[0].ry && this.pos.y < obstacle[0].ry + obstacle[0].rh){
this.crashed = true;
}
if(this.pos.x > obstacle[1].rx && this.pos.x < obstacle[1].rx + obstacle[1].rw && this.pos.y > obstacle[1].ry && this.pos.y < obstacle[1].ry + obstacle[1].rh){
this.crashed = true;
}
if(this.pos.x > obstacle[2].rx && this.pos.x < obstacle[2].rx + obstacle[2].rw && this.pos.y > obstacle[2].ry && this.pos.y < obstacle[2].ry + obstacle[2].rh){
this.crashed = true;
}
if(this.pos.x > width || this.pos.x < 0){
this.crashed = true;
}
if(this.pos.y > height || this.pos.y < 0){
this.crashed = true;
}
this.applyForce(this.dna.genes[count]);
if(!this.completed && !this.crashed){
this.vel.add(this.acc);
this.pos.add(this.vel);
this.acc.mult(0.1);
this.vel.limit(4);
}
}
show(){
push();
noStroke();
fill(0, 255, 255, 150);
translate(this.pos.x, this.pos.y);
rotate(this.vel.heading());
ellipse(0, 0, 5, 5);
pop();
}
}