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.

39 lines
938 B

  1. /**
  2. * Created by Alex on 2/23/2015.
  3. */
  4. class CentralGravitySolver {
  5. constructor(body, physicsBody, options) {
  6. this.body = body;
  7. this.physicsBody = physicsBody;
  8. this.setOptions(options);
  9. }
  10. setOptions(options) {
  11. this.options = options;
  12. }
  13. solve() {
  14. var dx, dy, distance, node, i;
  15. var nodes = this.physicsBody.calculationNodes;
  16. var nodeIndices = this.physicsBody.calculationNodeIndices;
  17. var forces = this.physicsBody.forces;
  18. var gravity = this.options.centralGravity;
  19. var gravityForce = 0;
  20. for (i = 0; i < nodeIndices.length; i++) {
  21. let nodeId = nodeIndices[i];
  22. node = nodes[nodeId];
  23. dx = -node.x;
  24. dy = -node.y;
  25. distance = Math.sqrt(dx * dx + dy * dy);
  26. gravityForce = (distance == 0) ? 0 : (gravity / distance);
  27. forces[nodeId].x = dx * gravityForce;
  28. forces[nodeId].y = dy * gravityForce;
  29. }
  30. }
  31. }
  32. export {CentralGravitySolver};