@@ -9,11 +9,11 @@ export const CAR_HEIGHT = CAR_Y * SCALE_RATE;
99export const CAR_LENGTH = CAR_Z * SCALE_RATE ;
1010
1111export class Car {
12- constructor ( modelPath , position = { x : 0 , y : 0 , z : 0 } , onLoadCallback ) {
12+ constructor ( modelPath , position = { x : 0 , y : 0 , z : 0 } , heading = Math . PI / 2 , onLoadCallback ) {
1313 this . position = new THREE . Vector3 ( position . x , position . y , position . z ) ;
14- this . heading = Math . PI / 2 ;
14+ this . heading = heading ;
1515 this . velocity = 0 ;
16- this . wheelBase = CAR_LENGTH * 5 / 8 ; // Distance between front and rear axles
16+ this . wheelBase = CAR_LENGTH * 5 / 8 ;
1717 this . maxSpeed = 20 ;
1818 this . acceleration = 0.5 ;
1919 this . friction = 0.98 ;
@@ -27,6 +27,7 @@ export class Car {
2727 this . group = gltf . scene ;
2828 this . group . position . set ( this . position . x , this . position . y , this . position . z ) ;
2929 this . group . scale . set ( SCALE_RATE , SCALE_RATE , SCALE_RATE ) ;
30+ this . group . rotation . y = this . heading ;
3031
3132 if ( onLoadCallback ) onLoadCallback ( this ) ;
3233 } ,
@@ -42,41 +43,41 @@ export class Car {
4243 if ( keys . backward ) this . velocity = Math . max ( this . velocity - this . acceleration , - this . maxSpeed ) ;
4344 this . velocity *= this . friction ;
4445
45- if ( keys . left ) this . steerAngle = Math . max ( this . steerAngle - 0.02 , - this . maxSteerAngle ) ;
46- if ( keys . right ) this . steerAngle = Math . min ( this . steerAngle + 0.02 , this . maxSteerAngle ) ;
46+ if ( keys . right ) this . steerAngle = Math . max ( this . steerAngle - 0.02 , - this . maxSteerAngle ) ;
47+ if ( keys . left ) this . steerAngle = Math . min ( this . steerAngle + 0.02 , this . maxSteerAngle ) ;
4748 if ( ! keys . left && ! keys . right ) this . steerAngle *= 0.9 ;
4849
4950 const frontWheel = this . position . clone ( ) . add ( new THREE . Vector3 (
50- Math . cos ( this . heading ) * ( this . wheelBase / 2 ) + Math . sin ( this . heading ) * ( CAR_WIDTH / 2 ) ,
51+ Math . sin ( this . heading ) * ( this . wheelBase / 2 ) - Math . cos ( this . heading ) * ( CAR_WIDTH / 2 ) ,
5152 0 ,
52- Math . sin ( this . heading ) * ( this . wheelBase / 2 ) - Math . cos ( this . heading ) * ( CAR_WIDTH / 2 )
53+ Math . cos ( this . heading ) * ( this . wheelBase / 2 ) + Math . sin ( this . heading ) * ( CAR_WIDTH / 2 )
5354 ) ) ;
5455 const backWheel = this . position . clone ( ) . add ( new THREE . Vector3 (
55- - Math . cos ( this . heading ) * ( this . wheelBase / 2 ) + Math . sin ( this . heading ) * ( CAR_WIDTH / 2 ) ,
56+ - Math . sin ( this . heading ) * ( this . wheelBase / 2 ) - Math . cos ( this . heading ) * ( CAR_WIDTH / 2 ) ,
5657 0 ,
57- - Math . sin ( this . heading ) * ( this . wheelBase / 2 ) - Math . cos ( this . heading ) * ( CAR_WIDTH / 2 )
58+ - Math . cos ( this . heading ) * ( this . wheelBase / 2 ) + Math . sin ( this . heading ) * ( CAR_WIDTH / 2 )
5859 ) ) ;
5960
6061 backWheel . add ( new THREE . Vector3 (
61- Math . cos ( this . heading ) * this . velocity * dt ,
62+ Math . sin ( this . heading ) * this . velocity * dt ,
6263 0 ,
63- Math . sin ( this . heading ) * this . velocity * dt
64+ Math . cos ( this . heading ) * this . velocity * dt
6465 ) ) ;
6566 frontWheel . add ( new THREE . Vector3 (
66- Math . cos ( this . heading + this . steerAngle ) * this . velocity * dt ,
67+ Math . sin ( this . heading + this . steerAngle ) * this . velocity * dt ,
6768 0 ,
68- Math . sin ( this . heading + this . steerAngle ) * this . velocity * dt
69+ Math . cos ( this . heading + this . steerAngle ) * this . velocity * dt
6970 ) ) ;
7071
7172 this . position = frontWheel . clone ( ) . add ( backWheel ) . multiplyScalar ( 0.5 ) . add ( new THREE . Vector3 (
72- - Math . sin ( this . heading ) * ( CAR_WIDTH / 2 ) ,
73+ Math . cos ( this . heading ) * ( CAR_WIDTH / 2 ) ,
7374 0 ,
74- Math . cos ( this . heading ) * ( CAR_WIDTH / 2 )
75+ - Math . sin ( this . heading ) * ( CAR_WIDTH / 2 )
7576 ) ) ;
76- this . heading = Math . atan2 ( frontWheel . z - backWheel . z , frontWheel . x - backWheel . x ) ;
77+ this . heading = Math . atan2 ( frontWheel . x - backWheel . x , frontWheel . z - backWheel . z ) ;
7778
7879 this . group . position . copy ( this . position ) ;
79- this . group . rotation . y = - this . heading + Math . PI / 2 ;
80+ this . group . rotation . y = this . heading ;
8081 }
8182
8283
0 commit comments