-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcar.js
More file actions
207 lines (177 loc) · 5.67 KB
/
car.js
File metadata and controls
207 lines (177 loc) · 5.67 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
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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
class Car{
constructor(x,y,width,height,controlType, maxSpeed=6){
// Position
this.x = x;
this.y = y;
// Size
this.width = width;
this.height = height;
// Speed
this.speed = 0;
this.acceleration = 0.2;
this.maxSpeed = maxSpeed;
this.friction = 0.05;
// Angle
this.angle = 0;
this.rotateSpeed = 0.03;
// Car Collision
this.damaged = false;
// Car Control Type
this.controlType = controlType;
// if controlType is AI, useBrain is true
this.useBrain = (controlType == "AI");
// Player Car's Sensor
if(controlType != "DUMMY"){
// Sensors Creation
this.sensor = new Sensor(this);
// NNs Creation
this.brain = new NeuralNetwork(
[
// Input Layer
this.sensor.rayCount,
// Hidden Layer
8,
// Output Layer
4
]
);
}
this.controls = new Controls(controlType);
}
draw(context, showSensors=false){
// Car Color Settings
let color;
if (this.controlType == "DUMMY"){
color = "green";
}
else{
color = "black";
}
// Change PLayer Car's color when damaged
if(this.damaged){
context.fillStyle = 'red';
}
else{
context.fillStyle = color;
}
// Draw Car
context.beginPath();
context.moveTo(this.polygon[0].x, this.polygon[0].y);
for(let i=1; i<this.polygon.length; i++){
context.lineTo(this.polygon[i].x, this.polygon[i].y);
}
context.fill();
// Draw Sensors && Only show sensors for bestCar
if(this.sensor && showSensors){
this.sensor.draw(context);
}
}
update(roadBorders,traffic){
if(!this.damaged){
this.#move();
this.polygon = this.#createPolygon();
this.damaged = this.#accessDamage(roadBorders, traffic);
}
if(this.sensor){
this.sensor.update(roadBorders, traffic);
// Reading is 0 if rays are not intersecting
// Reading is 1-offset if rays are intersecting
const offsets = this.sensor.readings.map(
r => r==null ? 0 : 1-r.offset
);
const outputs = NeuralNetwork.feedForward(offsets, this.brain);
// AI Car's Controls
if(this.useBrain){
this.controls.up = outputs[0];
this.controls.down = outputs[1];
this.controls.left = outputs[2];
this.controls.right = outputs[3];
}
//// Debugger
// console.log(outputs);
}
}
#move(){
//// Up & Down Movement
// Acceleration
if(this.controls.up){
this.speed += this.acceleration;
}
if(this.controls.down){
this.speed -= this.acceleration;
}
// Speed Cap
if(this.speed > this.maxSpeed){
this.speed = this.maxSpeed;
}
if(this.speed < -this.maxSpeed/2){
this.speed = -this.maxSpeed/2;
}
// Friction
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;
}
//// Angle Rotation
// Left & Right
if(this.speed!='0'){ // Only rotate if moving
// Flip the rotation if car goes backwards
const flip = this.speed > 0 ? 1 : -1;
if(this.controls.left){
this.angle += this.rotateSpeed * flip;
}
if(this.controls.right){
this.angle -= this.rotateSpeed * flip;
}
}
//// Update & Move the Car
// Update Car Position
this.x -= Math.sin(this.angle) * this.speed;
this.y -= Math.cos(this.angle) * this.speed;
}
// Create a car polygon for collision detection
#createPolygon(){
const points = [];
const rad = Math.hypot(this.width, this.height)/2; //Hypothenuse
const alpha = Math.atan2(this.width,this.height); // Using Tangent to find Angle of center to corner
// Top Right Corner
points.push({
x: this.x - Math.sin(this.angle - alpha) * rad,
y: this.y - Math.cos(this.angle - alpha) * rad
});
// Top Left Corner
points.push({
x: this.x - Math.sin(this.angle + alpha) * rad,
y: this.y - Math.cos(this.angle + alpha) * rad
});
// Bottom Left Corner
points.push({
x: this.x - Math.sin(Math.PI + this.angle - alpha) * rad,
y: this.y - Math.cos(Math.PI + this.angle - alpha) * rad
});
// Bottom Right Corner
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;
}
#accessDamage(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;
}
}