-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathAgent.js
More file actions
73 lines (69 loc) · 2 KB
/
Copy pathAgent.js
File metadata and controls
73 lines (69 loc) · 2 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
class Agent {
constructor(i) {
this.isTouchingGround = false; // back Weel
this.alive = true;
this.index = i;
}
addToWorld() {
this.rect = Bodies.rectangle(300, 300, 100, 40, {
id: this.index * 3,
collisionFilter: {
group: -1
},
});
this.circle = Bodies.circle(260, 320, 20, {
id: this.index * 3 + 1,
friction: 1,
collisionFilter: {
group: -1
}
});
this.circle2 = Bodies.circle(340, 320, 20, {
id: this.index * 3 + 2,
friction: 1,
collisionFilter: {
group: -1
}
});
this.constr1 = Matter.Constraint.create({
bodyA: this.rect,
pointA: { x: -40, y: 20 }, // offset!
bodyB: this.circle,
stiffness: 1,
length: 0
});
this.constr2 = Matter.Constraint.create({
bodyA: this.rect,
pointA: { x: 40, y: 20 },
bodyB: this.circle2,
stiffness: 1,
length: 0
})
World.add(world, [
this.rect, this.circle, this.circle2, this.constr1, this.constr2
]);
}
run() {
if (this.alive) {
if (this.rect.position.x < 0 || this.rect.position.x > 11500 || this.rect.position.y > 1000) { this.kill(); } else {
var vertex1 = verticies[Math.round((this.rect.position.x - 250) / 200) + 3];
var vertex2 = verticies[Math.round((this.rect.position.x - 250) / 200) + 4];
var m = (vertex1.y - vertex2.y) / (vertex2.x - vertex1.x);
var output = neat.population[this.index].activate([this.rect.speed / 100, Math.sin(this.rect.angle), this.isTouchingGround, m]);
if (output[0] > 0.5 && this.circle.angularVelocity < 1.5) Matter.Body.setAngularVelocity(this.circle, this.circle.angularVelocity += 0.2);
if (output[1] > 0.5 && this.circle.angularVelocity > -1.5) Matter.Body.setAngularVelocity(this.circle, this.circle.angularVelocity -= 0.2);
}
}
}
kill() {
if (this.alive) {
neat.population[this.index].score = this.rect.position.x;
scoresThisRound.push(this.rect.position.x);
this.alive = false;
World.remove(world, [
this.rect, this.circle, this.circle2, this.constr1, this.constr2
]);
agentsAlive--;
}
}
}