Browse Source

bug fix for interaction, was missing the length property on draggingNodes

clearing forces/velocities on manually changed nodes to smooth out physics response.
webworkersNetwork^2^2
Eric VanDever 9 years ago
parent
commit
eb181ba21e
2 changed files with 27 additions and 17 deletions
  1. +25
    -17
      lib/network/modules/PhysicsEngine.js
  2. +2
    -0
      lib/network/modules/PhysicsWorker.js

+ 25
- 17
lib/network/modules/PhysicsEngine.js View File

@ -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.

+ 2
- 0
lib/network/modules/PhysicsWorker.js View File

@ -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;

Loading…
Cancel
Save