4 ms·
Nice; it's lovely how position Verlet lets you simulate geometric constraints so naturally. If you're interested in learning more, I recommend Thomas Jakobsen's
by mbostock 15y ago
Nice; it's lovely how position Verlet lets you simulate geometric constraints so naturally. If you're interested in learning more, I recommend Thomas Jakobsen's "Advanced Character Physics" (I could only find a PDF link):
http://www.pagines.ma1.upc.edu/~susin/files/AdvancedCharacterPhysics.pdf http://www.pagines.ma1.upc.edu/~susin/files/AdvancedCharacte...
The first demo reminded me of a collision-detection D3 demo I made last year. This one uses a quadtree to accelerate collision-detection, as well as another quadtree for the Barnes–Hut approximation of charge forces:
http://mbostock.github.com/d3/talk/20110921/collision.html http://mbostock.github.com/d3/talk/20110921/collision.html
More here: http://vimeo.com/29458354 http://vimeo.com/29458354
- jashkenas 15y agoAs someone who's had a lot of experience implementing these things for JS, I wonder what you think of @soulwire's strategy and general structure for applying (say, Verlet trajectories): https://github.com/soulwire/Coffee-Physics/blob/master/source/engine/integrator/Verlet.coffee https://github.com/soulwire/Coffee-Physics/blob/master/sourc... ... compared to your own approach: https://github.com/mbostock/d3/blob/master/src/layout/force.js#L45-118 https://github.com/mbostock/d3/blob/master/src/layout/force....
- mbostock 15y agoThe Verlet integration is just this part here: https://github.com/mbostock/d3/blob/master/src/layout/force.js#L104-114 https://github.com/mbostock/d3/blob/master/src/layout/force.... The rest of the function is calculating default forces and constraints for graph layout; these can be disabled, and thanks to the resilience of Verlet integration, you can easily implement custom forces or constraints in your "tick" event listener (as Shan Carter did in the budget piece a few weeks ago). Given that it's a generalized physics engine, I like that @soulwire's code is organized into clean, modular units. That makes it easier to test and modify the internals. D3's force layout is specifically-tailored to graph layout, so I don't consider it necessary to make the implementation so modular; the requirements of the force layout are that it is fast by default, that it can be customized with incremental additional effort, and that it is convenient for the common cases. Also, with larger graphs numerical integration is one of the few places where JavaScript performance (not just rendering and DOM manipulation) actually matters; while unfortunate, it's often necessary to make a trade-off between generality and performance. Still, I'm considering more modular reusable forces and constraints for a future iteration of the force layout (perhaps similar to the older Protovis force layout). But since custom forces tend to be very… well… custom, I think the best option is simply to modify the nodes' positions as needed and let Verlet do the rest.