diff --git a/lib/network/modules/PhysicsEngine.js b/lib/network/modules/PhysicsEngine.js index bd81ea94..319403b4 100644 --- a/lib/network/modules/PhysicsEngine.js +++ b/lib/network/modules/PhysicsEngine.js @@ -260,12 +260,12 @@ class PhysicsEngine extends PhysicsBase { physicsWorkerMessageHandler(event) { var msg = event.data; switch (msg.type) { - case 'positions': + case 'tickResults': this.stabilized = msg.data.stabilized; + this.stabilizationIterations = msg.data.stabilizationIterations; this._receivedPositions(msg.data.positions); break; case 'finalizeStabilization': - this.stabilizationIterations = msg.data.stabilizationIterations; this._finalizeStabilization(); break; case 'emit': @@ -393,6 +393,14 @@ class PhysicsEngine extends PhysicsBase { } } + _sendWorkerStabilized() { + if (this.physicsWorker) { + this.physicsWorker.postMessage({ + type: 'stabilized' + }); + } + } + /** * trigger the stabilized event. * @private @@ -403,6 +411,7 @@ class PhysicsEngine extends PhysicsBase { this.body.emitter.emit('stabilized', {iterations: amountOfIterations}); this.startedStabilization = false; this.stabilizationIterations = 0; + this._sendWorkerStabilized(); }, 0); } } diff --git a/lib/network/modules/PhysicsWorker.js b/lib/network/modules/PhysicsWorker.js index dfc13a92..abaf26ee 100644 --- a/lib/network/modules/PhysicsWorker.js +++ b/lib/network/modules/PhysicsWorker.js @@ -13,7 +13,6 @@ class PhysicsWorker extends PhysicsBase { nodeIds: [], edgeIds: [] }; - this.physicsTimeout = null; this.isWorker = true; this.emit = (event, data) => {this.postMessage({type: 'emit', data: {event: event, data: data}})}; } @@ -24,7 +23,7 @@ class PhysicsWorker extends PhysicsBase { case 'physicsTick': this.processRemovals(); this.physicsTick(); - this.sendPositions(); + this.sendTickResults(); break; case 'updatePositions': this.receivePositions(msg.data); @@ -44,6 +43,10 @@ class PhysicsWorker extends PhysicsBase { case 'setStabilized': this.stabilized = msg.data; break; + case 'stabilized': + this.startedStabilization = false; + this.stabilizationIterations = 0; + break; case 'initPhysicsData': console.debug('init physics data'); this.initPhysicsData(msg.data); @@ -58,7 +61,7 @@ class PhysicsWorker extends PhysicsBase { } } - sendPositions() { + sendTickResults() { let nodeIndices = this.physicsBody.physicsNodeIndices; let positions = {}; for (let i = 0; i < nodeIndices.length; i++) { @@ -68,10 +71,11 @@ class PhysicsWorker extends PhysicsBase { } this.postMessage({ - type: 'positions', + type: 'tickResults', data: { positions: positions, - stabilized: this.stabilized + stabilized: this.stabilized, + stabilizationIterations: this.stabilizationIterations } }); } @@ -254,12 +258,9 @@ class PhysicsWorker extends PhysicsBase { } _finalizeStabilization() { - this.sendPositions(); + this.sendTickResults(); this.postMessage({ - type: 'finalizeStabilization', - data: { - stabilizationIterations: this.stabilizationIterations - } + type: 'finalizeStabilization' }); } }