-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRocket.pde
95 lines (85 loc) · 1.64 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
class Rocket
{
PVector pos;
PVector vel;
PVector acc;
PVector moves[];
float fitness;
boolean hit;
boolean crashed;
Rocket(PVector p, int maxMoves, boolean createMoves)
{
hit = false;
crashed = false;
pos = new PVector(p.x, p.y);
vel = new PVector();
acc = new PVector();
moves = new PVector[maxMoves];
for (int i = 0; i < moves.length; i++)
{
moves[i] = new PVector();
}
if (createMoves) generateMoves();
}
void checkGoal(PVector obj)
{
if (pos.dist(obj) < 10)
{
hit = true;
}
}
void checkCollision(Obstacle o)
{
if (2 * pos.dist(o.pos) < o.radius) crashed = true;
if (pos.x < 0 || pos.x > width || pos.y < -50 || pos.y > height) crashed = true;
}
void reset(PVector start)
{
hit = false;
crashed = false;
pos.set(start);
acc.mult(0);
vel.mult(0);
fitness = 0;
}
void generateMoves()
{
for (int i = 0; i < moves.length; i++)
{
moves[i] = PVector.random2D();
}
}
void calculateFitness(PVector goal)
{
// Fitness = 0 if crashed?
if (hit) fitness = 1;
else fitness = 100 / pos.dist(goal);
if (crashed) fitness *= 0.7;
}
void addMoveForce(int i)
{
if (!hit && !crashed)
{
acc.x = moves[i].x;
acc.y = moves[i].y;
}
}
void applyForce()
{
if (!hit && !crashed)
{
vel.x += acc.x;
vel.y += acc.y;
pos.x += vel.x;
pos.y += vel.y;
acc.mult(0);
}
}
void show()
{
if (hit) stroke(0, 200, 0);
else if (crashed) stroke(200, 0, 0);
else stroke(0);
line(pos.x, pos.y, pos.x + vel.x, pos.y + vel.y);
}
}