vasturiano / d3-force-3d

Force-directed graph layout in 1D, 2D or 3D using velocity Verlet integration.
https://observablehq.com/@vasturiano/multi-dimensional-d3-force-simulation
MIT License
369 stars 52 forks source link

How does the simulation work with time? #17

Closed Laytoder closed 2 years ago

Laytoder commented 2 years ago

I was exploring the simulation.tick function and found that it used no concept of delta-time while applying physics. I was curious about how d3-force-3d tackles this. I also explored the d3-timer which is the internal timer used by the library. Can anyone explain to me how d3-timer helps get around this? If I understand it correctly, d3-timer runs a timer that matches the frame rate. If my browser runs on 60fps, the tick function inside d3-timer gets called 60 times every second. If the frame rate changes, calls to the tick function will change, affecting the speed of the simulation accordingly. I don't seem to understand how this is prevented. Can any please help me with this?

vasturiano commented 2 years ago

@Laytoder thanks for reaching out.

The concept of time in this simulation is purely derived from the frame rate. This module makes no attempt at trying to ensure that simulations progress at the same speed across different frame-rates. If there's lower frame-rate than 60fps, the iterations will just take longer to complete, leading into a slow-down of the simulation progress.

So essentially, the simulation just runs as fast as it can given the existing browser conditions. This is intrinsic to the design of this module. You are also correct that d3-timer uses frame-rates, not time-deltas as iteration units.

Laytoder commented 2 years ago

Thank you so much for letting me know :)