From c687e414dd08bdc3d1de0037e5116374316e341d Mon Sep 17 00:00:00 2001 From: Eric VanDever Date: Mon, 26 Oct 2015 09:44:33 -0400 Subject: [PATCH] final touches on stabilization --- lib/network/modules/PhysicsBase.js | 6 ++++++ lib/network/modules/PhysicsEngine.js | 21 +++++++++++++-------- lib/network/modules/PhysicsWorker.js | 8 +++++++- 3 files changed, 26 insertions(+), 9 deletions(-) diff --git a/lib/network/modules/PhysicsBase.js b/lib/network/modules/PhysicsBase.js index 748997e6..37db6e07 100644 --- a/lib/network/modules/PhysicsBase.js +++ b/lib/network/modules/PhysicsBase.js @@ -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; } /** diff --git a/lib/network/modules/PhysicsEngine.js b/lib/network/modules/PhysicsEngine.js index 9ddb7b75..e217727b 100644 --- a/lib/network/modules/PhysicsEngine.js +++ b/lib/network/modules/PhysicsEngine.js @@ -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 } diff --git a/lib/network/modules/PhysicsWorker.js b/lib/network/modules/PhysicsWorker.js index d21812ca..67d9f4bf 100644 --- a/lib/network/modules/PhysicsWorker.js +++ b/lib/network/modules/PhysicsWorker.js @@ -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); }