vis.js is a dynamic, browser-based visualization library
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

32 lines
761 B

/**
* Created by Alex on 2/23/2015.
*/
class CentralGravitySolver {
constructor(body, options) {
this.body = body;
this.options = options;
}
solve() {
var dx, dy, distance, node, i;
var nodes = this.body.calculationNodes;
var gravity = this.options.centralGravity;
var gravityForce = 0;
var calculationNodeIndices = this.body.calculationNodeIndices;
for (i = 0; i < calculationNodeIndices.length; i++) {
node = nodes[calculationNodeIndices[i]];
dx = -node.x;
dy = -node.y;
distance = Math.sqrt(dx * dx + dy * dy);
gravityForce = (distance == 0) ? 0 : (gravity / distance);
node.fx = dx * gravityForce;
node.fy = dy * gravityForce;
}
}
}
export {CentralGravitySolver};