@@ -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}
0 commit comments