Created
June 29, 2018 13:39
-
-
Save w8r/6ed820182f27b27b6af52eb3aae7ebef to your computer and use it in GitHub Desktop.
d3 corrected verlet
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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