@@ -24,7 +24,7 @@ export default class Car {
2424 * @param {GLTFLoader } loader The GLTFLoader
2525 * @param {THREE.Scene } scene The Three.js scene
2626 * @param {CANNON.World } physicsWorld The cannon-es physics world
27- * @param {THREE.Vector3 } startPosition The start position of the car
27+ * @param {THREE.Vector3|CANNON.Vec3 } startPosition The start position of the car
2828 * @param {CANNON.Material } wheelMaterial The physics material for the wheels
2929 * @param {THREE.PerspectiveCamera } camera The Three.js camera for the scene
3030 * @param {Number } maxForce The maximum force to apply to the car
@@ -73,14 +73,15 @@ export default class Car {
7373 // Audio
7474 // Configure gainNode to control gain
7575 this . gainNode = new Tone . Gain ( 1 ) . toDestination ( ) ;
76- // Flag if engine is running
77- this . engineRunning = false ;
7876
7977 // Configure collision frequency
8078 this . lastCollisionTime = 0 ;
8179
8280 this . camera = camera ;
8381
82+ // Check if engine is ready
83+ this . engineReady = false ;
84+
8485 this . _loadModel ( loader , modelPath ) . then ( ( ) => {
8586 this . _addPhysics ( physicsWorld , wheelMaterial ) ;
8687 this . _createInstance ( scene ) ;
@@ -518,6 +519,7 @@ export default class Car {
518519 this . engine . loop = true ;
519520 // Connect to gain node to control proximity audio
520521 this . engine . connect ( this . gainNode ) ;
522+ this . engineReady = true ;
521523 } ) . toDestination ( ) ;
522524 }
523525
@@ -526,6 +528,9 @@ export default class Car {
526528 * @protected
527529 */
528530 _updateEngineNoise ( ) {
531+ // Start engine if needed
532+ if ( this . engine . state === "stopped" ) this . engine . start ( ) ;
533+
529534 // Get the vehicle's speed
530535 const speed = this . chassis . velocity . length ( ) ;
531536
@@ -586,6 +591,6 @@ export default class Car {
586591 proximityVolume ( this . chassis . position , this . camera , this . gainNode , - 0.3 ) ;
587592
588593 // If the engine player is ready, play engine noises
589- if ( this . engine ) this . _updateEngineNoise ( ) ;
594+ if ( this . engineReady ) this . _updateEngineNoise ( ) ;
590595 }
591596}
0 commit comments