-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathplayer.js
More file actions
119 lines (91 loc) · 3.4 KB
/
Copy pathplayer.js
File metadata and controls
119 lines (91 loc) · 3.4 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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
class Player {
constructor() {
this.position = createVector(
gTerrain.width * 0.2,
gTerrain.height / 2
);
this.acceleration = createVector();
this.velocity = createVector();
this.isDead = false;
// amount of frames the player was alive
this.framesAlive = 0;
this.referenceMaxVelocity = 3;
// make radius depend on terrain height (-> dynamic)
this.radius = gTerrain.height * 0.03;
// amount of sensors of each player
this.sensorAmount = 12;
// sensors that measure distance between player and terrain / obstacles
this.sensors = [];
// current calculated fitness of the player
this.fitness = 0;
/*
input: amount of sensors and x/y velocity
output: x/y acceleration
*/
this.inputNeurons = this.sensorAmount + 2;
this.hiddenNeurons = [10, 7];
this.outputNeurons = 2;
// neural network that serves as the "brain"
this.neuralNet = new biolib.Perceptron(this.inputNeurons, ...this.hiddenNeurons, this.outputNeurons);
this.neuralNet.setActivationFunction(biolib.Activation.TANH);
this.initSensors();
}
initSensors() {
let directions = Utilities.getCircumferencePoints(0, 0, 1, this.sensorAmount);
for (let i = 0; i < directions.length; i++) {
// increase direction length to reduce processing power
directions[i].mult(3);
this.sensors.push(new Sensor(directions[i].x, directions[i].y));
}
}
show() {
fill(225, 99, 72);
stroke(182, 78, 56);
strokeWeight(4);
ellipse(this.position.x, this.position.y, this.radius * 2, this.radius * 2);
ellipse(this.position.x, this.position.y + this.radius);
this.showSensors();
}
showSensors() {
if (!this.isDead) {
for (let i = 0; i < this.sensors.length; i++) {
//this.sensors[i].show();
}
}
}
update() {
// don't do anything if player is dead
if (this.isDead) {
this.position.x -= gTerrain.velocity;
return;
}
this.updateSensors();
this.updateAcceleration();
this.velocity.add(this.acceleration);
this.position.add(this.velocity);
this.framesAlive++;
// check for terrain / obstacle collisions
if (gTerrain.collidesCircle(this.position.x, this.position.y, this.radius)) {
this.isDead = true;
}
}
updateSensors() {
for (let i = 0; i < this.sensors.length; i++) {
this.sensors[i].update(this.position.x, this.position.y);
}
}
updateAcceleration() {
let input = [];
for (let i = 0; i < this.sensors.length; i++) {
input[i] = map(pow(this.sensors[i].distance, 2), 0, pow(gTerrain.height, 2), -1, 1);
}
input.push(map(pow(this.velocity.x, 3), -pow(this.referenceMaxVelocity, 3), pow(this.referenceMaxVelocity, 3), -1, 1));
input.push(map(pow(this.velocity.y, 3), -pow(this.referenceMaxVelocity, 3), pow(this.referenceMaxVelocity, 3), -1, 1));
let output = this.neuralNet.feedForward(input);
this.acceleration.x = output[0];
this.acceleration.y = output[1];
}
calculateFitness() {
this.fitness = pow(this.framesAlive, 3);
}
}