Skip to content

Instantly share code, notes, and snippets.

@w8r
Created June 29, 2018 13:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save w8r/6ed820182f27b27b6af52eb3aae7ebef to your computer and use it in GitHub Desktop.
Save w8r/6ed820182f27b27b6af52eb3aae7ebef to your computer and use it in GitHub Desktop.
d3 corrected verlet
function tick() {
var i, n = nodes.length, node;
alpha += (alphaTarget - alpha) * alphaDecay;
iterations++;
forces.each(function(force) {
force(alpha);
});
//for (i = 0; i < n; ++i) {
// node = nodes[i];
// if (node.fx == null) node.x += node.vx *= velocityDecay;
// else node.x = node.fx, node.vx = 0;
// if (node.fy == null) node.y += node.vy *= velocityDecay;
// else node.y = node.fy, node.vy = 0;
//}
// corrected verlet
const intertia = 0.5;
const timeStep = 1;
for (let i = 0; i < n; i++) {
const node = nodes[i];
const acc = 0.5 * (timeStep * timeStep);
const prevX = node.x, prevY = node.y;
const px = node.px, py = node.py;
const vx = intertia * (prevX - px);
const vy = intertia * (prevY - py);
node.x = prevX + vx + node.vx * acc;
node.y = prevY + vy + node.vy * acc;
node.px = prevX; node.py = prevY;
node.vx = node.vy = 0;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment