From e1d6e057476e5061faa7126738d007223f6a88ef Mon Sep 17 00:00:00 2001 From: Eric VanDever Date: Tue, 6 Oct 2015 16:29:52 -0400 Subject: [PATCH] additional logic to handle changing fixed nodes --- lib/network/modules/PhysicsEngine.js | 50 +++++++++++++++++++++++----- lib/network/modules/PhysicsWorker.js | 23 ++++++++----- 2 files changed, 56 insertions(+), 17 deletions(-) diff --git a/lib/network/modules/PhysicsEngine.js b/lib/network/modules/PhysicsEngine.js index 4be2bc0a..1ad93783 100644 --- a/lib/network/modules/PhysicsEngine.js +++ b/lib/network/modules/PhysicsEngine.js @@ -384,6 +384,7 @@ class PhysicsEngine { } if (this.stabilized === false) { + this.updateWorkerFixed(); // adaptivity means the timestep adapts to the situation, only applicable for stabilization if (this.adaptiveTimestep === true && this.adaptiveTimestepEnabled === true) { // this is the factor for increasing the timestep on success. @@ -464,15 +465,21 @@ class PhysicsEngine { let nodes = this.body.nodes; let edges = this.body.edges; + this.physicsBody.forces = {}; + this.physicsBody.physicsNodeIndices = []; + this.physicsBody.physicsEdgeIndices = []; + if (this.physicsWorker) { - var physicsWorkerNodes = {}; + this.physicsWorkerNodes = {}; var physicsWorkerEdges = {}; for (let nodeId in nodes) { if (nodes.hasOwnProperty(nodeId)) { let node = nodes[nodeId]; if (node.options.physics === true) { - physicsWorkerNodes[nodeId] = { + // for updating fixed later + this.physicsBody.physicsNodeIndices.push(nodeId); + this.physicsWorkerNodes[nodeId] = { id: node.id, x: node.x, y: node.y, @@ -522,15 +529,11 @@ class PhysicsEngine { this.physicsWorker.postMessage({ type: 'physicsObjects', data: { - nodes: physicsWorkerNodes, + nodes: this.physicsWorkerNodes, edges: physicsWorkerEdges } }); } else { - this.physicsBody.forces = {}; - this.physicsBody.physicsNodeIndices = []; - this.physicsBody.physicsEdgeIndices = []; - // get node indices for physics for (let nodeId in nodes) { if (nodes.hasOwnProperty(nodeId)) { @@ -571,11 +574,11 @@ class PhysicsEngine { updateWorkerPositions() { if (this.physicsWorker) { - for(let i = 0; i < this.draggingNodes.length; i++) { + 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', + type: 'updatePositions', data: { id: nodeId, x: node.x, @@ -586,6 +589,35 @@ class PhysicsEngine { } } + updateWorkerFixed() { + if (this.physicsWorker) { + for (let i = 0; i < this.physicsBody.physicsNodeIndices.length; i++) { + let nodeId = this.physicsBody.physicsNodeIndices[i]; + let physicsNode = this.physicsWorkerNodes[nodeId]; + let node = this.body.nodes[nodeId]; + if (physicsNode.options.fixed.x !== node.options.fixed.x || + physicsNode.options.fixed.y !== node.options.fixed.y) + { + let fixed = { + x: node.options.fixed.x, + y: node.options.fixed.y + }; + physicsNode.options.fixed.x = fixed.x; + physicsNode.options.fixed.y = fixed.y; + this.physicsWorker.postMessage({ + type: 'updateFixed', + data: { + id: nodeId, + x: node.x, + y: node.y, + fixed: fixed + } + }); + } + } + } + } + /** * 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 80ee0877..70994847 100644 --- a/lib/network/modules/PhysicsWorker.js +++ b/lib/network/modules/PhysicsWorker.js @@ -33,12 +33,19 @@ class PhysicsWorker { } }); break; - case 'update': - 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}; + case 'updatePositions': + let updatedNode = this.body.nodes[msg.data.id]; + updatedNode.x = msg.data.x; + updatedNode.y = msg.data.y; + this.physicsBody.forces[updatedNode.id] = {x: 0, y: 0}; + this.physicsBody.velocities[updatedNode.id] = {x: 0, y: 0}; + break; + case 'updateFixed': + let fixedNode = this.body.nodes[msg.data.id]; + fixedNode.x = msg.data.x; + fixedNode.y = msg.data.y; + fixedNode.options.fixed.x = msg.data.fixed.x; + fixedNode.options.fixed.y = msg.data.fixed.y; break; case 'options': this.options = msg.data; @@ -181,12 +188,12 @@ class PhysicsWorker { velocities[nodeId].x += ax * timestep; // velocity velocities[nodeId].x = (Math.abs(velocities[nodeId].x) > maxVelocity) ? ((velocities[nodeId].x > 0) ? maxVelocity : -maxVelocity) : velocities[nodeId].x; node.x += velocities[nodeId].x * timestep; // position - this.positions[nodeId].x = node.x; } else { forces[nodeId].x = 0; velocities[nodeId].x = 0; } + this.positions[nodeId].x = node.x; if (node.options.fixed.y === false) { let dy = this.modelOptions.damping * velocities[nodeId].y; // damping force @@ -194,12 +201,12 @@ class PhysicsWorker { velocities[nodeId].y += ay * timestep; // velocity velocities[nodeId].y = (Math.abs(velocities[nodeId].y) > maxVelocity) ? ((velocities[nodeId].y > 0) ? maxVelocity : -maxVelocity) : velocities[nodeId].y; node.y += velocities[nodeId].y * timestep; // position - this.positions[nodeId].y = node.y; } else { forces[nodeId].y = 0; velocities[nodeId].y = 0; } + this.positions[nodeId].y = node.y; let totalVelocity = Math.sqrt(Math.pow(velocities[nodeId].x,2) + Math.pow(velocities[nodeId].y,2)); return totalVelocity;