-
Notifications
You must be signed in to change notification settings - Fork 0
/
Rocket.pde
124 lines (112 loc) · 2.96 KB
/
Rocket.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
117
118
119
120
121
122
123
124
class Rocket {
int lifespan;
float fitness;
PVector pos, vel, acc, target;
DNA dna;
boolean reached, crashed;
color fillcolor = color(255, 150);
Rocket(int lifespan_, PVector target_) {
reached = false;
crashed = false;
lifespan = lifespan_;
fitness = 0;
target = target_;
pos = new PVector(width / 2, height - 20);
vel = new PVector(0, 0);
acc = new PVector(0, 0);
dna = new DNA(lifespan);
}
void calcFitness() {
fitness = 1 / (pos.dist(target) + 0.001); // fitness equals 1 over the distance between rocket and target.
// fitness = map((pos.dist(target)), 0, (dist(0, 0, width, height)), (dist(0, 0, width, height)), 0); // Alternate fitness function.
if (reached) fitness *= 25;
if (crashed) fitness /= 10;
}
void show() {
push();
translate(pos.x, pos.y);
rotate(vel.heading() + (PI / 2));
beginShape(TRIANGLES);
fill(fillcolor);
vertex(0, 0);
vertex(-5, 15);
vertex(5, 15);
endShape(CLOSE);
pop();
//push();
//translate(pos.x, pos.y);
//stroke(255, 150);
//noFill();
//rectMode(CENTER);
//rotate(vel.heading() + (PI / 2));
//rect(0, 0, 10, 50);
//pop();
}
void update() {
applyForce(dna.genes[timer]);
vel.add(acc);
pos.add(vel);
acc.mult(0);
}
void applyForce(PVector force) {
acc.add(force);
}
void checkMotion() {
if (pos.x > width || pos.x < 0 || pos.y > height || pos.y < 0) {
crashed = true;
fillcolor = color(255, 0, 0, 150);
}
// Check if the rocket hit an obstacle or not:
for (Obstacle obs : obstacles) {
if (obs.yi < obs.yf) {
if (pos.y > obs.yi && pos.y < obs.yf) {
if (obs.xi < obs.xf) {
if (pos.x > obs.xi && pos.x < obs.xf) {
crashed = true;
fillcolor = color(255, 0, 0, 150);
}
} else {
if (pos.x < obs.xi && pos.x > obs.xf) {
crashed = true;
fillcolor = color(255, 0, 0, 150);
}
}
}
} else {
if (pos.y < obs.yi && pos.y > obs.yf) {
if (obs.xi < obs.xf) {
if (pos.x > obs.xi && pos.x < obs.xf) {
crashed = true;
fillcolor = color(255, 0, 0, 150);
}
} else {
if (pos.x < obs.xi && pos.x > obs.xf) {
crashed = true;
fillcolor = color(255, 0, 0, 150);
}
}
}
}
}
if (pos.dist(target) <= 10) {
pos = target;
reached = true;
fillcolor = color(0, 255, 0, 150);
}
}
void reset() {
reached = false;
crashed = false;
fitness = 0;
pos = new PVector(width / 2, height - 20);
vel = new PVector(0, 0);
acc = new PVector(0, 0);
fillcolor = color(255, 150);
}
boolean reached() {
return (pos.dist(target) <= 10);
}
boolean crashed() {
return (pos.x > width || pos.x < 0 || pos.y > height || pos.y < 0);
}
}