News for hackers. Stuff that matters.
Re:Show HN: A 2D Physics Simulator in JavaScript by dkonofalski on Thursday July 27, 2017 @11:42AM (#14866323)
Is this really a simulation if you can't modify any of the objects? For all we know, this could all be pre-calculated or pre-animated. Everything moves exactly the same way every time.
Re:Show HN: A 2D Physics Simulator in JavaScript by FroshKiller on Thursday July 27, 2017 @11:52AM (#14866422)
It moves differently in different browsers for me. And neither instance seems to actually make physical sense.

In Chrome, the fourth box from the top starts sliding when it hits the floor, pushing the box to the right clear off the floor box. Every box eventually slides along the floor and falls into the pit.

In Firefox, the fourth from the top stays put. It never pushes the box on the right, though all the rest eventually slide into the pit.

Re:Show HN: A 2D Physics Simulator in JavaScript by SketchySeaBeast on Thursday July 27, 2017 @12:00PM (#14866509)
In Firefox Developer Edition everything happens faster than in regular Firefox, though they seem to end at the same result. I wonder why that is. Animation doesn't seem much faster.

I think the "neither instance seems to actually make physical sense" is due to a lack of friction.

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!
Re:Show HN: A 2D Physics Simulator in JavaScript by dkonofalski on Thursday July 27, 2017 @12:14PM (#14866658)
For me, boxes move underneath other boxes due to the lack of friction. It's the exact same movement every time. I wonder why I'm getting downvoted.
Re:Show HN: A 2D Physics Simulator in JavaScript by archgoon on Thursday July 27, 2017 @01:58PM (#14867789)
> For all we know, this could all be pre-calculated or pre-animated. Everything moves exactly the same way every time.

You can look at the source code and see if that's what it's doing.

So you want to change the simulation? Change the initial setup.

This is obviously not user friendly, but I'm not sure how you can possibly suggest this is not a simulation.

Re:Show HN: A 2D Physics Simulator in JavaScript by xchip on Thursday July 27, 2017 @12:33PM (#14866887)
:D you are welcome to add objects, make the chain longer or change gravity...

Having always the same animation helps debugging. But yeah I should have added the option to add a mouse!