-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrocket.js
63 lines (53 loc) · 1.75 KB
/
rocket.js
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
class Rocket {
constructor(x, y) {
this.position = createVector(x, y);
this.acceleration = createVector();
this.velocity = createVector();
this.dna = new DNA();
this.dnaCounter = 0;
this.crashed = false;
this.finished = false;
this.fitness = 0;
}
applyForce(force) {
this.acceleration.add(force);
}
calculateFitness() {
// += because of punishemnt function when rocket gets out of canvas
this.fitness = 2000 - Math.sqrt(Math.pow(this.position.x - target.x, 2) + Math.pow(this.position.y - target.y, 2));
if (this.crashed)
this.fitness *= 0.1;
else if (this.finished)
this.fitness += 100000;
}
// Physics (movement) for rocket
tick() {
if (!this.crashed && !this.finished) {
this.velocity.add(this.acceleration);
this.position.add(this.velocity);
this.acceleration.mult(0);
this.velocity.setMag(3);
}
if (!this.finished && Math.pow(this.position.x - target.x, 2) + Math.pow(this.position.y - target.y, 2) <= Math.pow(target.r, 2)) {
this.finished = true;
numOfHits++; // Global variable from main.js
}
if (this.position.x < 0 || this.position.x > CANVAS_WIDTH ||
this.position.y < 0 || this.position.y > CANVAS_HEIGHT) {
this.crashed = true;
}
}
draw() {
let theta = this.velocity.heading() + radians(90);
fill(255);
push();
translate(this.position.x, this.position.y);
rotate(theta);
beginShape();
vertex(0, -15);
vertex(-5, 15);
vertex(5, 15);
endShape(CLOSE);
pop();
}
};