News for hackers. Stuff that matters.
Re:Show HN: A 2D Physics Simulator in JavaScript by xchip on Thursday July 27, 2017 @12:29PM (#14866830)
The animation speed is determined by a timer, this should make it run the same in all the browsers. Is the simulation nice and smooth all the time?

Friction is a biggie that I need to add ASAP!

Re:Show HN: A 2D Physics Simulator in JavaScript by pjc50 on Thursday July 27, 2017 @02:02PM (#14867826)
I suspect you're experiencing the joy of floating-point nondeterminism.

Edit: yup, it's the same every time per browser, but I can have e.g. Edge and Chrome running next to one another doing different things.

Re:Show HN: A 2D Physics Simulator in JavaScript by archgoon on Thursday July 27, 2017 @02:45PM (#14868200)
Hey hey hey, let's not blame floating point here ;).

Floating point is completely deterministic (as you note by getting the same behavior every time you run the simulation in the browser). I'm looking at the js spec, and there shouldn't be any freedom in evaluating floating point expressions (but overly clever browser vendors may have ignored that and reordered them anyhow).

However, I do know for a fact that the implementations of the Math.sin and Math.cos functions are browser specific,

https://tc39.github.io/ecma262/#sec-math.sin

so that alone will remove any chance at browser independent reproducibility, without additional controls.

Be interesting to see if replacing Math.cos and Math.sin with your own implementation would remove the cross browser dependency.

Btw, spec conforming implementation of Math.sin

  function sin(x) {
    if (   x === Infinity 
        || x === -Infinity
        || x === NaN) {
      return NaN;
    }
    return x;     
  }
Re:Show HN: A 2D Physics Simulator in JavaScript by xchip on Thursday July 27, 2017 @03:19PM (#14868473)
that is quite strange... the time step is always the same no matter on how the frames are rendered.... the animation should be identical
Re:Show HN: A 2D Physics Simulator in JavaScript by potench on Thursday July 27, 2017 @02:56PM (#14868280)
try using requestAnimationFrame instead of a timer
Re:Show HN: A 2D Physics Simulator in JavaScript by xchip on Thursday July 27, 2017 @03:13PM (#14868425)
Awesome, I didnt know about that one (still learning!), thanks!