Skip to content

Commit 8d82cc0

Browse files
committed
fix: resolve engine noise errors
1 parent 3597984 commit 8d82cc0

File tree

4 files changed

+9
-7
lines changed

4 files changed

+9
-7
lines changed

src/scripts/cars/Car.js

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -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
}

src/scripts/worlds/LivingRoom.js

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -140,8 +140,6 @@ export default class LivingRoom extends World {
140140
// Wait for assets to load before proceeding
141141
const checkLoaded = () => {
142142
if ((this.radio && this.radio.isLoaded) && (this.tv && this.tv.isLoaded)) {
143-
// Start car engine
144-
this.car.engine.start();
145143
this.isLoaded = true;
146144
} else {
147145
requestAnimationFrame(checkLoaded); // Check again in the next frame

src/scripts/worlds/World.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -282,7 +282,6 @@ export default class World {
282282
*/
283283
_start() {
284284
this.isLoaded = true;
285-
this.car.engine.start();
286285
}
287286

288287
/**

static/sfx/engine.mp3

-27.2 KB
Binary file not shown.

0 commit comments

Comments
 (0)