-
Notifications
You must be signed in to change notification settings - Fork 0
/
Dot.pde
116 lines (105 loc) · 2.31 KB
/
Dot.pde
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
class Dot {
boolean alive = true;
boolean reached = false;
boolean isBest = false;
PVector pos;
PVector vel;
PVector acc;
float r;
DNA dna;
int dnaLength = 1000;
float cap = 0.5;
int i = 0;
boolean isTarget = false;
float fitness;
Dot(Dot d){
pos = new PVector(width / 2, height - 50);
vel = new PVector(0, 0);
acc = new PVector(0, 0);
r = d.r;
dna = new DNA(d.dna);
dnaLength = d.dnaLength;
cap = d.cap;
isTarget = false;
}
Dot(int x, int y, float r, boolean isTarget){
this.isTarget = isTarget;
dna = new DNA(dnaLength, cap);
pos = new PVector(x, y);
vel = new PVector(0, 0);
acc = new PVector(0, 0);
this.r = r;
}
Dot(int x, int y, float r, boolean isTarget, DNA passedDNA){
this.isTarget = isTarget;
dna = new DNA(passedDNA);
pos = new PVector(x, y);
vel = new PVector(0, 0);
acc = new PVector(0, 0);
this.r = r;
}
void update(){
if(pos.x > width - r || pos.x < r || pos.y > height - r || pos.y < r){
die();
}else if(this.isTouching(target)){
this.reached = true;
die();
}else if(touchingObstacle()){
die();
}
if(i < dnaLength){
acc.add(dna.list[i++]);
}else{
die();
}
vel.add(acc);
vel.limit(5);
pos.add(vel);
acc.setMag(0);
}
boolean touchingObstacle(){
if(pos.y > 300 && pos.y < 310 && pos.x > 0 && pos.x < width - 300){
return true;
}else if(pos.y > 400 && pos.y < 410 && pos.x > 300 && pos.x < width){
return true;
}else if(pos.y > 500 && pos.y < 510 && pos.x > 0 && pos.x < width - 300){
return true;
}
return false;
}
float dist(Dot d){
return this.pos.dist(d.pos);
}
void die(){
calcFitness();
alive = false;
}
boolean isTouching(Dot d){
if(this.dist(d) < this.r + d.r){
return true;
}else{
return false;
}
}
void calcFitness(){
if(this.reached){
fitness = 0.01 + (1.0 / (i * i));
}else{
fitness = 1 / (this.dist(target) * this.dist(target));
}
}
void applyForce(PVector p){
acc.add(p);
}
void display(){
fill(255, 100);
if(isBest){
fill(255, 46, 76);
}
if(isTarget){
fill(252, 215, 127);
}
stroke(100);
ellipse(pos.x, pos.y, 2 * r, 2 * r);
}
}