Skip to content

Commit 00d9838

Browse files
committed
Improved crash detection and bugfixes
1 parent 7862c34 commit 00d9838

File tree

3 files changed

+50
-16
lines changed

3 files changed

+50
-16
lines changed

car.js

Lines changed: 36 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,10 @@ class Car {
33
this.speed = 0;
44
this.maxSpeed = 7.5;
55
this.invSpeed = this.maxSpeed;
6+
// Multiplier for the acceleration and decelleration
67
this.mult = 0.955;
78

9+
// Features of the car
810
this.l = 32;
911
this.w = 16;
1012
this.theta = atan(this.w / this.l);
@@ -29,28 +31,59 @@ class Car {
2931
pop();
3032
}
3133
isColliding(track) {
34+
// Calculate all four corners of the car
35+
3236
let x1 = this.pos.x + this.hyp * cos(this.rotation + this.theta);
3337
let y1 = this.pos.y + this.hyp * sin(this.rotation + this.theta);
3438

3539
let x2 = this.pos.x + this.hyp * cos(this.rotation - this.theta);
3640
let y2 = this.pos.y + this.hyp * sin(this.rotation - this.theta);
3741

38-
// check if colliding with each separate wall
42+
let x3 = this.pos.x - this.hyp * cos(this.rotation + this.theta);
43+
let y3 = this.pos.y - this.hyp * sin(this.rotation + this.theta);
44+
45+
let x4 = this.pos.x - this.hyp * cos(this.rotation - this.theta);
46+
let y4 = this.pos.y - this.hyp * sin(this.rotation - this.theta);
47+
48+
49+
// Check if colliding with each separate wall
3950

4051
for (let i = 0; i < track.innerWalls.length; i++) {
4152

42-
if (linesCross([createVector(x1, y1), createVector(x2, y2)], [track.innerWalls[i].posA, track.innerWalls[i].posB])) {
53+
// if (linesCross([createVector(x1, y1), createVector(x2, y2)], [track.innerWalls[i].posA, track.innerWalls[i].posB])) {
54+
// return true;
55+
// }
56+
if (linesCross([createVector(x2, y2), createVector(x3, y3)], [track.innerWalls[i].posA, track.innerWalls[i].posB])) {
57+
return true;
58+
}
59+
// if (linesCross([createVector(x3, y3), createVector(x4, y4)], [track.innerWalls[i].posA, track.innerWalls[i].posB])) {
60+
// return true;
61+
// }
62+
if (linesCross([createVector(x4, y4), createVector(x1, y1)], [track.innerWalls[i].posA, track.innerWalls[i].posB])) {
4363
return true;
4464
}
65+
4566
}
4667

4768
for (let i = 0; i < track.outerWalls.length; i++) {
4869

49-
if (linesCross([createVector(x1, y1), createVector(x2, y2)], [track.outerWalls[i].posA, track.outerWalls[i].posB])) {
70+
// if (linesCross([createVector(x1, y1), createVector(x2, y2)], [track.outerWalls[i].posA, track.outerWalls[i].posB])) {
71+
// return true;
72+
// }
73+
if (linesCross([createVector(x2, y2), createVector(x3, y3)], [track.outerWalls[i].posA, track.outerWalls[i].posB])) {
74+
return true;
75+
}
76+
// if (linesCross([createVector(x3, y3), createVector(x4, y4)], [track.outerWalls[i].posA, track.outerWalls[i].posB])) {
77+
// return true;
78+
// }
79+
if (linesCross([createVector(x4, y4), createVector(x1, y1)], [track.outerWalls[i].posA, track.outerWalls[i].posB])) {
5080
return true;
5181
}
82+
5283
}
5384

85+
// If it doesn't, return false
86+
5487
return false;
5588
}
5689
}

index.html

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,13 @@
11
<!DOCTYPE html>
22
<html>
3-
<head>
4-
<meta charset="utf-8">
5-
<title></title>
6-
<script src="https://cdnjs.cloudflare.com/ajax/libs/p5.js/1.1.9/p5.js"></script>
7-
<script src="racetrack.js"></script>
8-
<script src="wall.js"></script>
9-
<script src="car.js"></script>
10-
<script src="sketch.js"></script>
11-
</head>
12-
<body>
13-
</body>
3+
<head>
4+
<meta charset="utf-8"></meta>
5+
<title></title>
6+
<script src="https://cdnjs.cloudflare.com/ajax/libs/p5.js/1.1.9/p5.js"></script>
7+
<script src="racetrack.js"></script>
8+
<script src="wall.js"></script>
9+
<script src="car.js"></script>
10+
<script src="sketch.js"></script>
11+
</head>
12+
<body></body>
1413
</html>

sketch.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -108,8 +108,10 @@ function linesCross(line1, line2) {
108108

109109

110110
let den = (x1 - x2) * (y3 - y4) - (y1 - y2) * (x3 - x4);
111-
let t = ((x1 - x3) * (y3 - y4) - (y1 - y3) * (x3 - x4)) / den;
112-
let u = -(((x1 - x2) * (y1 - y3) - (y1 - y2) * (x1 - x3)) / den);
111+
let tNum = (x1 - x3) * (y3 - y4) - (y1 - y3) * (x3 - x4);
112+
let uNum = -((x1 - x2) * (y1 - y3) - (y1 - y2) * (x1 - x3));
113+
let t = tNum / den;
114+
let u = uNum / den;
113115

114116
if (t >= 0 && t <= 1 && u >= 0 && u <= 1) {
115117
return true;

0 commit comments

Comments
 (0)