Browse Source

final touches on stabilization

webworkersNetwork^2^2
Eric VanDever 9 years ago
parent
commit
c687e414dd
3 changed files with 26 additions and 9 deletions
  1. +6
    -0
      lib/network/modules/PhysicsBase.js
  2. +13
    -8
      lib/network/modules/PhysicsEngine.js
  3. +7
    -1
      lib/network/modules/PhysicsWorker.js

+ 6
- 0
lib/network/modules/PhysicsBase.js View File

@ -19,6 +19,12 @@ class PhysicsBase {
this.stabilized = false;
this.stabilizationIterations = 0;
this.timestep = 0.5;
// parameters for the adaptive timestep
this.adaptiveTimestep = false;
this.adaptiveTimestepEnabled = false;
this.adaptiveCounter = 0;
this.adaptiveInterval = 3;
}
/**

+ 13
- 8
lib/network/modules/PhysicsEngine.js View File

@ -15,12 +15,6 @@ class PhysicsEngine extends PhysicsBase {
this.freezeCache = {};
this.renderTimer = undefined;
// parameters for the adaptive timestep
this.adaptiveTimestep = false;
this.adaptiveTimestepEnabled = false;
this.adaptiveCounter = 0;
this.adaptiveInterval = 3;
this.ready = false; // will be set to true if the stabilize
// default options
@ -106,7 +100,6 @@ class PhysicsEngine extends PhysicsBase {
});
this.body.emitter.on('_positionUpdate', (properties) => this.positionUpdateHandler(properties));
this.body.emitter.on('_physicsUpdate', (properties) => this.physicsUpdateHandler(properties));
// For identifying which nodes to send to worker thread
this.body.emitter.on('dragStart', (properties) => {
this.draggingNodes = properties.nodes;
});
@ -316,6 +309,7 @@ class PhysicsEngine extends PhysicsBase {
startSimulation() {
if (this.physicsEnabled === true && this.options.enabled === true) {
this.stabilized = false;
this._updateWorkerStabilized();
// when visible, adaptivity is disabled.
this.adaptiveTimestep = false;
@ -339,6 +333,8 @@ class PhysicsEngine extends PhysicsBase {
*/
stopSimulation(emit = true) {
this.stabilized = true;
this._updateWorkerStabilized();
if (emit === true) {
this._emitStabilized();
}
@ -351,6 +347,14 @@ class PhysicsEngine extends PhysicsBase {
}
}
_updateWorkerStabilized() {
if (this.physicsWorker) {
this.physicsWorker.postMessage({
type: 'setStabilized',
data: this.stabilized
});
}
}
/**
* The viewFunction inserts this step into each renderloop. It calls the physics tick and handles the cleanup at stabilized.
@ -555,6 +559,7 @@ class PhysicsEngine extends PhysicsBase {
return totalVelocity;
}
// TODO probably want to move freeze/restore to PhysicsBase and do in worker if running
/**
* When initializing and stabilizing, we can freeze nodes with a predefined position. This greatly speeds up stabilization
* because only the supportnodes for the smoothCurves have to settle.
@ -629,7 +634,7 @@ class PhysicsEngine extends PhysicsBase {
if (this.physicsWorker) {
this.physicsWorker.postMessage({
type: 'stabilization',
type: 'stabilize',
data: {
targetIterations: iterations
}

+ 7
- 1
lib/network/modules/PhysicsWorker.js View File

@ -37,9 +37,12 @@ class PhysicsWorker extends PhysicsBase {
case 'removeElements':
this.removeElements(msg.data);
break;
case 'stabilization':
case 'stabilize':
this.stabilize(msg.data);
break;
case 'setStabilized':
this.stabilized = msg.data;
break;
case 'initPhysicsData':
console.debug('init physics data');
this.initPhysicsData(msg.data);
@ -123,6 +126,9 @@ class PhysicsWorker extends PhysicsBase {
if (opts.mass !== undefined) {
optionsNode.options.mass = opts.mass;
}
if (opts.edges && opts.edges.length) {
optionsNode.edges.length = opts.edges.length;
}
} else {
console.warn('sending properties to unknown node', data.id, data.options);
}

Loading…
Cancel
Save