Browse Source

update change physics options on nodes

webworkersNetwork^2^2
Eric VanDever 9 years ago
parent
commit
81890032ee
2 changed files with 14 additions and 30 deletions
  1. +11
    -2
      lib/network/modules/PhysicsEngine.js
  2. +3
    -28
      lib/network/modules/PhysicsWorker.js

+ 11
- 2
lib/network/modules/PhysicsEngine.js View File

@ -158,7 +158,15 @@ class PhysicsEngine extends PhysicsBase {
*/
initEmbeddedPhysics() {
this.positionUpdateHandler = () => {};
this.physicsUpdateHandler = () => {};
this.physicsUpdateHandler = (properties) => {
if (properties.options.physics !== undefined) {
// we've received a node that has changed physics state
// so rebuild physicsBody
this.initPhysicsData();
}
// else we're accessing the information directly out of the node
// so no need to do anything.
};
if (this.physicsWorker) {
this.options.useWorker = false;
this.physicsWorker.terminate();
@ -172,11 +180,13 @@ class PhysicsEngine extends PhysicsBase {
if (!this.physicsWorker) {
// setup path to webworker javascript file
if (!__webpack_public_path__) {
// search for element with id of 'visjs'
let parentScript = document.getElementById('visjs');
if (parentScript) {
let src = parentScript.getAttribute('src')
__webpack_public_path__ = src.substr(0, src.lastIndexOf('/') + 1);
} else {
// search all scripts for 'vis.js'
let scripts = document.getElementsByTagName('script');
for (let i = 0; i < scripts.length; i++) {
let src = scripts[i].getAttribute('src');
@ -383,7 +393,6 @@ class PhysicsEngine extends PhysicsBase {
}
}
// TODO determine when startedStabilization needs to be propogated from the worker
/**
* trigger the stabilized event.
* @private

+ 3
- 28
lib/network/modules/PhysicsWorker.js View File

@ -22,6 +22,7 @@ class PhysicsWorker extends PhysicsBase {
var msg = event.data;
switch (msg.type) {
case 'physicsTick':
this.processRemovals();
this.physicsTick();
this.sendPositions();
break;
@ -57,24 +58,6 @@ class PhysicsWorker extends PhysicsBase {
}
}
// physicsTick() {
// if(this.physicsTimeout) {
// // cancel any outstanding requests to prevent manipulation of data while iterating
// // and we're going to handle it next anyways.
// clearTimeout(this.physicsTimeout);
// }
// this.processRemovals();
// if (this.options.enabled) {
// this.calculateForces();
// this.moveNodes();
// // Handle the case where physics was enabled, data was removed
// // but this physics tick was longer than the timeout and during that delta
// // physics was disabled.
// this.processRemovals();
// }
// this.stabilizationIterations++;
// }
sendPositions() {
let nodeIndices = this.physicsBody.physicsNodeIndices;
let positions = {};
@ -182,17 +165,9 @@ class PhysicsWorker extends PhysicsBase {
this.toRemove.nodeIds.push.apply(this.toRemove.nodeIds, data.nodeIds);
this.toRemove.edgeIds.push.apply(this.toRemove.edgeIds, data.edgeIds);
// Handle case where physics is disabled.
if(this.physicsTimeout) {
// don't schedule more than one physicsTick
clearTimeout(this.physicsTimeout);
if (!this.options.enabled) {
this.processRemovals();
}
this.physicsTimeout = setTimeout(()=> {
// if physics is still enabled, the next tick will handle removeElements
if (!this.options.enabled) {
this.physicsTimeout = null;
this.physicsTick();
}
}, 250);
}
processRemovals() {

Loading…
Cancel
Save