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) { physicsWorkerMessageHandler(event) {
var msg = event.data; var msg = event.data;
switch (msg.type) { switch (msg.type) {
case 'positions':
case 'tickResults':
this.stabilized = msg.data.stabilized; this.stabilized = msg.data.stabilized;
this.stabilizationIterations = msg.data.stabilizationIterations;
this._receivedPositions(msg.data.positions); this._receivedPositions(msg.data.positions);
break; break;
case 'finalizeStabilization': case 'finalizeStabilization':
this.stabilizationIterations = msg.data.stabilizationIterations;
this._finalizeStabilization(); this._finalizeStabilization();
break; break;
case 'emit': case 'emit':
@ -393,6 +393,14 @@ class PhysicsEngine extends PhysicsBase {
} }
} }
_sendWorkerStabilized() {
if (this.physicsWorker) {
this.physicsWorker.postMessage({
type: 'stabilized'
});
}
}
/** /**
* trigger the stabilized event. * trigger the stabilized event.
* @private * @private
@ -403,6 +411,7 @@ class PhysicsEngine extends PhysicsBase {
this.body.emitter.emit('stabilized', {iterations: amountOfIterations}); this.body.emitter.emit('stabilized', {iterations: amountOfIterations});
this.startedStabilization = false; this.startedStabilization = false;
this.stabilizationIterations = 0; this.stabilizationIterations = 0;
this._sendWorkerStabilized();
}, 0); }, 0);
} }
} }

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

@ -13,7 +13,6 @@ class PhysicsWorker extends PhysicsBase {
nodeIds: [], nodeIds: [],
edgeIds: [] edgeIds: []
}; };
this.physicsTimeout = null;
this.isWorker = true; this.isWorker = true;
this.emit = (event, data) => {this.postMessage({type: 'emit', data: {event: event, data: data}})}; this.emit = (event, data) => {this.postMessage({type: 'emit', data: {event: event, data: data}})};
} }
@ -24,7 +23,7 @@ class PhysicsWorker extends PhysicsBase {
case 'physicsTick': case 'physicsTick':
this.processRemovals(); this.processRemovals();
this.physicsTick(); this.physicsTick();
this.sendPositions();
this.sendTickResults();
break; break;
case 'updatePositions': case 'updatePositions':
this.receivePositions(msg.data); this.receivePositions(msg.data);
@ -44,6 +43,10 @@ class PhysicsWorker extends PhysicsBase {
case 'setStabilized': case 'setStabilized':
this.stabilized = msg.data; this.stabilized = msg.data;
break; break;
case 'stabilized':
this.startedStabilization = false;
this.stabilizationIterations = 0;
break;
case 'initPhysicsData': case 'initPhysicsData':
console.debug('init physics data'); console.debug('init physics data');
this.initPhysicsData(msg.data); this.initPhysicsData(msg.data);
@ -58,7 +61,7 @@ class PhysicsWorker extends PhysicsBase {
} }
} }
sendPositions() {
sendTickResults() {
let nodeIndices = this.physicsBody.physicsNodeIndices; let nodeIndices = this.physicsBody.physicsNodeIndices;
let positions = {}; let positions = {};
for (let i = 0; i < nodeIndices.length; i++) { for (let i = 0; i < nodeIndices.length; i++) {
@ -68,10 +71,11 @@ class PhysicsWorker extends PhysicsBase {
} }
this.postMessage({ this.postMessage({
type: 'positions',
type: 'tickResults',
data: { data: {
positions: positions, positions: positions,
stabilized: this.stabilized
stabilized: this.stabilized,
stabilizationIterations: this.stabilizationIterations
} }
}); });
} }
@ -254,12 +258,9 @@ class PhysicsWorker extends PhysicsBase {
} }
_finalizeStabilization() { _finalizeStabilization() {
this.sendPositions();
this.sendTickResults();
this.postMessage({ this.postMessage({
type: 'finalizeStabilization',
data: {
stabilizationIterations: this.stabilizationIterations
}
type: 'finalizeStabilization'
}); });
} }
} }

Loading…
Cancel
Save