Skip to content

Commit

Permalink
Inlcude p5.js library locally, for faster page loading
Browse files Browse the repository at this point in the history
Improve death system, so that when I implement neuro-evolution
it works better
Change return value of the Car.raycast(track) method, so that
it doesn't have to calculate the distance twice
Add the drawRaycasts option
  • Loading branch information
FintasticMan committed Dec 2, 2020
1 parent d4dd683 commit 6328702
Show file tree
Hide file tree
Showing 7 changed files with 93,108 additions and 33 deletions.
30 changes: 18 additions & 12 deletions car.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ class Car {
this.theta = atan(this.w / this.l);
this.hyp = sqrt(sq(this.l / 2) + sq(this.w / 2));

this.alive = true;
this.pos = pos;
this.rotation = rot;
this.direction = [];
Expand All @@ -20,7 +21,7 @@ class Car {
this.direction.push(0);
}

this.raycasts = 7;
this.raycasts = 9;
this.fov = PI;
this.rays = [];

Expand Down Expand Up @@ -50,7 +51,7 @@ class Car {
rect(0, 0, this.l, this.w);
pop();
}

isColliding(track) {
// Calculate all four corners of the car

Expand Down Expand Up @@ -116,6 +117,7 @@ class Car {
}

let casts = [];
let distances = [];

// Cycle through all of the rays
for (let i = 0; i < this.rays.length; i++) {
Expand Down Expand Up @@ -153,31 +155,35 @@ class Car {
// the closest to the car
for (let j = 0; j < intersections.length; j++) {

let distanceCurrent = sqrt(sq(intersections[j].x - this.pos.x) + sq(intersections[j].y - this.pos.y));
if (j === 0) {
casts.push(intersections[j]);
distances.push(distanceCurrent);
} else {
let distanceCurrent = sqrt(sq(intersections[j].x - this.pos.x) + sq(intersections[j].y - this.pos.y));
let distanceSmallest = sqrt(sq(casts[i].x - this.pos.x) + sq(casts[i].y - this.pos.y));

if (distanceCurrent < distanceSmallest) {
casts[i] = intersections[j]
casts[i] = intersections[j];
distances[i] = distanceCurrent;
}
}

}
}

// Draw the rays
for (let i = 0; i < casts.length; i++) {
push();
stroke(255);
strokeWeight(2);
line(this.pos.x, this.pos.y, casts[i].x, casts[i].y);
pop();
if (drawRaycasts) {
// Draw the rays
for (let i = 0; i < casts.length; i++) {
push();
stroke(255);
strokeWeight(2);
line(this.pos.x, this.pos.y, casts[i].x, casts[i].y);
pop();
}
}

// Return the intersection points
return casts;
return [casts, distances];
}


Expand Down
12 changes: 6 additions & 6 deletions index.html
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@
<head>
<meta charset="utf-8"></meta>
<title></title>
<script src="https://cdnjs.cloudflare.com/ajax/libs/p5.js/1.1.9/p5.js"></script>
<script src="racetrack.js"></script>
<script src="wall.js"></script>
<script src="ray.js"></script>
<script src="car.js"></script>
<script src="sketch.js"></script>
<script defer src="p5/p5.js"></script>
<script defer src="racetrack.js"></script>
<script defer src="wall.js"></script>
<script defer src="ray.js"></script>
<script defer src="car.js"></script>
<script defer src="sketch.js"></script>
</head>
<body></body>
</html>
Loading

0 comments on commit 6328702

Please sign in to comment.