3 ms·
Very neat! But possibly afflicted somewhat with rounding errors; I spent a few minutes reading about Lagrangian mechanics, and came back to find the pendulum wh
by throwanem 10y ago
Very neat! But possibly afflicted somewhat with rounding errors; I spent a few minutes reading about Lagrangian mechanics, and came back to find the pendulum whirling around its origin point like a thrown bolas. It looks like you've put the proper amount of effort into scope hygiene and the debugger doesn't seem to want to work, so I can't give the values of any of your code's variables, but I'm using Firefox 45 ESR, if that helps at all.
- grondilu 10y agoI've also noticed that the simulation goes berserk, but only if you leave the page (I mean if you go to an other window or tab). I don't think it's due to Runge-Kutta but rather to the way HTML5/javascript behaves when the page is running in the background. I have no idea what could be done to avoid it.
- tgb 10y agoThis happened to me, too. My guess is that the simulation stops running when not open, but when it next runs it computes using a time delta calculated by the actual time since the tab was seen. So the integration adds a ridiculous amount of acceleration to the velocity, at which point the whole thing is doomed. This problem: http://gafferongames.com/game-physics/fix-your-timestep/ http://gafferongames.com/game-physics/fix-your-timestep/
- grondilu 10y agoThat looks like a sensible explanation, thanks. I guess I can put a ceiling on dt as a quick work-around.
- throwanem 10y agoI bet you're right! Or close, anyway. From [1]: > The callback rate may be reduced to a lower rate when running in background tabs or in hidden <iframe>s in order to improve performance and battery life. [1] https://developer.mozilla.org/it/docs/Web/API/window/requestAnimationFrame https://developer.mozilla.org/it/docs/Web/API/window/request...
- jchavannes 10y agoYep. This is the code that's causing it[1]: var now = Date.now()/1000; var dt = now - then; Adding this line fixes it: dt = dt > 1 ? 1 : dt; [1] https://github.com/grondilu/grondilu.github.io/blob/47d41d/pendulum-simple.js#L53 https://github.com/grondilu/grondilu.github.io/blob/47d41d/p...