From eb181ba21eafe783f6738f2ff4fa23f1900bd4d3 Mon Sep 17 00:00:00 2001 From: Eric VanDever Date: Tue, 6 Oct 2015 14:34:37 -0400 Subject: [PATCH] bug fix for interaction, was missing the length property on draggingNodes clearing forces/velocities on manually changed nodes to smooth out physics response. --- lib/network/modules/PhysicsEngine.js | 42 +++++++++++++++++----------- lib/network/modules/PhysicsWorker.js | 2 ++ 2 files changed, 27 insertions(+), 17 deletions(-) diff --git a/lib/network/modules/PhysicsEngine.js b/lib/network/modules/PhysicsEngine.js index c1ea64fe..4be2bc0a 100644 --- a/lib/network/modules/PhysicsEngine.js +++ b/lib/network/modules/PhysicsEngine.js @@ -117,7 +117,13 @@ class PhysicsEngine { }); // For identifying which nodes to send to worker thread this.body.emitter.on('dragStart', (properties) => {this.draggingNodes = properties.nodes;}); - this.body.emitter.on('dragEnd', () => {this.draggingNodes = [];}); + this.body.emitter.on('dragEnd', () => { + // need one last update to handle the case where a drag happens + // and the user holds the node clicked at the final position + // for a time prior to releasing + this.updateWorkerPositions(); + this.draggingNodes = []; + }); this.body.emitter.on('destroy', () => { if (this.physicsWorker) { this.physicsWorker.terminate(); @@ -244,8 +250,7 @@ class PhysicsEngine { case 'positions': this.stabilized = msg.data.stabilized; var positions = msg.data.positions; - // console.log('received positions', positions); - for (let i = 0; i < this.draggingNodes; i++) { + for (let i = 0; i < this.draggingNodes.length; i++) { delete positions[this.draggingNodes[i]]; } let nodeIds = Object.keys(positions); @@ -290,20 +295,7 @@ class PhysicsEngine { */ startSimulation() { if (this.physicsEnabled === true && this.options.enabled === true) { - if (this.physicsWorker) { - for(let i = 0; i < this.draggingNodes.length; i++) { - let nodeId = this.draggingNodes[i]; - let node = this.body.nodes[nodeId]; - this.physicsWorker.postMessage({ - type: 'update', - data: { - id: nodeId, - x: node.x, - y: node.y - } - }); - } - } + this.updateWorkerPositions(); this.stabilized = false; // when visible, adaptivity is disabled. @@ -577,6 +569,22 @@ class PhysicsEngine { } } + updateWorkerPositions() { + if (this.physicsWorker) { + for(let i = 0; i < this.draggingNodes.length; i++) { + let nodeId = this.draggingNodes[i]; + let node = this.body.nodes[nodeId]; + this.physicsWorker.postMessage({ + type: 'update', + data: { + id: nodeId, + x: node.x, + y: node.y + } + }); + } + } + } /** * Revert the simulation one step. This is done so after stabilization, every new start of the simulation will also say stabilized. diff --git a/lib/network/modules/PhysicsWorker.js b/lib/network/modules/PhysicsWorker.js index f176a11c..80ee0877 100644 --- a/lib/network/modules/PhysicsWorker.js +++ b/lib/network/modules/PhysicsWorker.js @@ -37,6 +37,8 @@ class PhysicsWorker { let node = this.body.nodes[msg.data.id]; node.x = msg.data.x; node.y = msg.data.y; + this.physicsBody.forces[node.id] = {x: 0, y: 0}; + this.physicsBody.velocities[node.id] = {x: 0, y: 0}; break; case 'options': this.options = msg.data;