-
Notifications
You must be signed in to change notification settings - Fork 0
/
car.js
140 lines (126 loc) · 4.34 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
class Car {
constructor(x, y, width, height, controlType, maxSpeed=3) {
this.x = x; // x axis
this.y = y; // y axis
this.width = width;
this.height = height;
this.speed = 0;
this.acceleration = 0.2;
this.maxSpeed = maxSpeed;
this.friction = 0.05; // tarcie
this.angle = 0; // for better animation on turn left/right
this.damaged = false;
this.useBrain = controlType == "AI";
if(controlType != "DUMMY") {
this.sensor = new Sensor(this);
this.brain = new NeuralNetwork([this.sensor.rayCount, 6, 4]);
}
this.controls = new Controls(controlType);
}
update(roadBorders, traffic) {
if(!this.damaged) {
this.#move();
this.polygon = this.#createPolygon();
this.damaged = this.#assessDamage(roadBorders, traffic);
}
if(this.sensor) {
this.sensor.update(roadBorders, traffic);
const offsets = this.sensor.readings.map(
s => s == null ? 0 : 1 - s.offset
);
const outputs = NeuralNetwork.feedForward(offsets, this.brain);
if(this.useBrain) {
this.controls.forward = outputs[0];
this.controls.left = outputs[1];
this.controls.right = outputs[2];
this.controls.reverse = outputs[3];
}
}
}
#assessDamage(roadBorders, traffic) {
for(let i = 0; i < roadBorders.length; i++) {
if(polysIntersect(this.polygon, roadBorders[i])) {
return true;
}
}
for(let i = 0; i < traffic.length; i++) {
if(polysIntersect(this.polygon, traffic[i].polygon)) {
return true;
}
}
return false;
}
#createPolygon() {
const points = [];
// redius -> distance from center to sides and corners
const rad = Math.hypot(this.width, this.height) / 2; // hypotenuse / 2
const alpha = Math.atan2(this.width, this.height); // angel
points.push({ // top right point
x: this.x - Math.sin(this.angle - alpha) * rad,
y: this.y - Math.cos(this.angle - alpha) * rad
});
points.push({ //top left point
x: this.x - Math.sin(this.angle + alpha) * rad,
y: this.y - Math.cos(this.angle + alpha) * rad
});
points.push({ // bottom right
x: this.x - Math.sin(Math.PI + this.angle - alpha) * rad,
y: this.y - Math.cos(Math.PI + this.angle - alpha) * rad
});
points.push({ // bottom left
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.controls.forward) {
this.speed += this.acceleration;
}
if(this.controls.reverse) {
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.fraction) {
this.speed = 0;
}
if(this.speed != 0) {
const flip = this.speed > 1 ? 1 : -1;
if(this.controls.left) {
this.angle += 0.03 * flip;
}
if(this.controls.right) {
this.angle -= 0.03 * flip;
}
}
this.x -= Math.sin(this.angle) * this.speed;
this.y -= Math.cos(this.angle) * this.speed;
}
draw(ctx, color, drawSensor = false) {
if(this.damaged) {
ctx.fillStyle = "gray";
} else {
ctx.fillStyle = color;
}
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();
if(this.sensor && drawSensor){
this.sensor.draw(ctx); // car has responsibility to draw his own sensors
}
}
}