-
Notifications
You must be signed in to change notification settings - Fork 0
/
car.js
154 lines (131 loc) · 4.04 KB
/
car.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
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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
class Car {
constructor(
x,
y,
width,
height,
controlType,
maxspeed = 7,
sensorCount = 5,
sensorSpread = 4,
neurons = [sensorCount, 6, 4],
color = getColor(),
memorySize = 500
) {
this.x = x;
this.y = y;
this.width = width;
this.height = height;
this.canMove = true;
this.speed = 0.0;
this.acceleration = 0.2;
this.friction = 0.03;
this.maxSpeed = maxspeed;
this.angle = 0;
this.damaged = false;
this.useBrain = controlType == "AI";
this.polygon = this.#createPolygon();
this.position = 0;
if (controlType === "KEY" || controlType === "AI") {
this.sensors = new Sensors(this, sensorCount, sensorSpread);
// this.brain = new NeuralNetwork(neurons); // 6 for hiddenlayer count 6
this.brain = new LearningModel(this.sensorCount, 4, memorySize);
this.score = 0;
}
this.control = new Controls(controlType);
this.score = 0;
this.color = color;
this.state = Array(10).fill(0)
}
draw(ctx, sensorDrawing = false, opacity = 1) {
ctx.globalAlpha = opacity;
if (this.damaged) {
ctx.fillStyle = "gray";
} else {
ctx.fillStyle = this.color;
}
if (this.sensors && sensorDrawing) {
this.sensors.draw(ctx);
}
ctx.beginPath();
ctx.moveTo(this.polygon[0].x, this.polygon[0].y);
for (let i = 1; i < this.polygon.length; ++i) {
ctx.lineTo(this.polygon[i].x, this.polygon[i].y);
}
ctx.fill();
}
update(roadBorders, traffic, isDummy, startingLine) {
if (this.canMove && !this.damaged) {
this.#move();
this.polygon = this.#createPolygon();
if(!isDummy) {
this.damaged = this.#assesDamage(roadBorders, traffic.cars);
this.score = this.#findScore(traffic, startingLine)
}
}
if (this.sensors) {
this.sensors.update(roadBorders, traffic.cars);
this.state = this.sensors.readings.map((r) =>
r == null ? 0 : 1 - r.offset
);
}
}
#findScore(traffic, startingLine){
this.position = traffic.begin - find(traffic.yCoords, this.y)
return this.position - Math.floor(this.y - startingLine);
}
#assesDamage(roadBorders, traffic) {
for (let i = 0; i < roadBorders.length; ++i) {
if (polyIntersect(this.polygon, roadBorders[i])) {
return true;
}
}
for (let i = 0; i < traffic.length; ++i) {
for(let j = 0 ; j < traffic[i].length ; ++j){
let car = traffic[i][j]
if (!(this === car) &&polyIntersect(this.polygon, car.polygon)) {
return true;
}
}
}
return false;
}
#createPolygon() {
const points = [];
const rad = Math.hypot(this.width, this.height) / 2;
const alpha = Math.atan2(this.width, this.height);
points.push({
x: this.x - Math.sin(this.angle - alpha) * rad,
y: this.y - Math.cos(this.angle - alpha) * rad,
});
points.push({
x: this.x - Math.sin(this.angle + alpha) * rad,
y: this.y - Math.cos(this.angle + alpha) * rad,
});
points.push({
x: this.x - Math.sin(Math.PI + this.angle - alpha) * rad,
y: this.y - Math.cos(Math.PI + this.angle - alpha) * rad,
});
points.push({
x: this.x - Math.sin(Math.PI + this.angle + alpha) * rad,
y: this.y - Math.cos(Math.PI + this.angle + alpha) * rad,
});
return points;
}
#move() {
if (this.control.forward) {
this.speed += this.acceleration;
}
if (this.control.backward) this.speed -= this.acceleration;
if (this.speed > this.maxSpeed) this.speed = this.maxSpeed;
if (this.speed < -this.maxSpeed / 2) this.speed = -this.maxSpeed / 2;
if (this.speed > 0) this.speed -= this.friction;
if (this.speed < 0) this.speed += this.friction;
if (Math.abs(this.speed) < this.friction) this.speed = 0;
const flip = this.speed < 0 ? -1 : 1;
if (this.control.right) this.angle -= 0.03 * flip;
if (this.control.left) this.angle += 0.03 * flip;
this.x -= Math.sin(this.angle) * this.speed;
this.y -= Math.cos(this.angle) * this.speed;
}
}