-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRocket.pde
More file actions
134 lines (110 loc) · 3.29 KB
/
Rocket.pde
File metadata and controls
134 lines (110 loc) · 3.29 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
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
class Rocket {
PVector pos = new PVector(width/2, height -30);
PVector vel = new PVector();
PVector acc = new PVector();
float fitness = 0.00;
Dna dna;
boolean completed;
boolean crashed;
boolean hitbarrier;
float paint;
int time;
Rocket() {
this.dna = new Dna(null);
// Checks if rocket has reached target
this.completed = false;
// Checks if rocket had crashed
this.crashed = false;
this.hitbarrier = false;
this.paint = random(256);
}
Rocket(Dna dna) {
this.dna = dna;
// Checkes rocket has reached target
this.completed = false;
// Checks if rocket had crashed
this.crashed = false;
}
void applyForce(PVector force) {
this.acc.add(force);
}
void calcFitness() {
float d = dist(this.pos.x, this.pos.y, target.x, target.y);
// Flip following 2 lines:
this.fitness *= this.fitness;
this.fitness = map(d, 0, width, width, 0);
this.fitness += (height - this.pos.x)/3;
if (this.completed) {
this.fitness *= 10;
this.fitness *= 5*(lifespan/time);
}
// If rocket does not get to target decrease fitness
if (this.crashed) {
this.fitness /= 6;
}
// hitting the barrier is even worse than hitting the wall
if (this.hitbarrier == true) {
this.fitness /= 2;
}
if (this.pos.y > height*0.7) {
this.fitness /=10;
}
}
void update(int frame) {
// Checks distance from rocket to target
float d = dist(this.pos.x, this.pos.y, target.x, target.y);
// If distance less than 10 pixels, then it has reached target
if (d < 10) {
this.completed = true;
totalHits++;
hitsThisRound++;
this.time = frame;
this.pos = target.copy();
}
// Rocket hit the barrier
for(PVector p : barriers){
if (dist(p.x, p.y, this.pos.x, this.pos.y) < 20) {
this.crashed = true;
this.hitbarrier = true;
}
}
// Rocket has hit left or right of window
if (this.pos.x > width || this.pos.x < 0) {
this.crashed = true;
}
// Rocket has hit top or bottom of window
if (this.pos.y > height || this.pos.y < 0) {
this.crashed = true;
}
if (this.pos.y > height) {
this.hitbarrier = true;
}
//applies the random vectors defined in dna to consecutive frames of rocket
this.applyForce(this.dna.genes[frame]);
// if rocket has not got to goal and not crashed then update physics engine
if (!this.completed && !this.crashed) {
this.vel.add(this.acc);
this.pos.add(this.vel);
this.vel.limit(15);
// only for debugging:
//println("pos:" + this.pos.x + " " + this.pos.y + " vel:" + this.vel.x + " " + this.vel.y + " acc:" + this.acc.x + " " + this.acc.y + " force:" + this.dna.genes[count].x + " " + this.dna.genes[count].y);
this.acc.mult(0);
}
}
void show() {
// push and pop allow's rotating and translation not to affect other objects
pushMatrix();
//color customization of rockets
noStroke();
colorMode(HSB);
fill(paint, 150, 255);
//translate to the postion of rocket
translate(this.pos.x, this.pos.y);
//rotatates to the angle the rocket is pointing
rotate(this.vel.heading());
//creates a rectangle shape for rocket
rectMode(CENTER);
rect(0, 0, 50, 15);
popMatrix();
}
}