-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathRockets.js
More file actions
100 lines (87 loc) · 2.3 KB
/
Rockets.js
File metadata and controls
100 lines (87 loc) · 2.3 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
// José Bezerra - 21/07/2018
// josebezerraneto@outlook.com
function Rocket() {
this.pos = createVector(width / 2, height);
this.vel = createVector();
this.acc = createVector();
this.dna = new DNA();
this.fitness = 0;
this.crashed = false;
this.completed = false;
}
Rocket.prototype.crossover = function (partner) {
var newDNA = this.dna.crossover(partner.dna);
var child = new Rocket();
child.dna = newDNA;
return child;
}
Rocket.prototype.calDist = function () {
var d = dist(this.pos.x, this.pos.y, target.x, target.y);
var fitness = map(d, height, 0, 0, 1);
if (this.completed) {
fitness *= 10;
}
if (this.crashed) {
fitness /= 10;
}
// make the fitness function exponencial
fitness = pow(fitness,3);
this.fitness = fitness;
}
//Graphic Implementations
Rocket.prototype.applyForce = function (force) {
this.acc.add(force);
}
Rocket.prototype.update = function () {
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 > width || this.pos.x < 0) {
this.crashed = true;
}
if (this.pos.y > height || this.pos.y < 0) {
this.crashed = true;
}
// Obstacle Logic
if (mxBegin - mxEnd < 0){ //Negative number
var temp = mxBegin;
mxBegin = mxEnd;
mxEnd = temp;
}
if (myBegin - myEnd < 0){ //Negative number
var temp2 = myBegin;
myBegin = myEnd;
myEnd = temp2;
}
if( (this.pos.x > mxEnd && this.pos.x < mxBegin) && (this.pos.y > myEnd && this.pos.y < myBegin) ){
this.crashed = true;
}
if (!this.crashed && !this.completed) {
this.applyForce(this.dna.genes[count]);
this.vel.add(this.acc);
this.pos.add(this.vel);
this.acc.mult(0);
this.vel.limit(4);
}
}
Rocket.prototype.show = function () {
if(this.completed){
fill(0,255,0,210);
}
if(this.crashed){
fill(255,0,0,210);
}
else if(!this.completed && !this.crashed){
fill(255,255,255,210);
}
noStroke();
// moviments
push();
translate(this.pos.x, this.pos.y);
rotate(this.vel.heading());
rectMode(CENTER);
rect(0, 0, 25, 5,0,20,20,0);
pop();
}