Browse Source

fix stabilize (non-batch) events from web worker

webworkersNetwork^2^2
Eric VanDever 9 years ago
parent
commit
d245c23667
2 changed files with 22 additions and 12 deletions
  1. +11
    -2
      lib/network/modules/PhysicsEngine.js
  2. +11
    -10
      lib/network/modules/PhysicsWorker.js

+ 11
- 2
lib/network/modules/PhysicsEngine.js View File

@ -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);
}
}

+ 11
- 10
lib/network/modules/PhysicsWorker.js View File

@ -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'
});
}
}

Loading…
Cancel
Save