@@ -87,18 +87,17 @@ export default class Car {
8787
8888 // Wait for vehicle to load before proceeding
8989 const checkVehicleLoaded = ( ) => {
90- if ( this . chassis && this . isLoaded ) {
90+ if ( this . chassis ) {
91+ // Configure honk and engine noise
92+ this . _createHonk ( ) ;
93+ this . _createEngineNoise ( ) ;
94+ this . _createCollisionNoise ( ) ;
9195 this . isLoaded = true ;
9296 } else {
9397 requestAnimationFrame ( checkVehicleLoaded ) ; // Check again in the next frame
9498 }
9599 } ;
96100 checkVehicleLoaded ( ) ;
97-
98- // Configure honk and engine noise
99- this . _createHonk ( ) ;
100- this . _createEngineNoise ( ) ;
101- this . _createCollisionNoise ( ) ;
102101 } ) ;
103102 }
104103
@@ -285,6 +284,25 @@ export default class Car {
285284
286285 // Add the vehicle to the physics world
287286 this . vehicle . addToWorld ( physicsWorld ) ;
287+
288+ this . _syncToPhysics ( ) ;
289+ }
290+
291+ /**
292+ * Sync models to their physics body
293+ * @protected
294+ */
295+ _syncToPhysics ( ) {
296+ // Sync the chassis
297+ this . models . chassis . mesh . position . copy ( this . chassis . position ) ;
298+ this . models . chassis . mesh . quaternion . copy ( this . chassis . quaternion ) ;
299+
300+ // Sync each wheel mesh with the wheel body
301+ this . vehicle . wheelBodies . forEach ( ( wheelBody , index ) => {
302+ const wheelMesh = this . models . wheels [ index ] . mesh
303+ wheelMesh . position . copy ( wheelBody . position ) ;
304+ wheelMesh . quaternion . copy ( wheelBody . quaternion ) ;
305+ } ) ;
288306 }
289307
290308 /**
@@ -330,7 +348,6 @@ export default class Car {
330348
331349 // Save instance
332350 Car . instance = this ;
333- this . isLoaded = true ;
334351 }
335352
336353 /**
@@ -573,23 +590,14 @@ export default class Car {
573590 * @public
574591 */
575592 update ( ) {
576- // Do nothing if the care isn't load
593+ // Do nothing if the car isn't loaded
577594 if ( ! this . isLoaded ) return ;
578595
579596 // Drive is enabled
580597 if ( this . isDriving ) this . _drive ( ) ;
581598
582599 // Sync models to their physics body
583- // Sync the chassis
584- this . models . chassis . mesh . position . copy ( this . chassis . position ) ;
585- this . models . chassis . mesh . quaternion . copy ( this . chassis . quaternion ) ;
586-
587- // Sync each wheel mesh with the wheel body
588- this . vehicle . wheelBodies . forEach ( ( wheelBody , index ) => {
589- const wheelMesh = this . models . wheels [ index ] . mesh
590- wheelMesh . position . copy ( wheelBody . position ) ;
591- wheelMesh . quaternion . copy ( wheelBody . quaternion ) ;
592- } ) ;
600+ this . _syncToPhysics ( ) ;
593601
594602 // Control proximity volume between car and camera
595603 proximityVolume ( this . chassis . position , this . camera , this . gainNode , - 0.3 ) ;
0 commit comments