ngraph.physics.simulator
v2.0.0
Published
Physics library for ngraph
Downloads
6,555
Readme
Physics for ngraph
This is a physics module for ngraph. Its primary focus is to serve force based graph layout, thus it manages a naïve system of bodies and springs.
Simulator calculates forces acting on each body and then deduces their position via Newton's law. There are three major forces in the system:
- Spring force keeps connected nodes together via Hooke's law
- Each body repels each other via Coulomb's law
- To guarantee we get to "stable" state the system has a drag force which slows entire simulation down.
Body forces are calculated in n*lg(n)
time with help of Barnes-Hut algorithm implemented in quadtree module. Euler method is then used to solve ordinary differential equation of Newton's law and get position of bodies.
quickstart
var physics = require('ngraph.physics.primitives');
var body1 = new physics.Body(0, 0);
var body2 = new physics.Body(1, 0);
var createSimulator = require('ngraph.physics.simulator');
var simulator = createSimulator();
simulator.addBody(body1);
simulator.addBody(body2);
simulator.step();
This will move apart two bodies.
For more advanced use cases, please look inside index.js
, which includes documentation for public API and describes engine configuration properties.
install
With npm do:
npm install ngraph.physics.simulator
todo
I spent countless hours trying to optimize performance of this module but it's not perfect. Ideally I'd love to use native arrays to simulate physics. Eventually this will allow to calculate forces on video card or via webworkers.
license
MIT