-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAgent.js
85 lines (72 loc) · 2.76 KB
/
Agent.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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
class Agent {
constructor(index) {
this.position = createVector(25, CANVAS_HEIGHT / 2);
this.direction = createVector(0, 0);
this.leftAntenna;
this.rightAntenna;
this.rotation = 0;
this.radius = 30;
this.alive = true;
this.index = index;
this.score = 0;
this.antennaLength = 200;
}
collide(lines) {
if (this.alive) {
this.score++;
// collider
for (var i in lines) {
if (collideLineCircleVector(lines[i][0], lines[i][1], this.position, this.radius)) {
this.kill();
deathPoints.push(this.position);
break;
} else if (time > TIME) {
this.kill();
break;
}
}
}
}
move(lines) {
this.direction.x = cos(this.rotation);
this.direction.y = sin(this.rotation);
if (this.alive) {
this.position = p5.Vector.add(this.position, this.direction);
this.leftAntenna = createVector(cos(this.rotation - 35) * this.antennaLength + this.position.x, sin(this.rotation - 25) * this.antennaLength + this.position.y);
this.rightAntenna = createVector(cos(this.rotation + 35) * this.antennaLength + this.position.x, sin(this.rotation + 25) * this.antennaLength + this.position.y);
// check if the Antenna is colliding with line and then calculate the distance
var shortestDistLeft = 1;
var shortestDistRight = 1;
for (var i in lines) {
var distanceLeft = 1;
if (collideLineLineVector(lines[i][0], lines[i][1], this.position, this.leftAntenna)) {
var collisionPoint = getIntersectionPoint(lines[i][0], lines[i][1], this.position, this.leftAntenna);
circle(collisionPoint.x, collisionPoint.y, 5);
distanceLeft = this.position.dist(collisionPoint) / this.antennaLength;
shortestDistLeft = min(distanceLeft, shortestDistLeft);
}
var distanceRight = 1;
if (collideLineLineVector(lines[i][0], lines[i][1], this.position, this.rightAntenna)) {
var collisionPoint = getIntersectionPoint(lines[i][0], lines[i][1], this.position, this.rightAntenna);
circle(collisionPoint.x, collisionPoint.y, 5);
distanceRight = this.position.dist(collisionPoint) / this.antennaLength;
shortestDistRight = min(distanceRight, shortestDistRight);
}
}
neat.population[this.index].activate([shortestDistLeft, shortestDistRight]) > 0.8 ? this.rotation++ : this.rotation--;
}
}
draw() {
if (this.alive) {
line(this.position.x, this.position.y, this.rightAntenna.x, this.rightAntenna.y);
line(this.position.x, this.position.y, this.leftAntenna.x, this.leftAntenna.y);
circle(this.position.x, this.position.y, this.radius);
}
}
kill() {
this.alive = false;
agentsAlive--;
pointsLastGen.push(this.position.x*0.1);
neat.population[this.index].score = this.position.x; // give Score to Brain
}
}