News for hackers. Stuff that matters.
Re:Show HN: A 2D Physics Simulator in JavaScript by olegkikin on Thursday July 27, 2017 @03:22PM (#14868505)
Why is this upvoted so much? In what way is it better than any of the existing JS physics engines?

Matter.js: http://brm.io/matter-js/

Cannon.js: http://www.cannonjs.org/

PhysicsJS: http://wellcaffeinated.net/PhysicsJS/

Box2DJS: http://box2d-js.sourceforge.net/ (Emscripten-compiled from flash)

p2: https://github.com/schteppe/p2.js

Ammo.js: https://github.com/kripken/ammo.js/ (Emscripten-compiled from C++)

JPE: https://github.com/colorhook/JPE

Oimo.js: https://github.com/lo-th/Oimo.js/

Planck.js: https://github.com/shakiba/planck.js

Re:Show HN: A 2D Physics Simulator in JavaScript by xchip on Thursday July 27, 2017 @04:02PM (#14868902)
All those are great physics engines. Mine is just very simple and easy to understand, hopefully it inspires other people to get into the world of simulations.
Re:Show HN: A 2D Physics Simulator in JavaScript by looki on Thursday July 27, 2017 @04:35PM (#14869201)
Also thanks for the reference to Erin Catto's talk, need to check it out. Maybe physics engines aren't magic after all.
Re:Show HN: A 2D Physics Simulator in JavaScript by xchip on Friday July 28, 2017 @03:51PM (#14876991)
In fact that was one of the reasons that compelled me do write my own, because physics engines looked to me like voodoo magic. Thanks for understanding my obsession with learning :)
Re:Show HN: A 2D Physics Simulator in JavaScript by Aeolun on Friday July 28, 2017 @02:10AM (#14872338)
It doesn't take into account surface friction? Those boxes seem covered in soap to me.
Re:Show HN: A 2D Physics Simulator in JavaScript by throwaway91111 on Thursday July 27, 2017 @07:48PM (#14870806)
This is a Show HN; you don't need to be entirely novel to have things to show.

Personally i found the linked content to be much more approachable than your links, though they are undoubtedly better physics engines.

Re:Show HN: A 2D Physics Simulator in JavaScript by tachyonbeam on Thursday July 27, 2017 @04:00PM (#14868888)
Because it does something cool and people find it interesting. Doesn't take away from existing systems.
Re:Show HN: A 2D Physics Simulator in JavaScript by ivan_ah on Thursday July 27, 2017 @11:54PM (#14871839)
To add to the list, the best physics engine I've seen (not for js games, for actual physics simulations to teach people physics) is this one: https://www.myphysicslab.com/

PheT also has lots of good stuff: https://phet.colorado.edu/

Re:Show HN: A 2D Physics Simulator in JavaScript by opmac on Thursday July 27, 2017 @05:22PM (#14869707)
I'm with you, and I don't even find this particular example that interesting or accurate. You just re-run the same simulation over and over without any control of the initial or boundary conditions.
Re:Show HN: A 2D Physics Simulator in JavaScript by ivan_ah on Friday July 28, 2017 @12:10AM (#14871900)
What's special about this simulation is that the entire code fits on one page, including linear algebra library: https://github.com/aguaviva/Physics/blob/master/RigidBodyCha...

I've been trying to understand js physics simulation for many years, and this is the first demo that I find understandable. Amazing work!

Re:Show HN: A 2D Physics Simulator in JavaScript by xchip on Friday July 28, 2017 @03:57PM (#14877041)
Fantastic, in fact I wrote because I had been trying to understand physics engines four a long time and it was super hard to follow the papers. I wrote it four learning my self and published it to help others not go through my frustration :)
Re:Show HN: A 2D Physics Simulator in JavaScript by tapirl on Friday July 28, 2017 @09:30AM (#14874090)
I used some physics engines. I feel the simulation quality of the one made by op is quit good, even better than some complete engines.
Re:Show HN: A 2D Physics Simulator in JavaScript by xchip on Saturday July 29, 2017 @10:01AM (#14881254)
Thanks! :)
Re:Show HN: A 2D Physics Simulator in JavaScript by ell0ell0 on Thursday July 27, 2017 @12:03PM (#14866537)
If you want to look at precedence or what other people have done successfully in this space check out http://brm.io/matter-js/ matter JS. It's pretty sweet.
Re:Show HN: A 2D Physics Simulator in JavaScript by xchip on Thursday July 27, 2017 @12:44PM (#14866996)
pretty cool and inspiring!
Re:Show HN: A 2D Physics Simulator in JavaScript by xchip on Thursday July 27, 2017 @09:50AM (#14865203)
It features collisions, collisions response and hinges-constraints.

I also wrote a tutorial on how constraints work (probably the the most difficult part)

Re:Show HN: A 2D Physics Simulator in JavaScript by mobiletelephone on Thursday July 27, 2017 @10:50AM (#14865819)
Cool!

Is it deterministic? Does it do destructive updates?

Re:Show HN: A 2D Physics Simulator in JavaScript by xchip on Thursday July 27, 2017 @11:20AM (#14866106)
Thanks! What do you mean by deterministic/destructive updates?
Re:Show HN: A 2D Physics Simulator in JavaScript by kroltan on Thursday July 27, 2017 @11:56AM (#14866475)
No idea what destructive updates are, but determinism is when you can run multiple times in multiple environments and get the same results
Re:Show HN: A 2D Physics Simulator in JavaScript by xchip on Thursday July 27, 2017 @04:07PM (#14868950)
then yes, it is deterministic :)
Re:Show HN: A 2D Physics Simulator in JavaScript by reificator on Friday July 28, 2017 @12:22PM (#14875400)
I don't mean to be rude, but if you don't know the term I'd put money on it not being deterministic. Especially if you're running in JavaScript on multiple browsers.

A single step that isn't consistent in behavior across every setup is enough to make the entire process non-deterministic.

Re:Show HN: A 2D Physics Simulator in JavaScript by mobiletelephone on Thursday July 27, 2017 @04:20PM (#14869078)
Destructive updates are where computing the next step in the simulation destroys or overwrites the previous step.

This is destructive:

    this.position += this.velocity
Re:Show HN: A 2D Physics Simulator in JavaScript by gugagore on Thursday July 27, 2017 @05:16PM (#14869647)
The alternative being to store a history of the simulation?
Re:Show HN: A 2D Physics Simulator in JavaScript by T-hawk on Thursday July 27, 2017 @05:49PM (#14869933)
The alternative is to treat the simulation as a series of immutable instants. Every step of the simulation is encapsulated and represented as a data structure. So the core loop would look something like nextState = advanceFrame(currentState) , where currentState is immutable and contains every piece of information needed to calculate nextState.

Then you can do whatever you want with that series of immutable instants. You might pass that data structure to a function for rendering, or for storing history, or whatever else. You might even do something clever like pass two consecutive steps into your rendering function, so you can calculate motion blur between them.

Treating each simulation step as an instance of a data structure means you decouple the simulation state and time from your program state and time. Novice programmers often use global variables to hold the state of a simulation or game, but there's a smarter way to do it. You insert a layer of abstraction between your program's control flow and the simulation's time flow.

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!

Re:Show HN: A 2D Physics Simulator in JavaScript by romaniv on Thursday July 27, 2017 @12:53PM (#14867083)
If you're interested in this kind of stuff, I highly recommend reading or watching something about Ivan Sutherland's Sketchpad.

https://www.youtube.com/watch?v=USyoT_Ha_bA

Re:Show HN: A 2D Physics Simulator in JavaScript by xchip on Thursday July 27, 2017 @03:12PM (#14868414)
I knew that one! is fantastic isn't it!? This is one of my favourites: https://github.com/xibyte/jsketcher
Re:Show HN: A 2D Physics Simulator in JavaScript by pdm55 on Thursday July 27, 2017 @02:52PM (#14868252)
Thanks, good show. Another one I like is http://physics.weber.edu/schroeder/html5/. See Newton's cannon etc.
Re:Show HN: A 2D Physics Simulator in JavaScript by thanatropism on Thursday July 27, 2017 @04:51PM (#14869380)
This is where I show that I know nothing about physics engines:

... does it have conservation of energy? Can you track total energy somehow?

... does it obey approximately some kind of Liouville theorem?

Re:Show HN: A 2D Physics Simulator in JavaScript by santaclaus on Thursday July 27, 2017 @06:23PM (#14870200)
> does it have conservation of energy? Can you track total energy somehow?

You're going to need a geometric integrator for that, probably an energy-momentum integrator. In practice it is often better to conserve the symplectic form instead of energy, though, and you can show that for fixed time-steps you can't get all of energy/momentum/symplecticity at the same time (I haven't looked at this for a while, a student of the school of Marsden jump in if I'm saying anything wrong). Furthermore, these guarantees all require integrability...

Re:Show HN: A 2D Physics Simulator in JavaScript by thanatropism on Thursday July 27, 2017 @09:18PM (#14871173)
My (ongoing) thesis is in the field of symplectic integrators.

I was asking about these simplified physics engines (in games, etc.) that are probably not deriving physical laws out of whole conservation- (or Noetherian symmetry-) cloth.

Re:Show HN: A 2D Physics Simulator in JavaScript by santaclaus on Friday July 28, 2017 @01:26AM (#14872180)
Very cool! Game rigid body engines (to my knowledge) usually use symplectic Euler (v^{n+1} = v^n + h M^{-1} F^n, q^{n+1} = q^n + h v^n) with a Moreau/Stewart-Trinkle velocity level discretization of non-penetration constraint (effectively impulses), usually lobbing off the quadratic velocity term (so no precession). These constraints are typically solved in a (projected) Gauss-Seidel fashion to horribly loose tolerances, with liberal post-stabilization thrown in. Outside of rigid bodies, for the most part in games, these days, you don't get 'simulations' that can be derived from a variational principle, but that are instead constraint satisfaction problems with some notion of momentum hacked in the side (see 'Position Based Dynamics', 'Shape Matching').

The movie folks have larger compute time budgets, and I've seen various geometric integrators used there. I know of at least one studio that uses implicit midpoint for thin shell simulation. The Cal-Tech influence has wormed its way into the graphics research world, at least, and you see some papers pop up there frequently looking at geometric integrators. This Danish guy (I'm forgetting his name) at Disney publishes papers every now and then on the topic.

Re:Show HN: A 2D Physics Simulator in JavaScript by xchip on Saturday July 29, 2017 @10:06AM (#14881273)
Amazing post!
Re:Show HN: A 2D Physics Simulator in JavaScript by yorwba on Thursday July 27, 2017 @10:14PM (#14871401)
It is surprisingly difficult to find a concrete explanation of symplecticity. It seems like https://en.m.wikipedia.org/wiki/Symplectic_integrator has links to all the relevant information, but even there the conserved quantity is given as a differential form dp /\ dq, which makes me wonder whether you can even compute it for individual points, or whether you have to integrate over the whole space.
Re:Show HN: A 2D Physics Simulator in JavaScript by samuraiseoul on Thursday July 27, 2017 @11:05AM (#14865951)
The contact link just says <my_github_account_username>@gmail.com. I'd love to look at some actual code if there's a github link?
Re:Show HN: A 2D Physics Simulator in JavaScript by xchip on Thursday July 27, 2017 @11:23AM (#14866130)
There is code and there is also some quick tutorials on how constraints work,

http://htmlpreview.github.io/?https://github.com/aguaviva/Ph...

this one shows how to solve a double/n pendulum, which is something that is not explained in detail anywhere. So if you want to learn how constraints work that should get you up to speed!

Re:Show HN: A 2D Physics Simulator in JavaScript by claar on Thursday July 27, 2017 @11:09AM (#14866001)
Re:Show HN: A 2D Physics Simulator in JavaScript by santaclaus on Thursday July 27, 2017 @12:09PM (#14866606)
Is there friction or non-zero restitution?
Re:Show HN: A 2D Physics Simulator in JavaScript by xchip on Thursday July 27, 2017 @12:17PM (#14866678)
There is no friction so far but I will add it whenever I have a chance!
Re:Show HN: A 2D Physics Simulator in JavaScript by dmitrygr on Thursday July 27, 2017 @04:57PM (#14869441)
The only part missing? Physics...

Somehow boxes slide along a flat floor with no deceleration until they fall off the screen

Re:Show HN: A 2D Physics Simulator in JavaScript by eric_h on Thursday July 27, 2017 @06:22PM (#14870186)
I think the word you were looking for was "friction". This certainly looks like a simulation that a physics professor would propose on a chalk board: "Ignoring friction for the purpose of this exercise..."
Re:Show HN: A 2D Physics Simulator in JavaScript by laythea on Thursday July 27, 2017 @12:31PM (#14866867)
Erm, no offense but:

a) physics programs are supposed to offer interactivity. Else whats the point?

b) the physics look unearthly

Re:Show HN: A 2D Physics Simulator in JavaScript by santaclaus on Thursday July 27, 2017 @12:40PM (#14866963)
> physics programs are supposed to offer interactivity. Else whats the point?

To solve differential equations for which analytical solutions do not exist?

Edit: Differential inclusions, if inequality constraints are included in the system (as they would here).

Re:Show HN: A 2D Physics Simulator in JavaScript by laythea on Thursday July 27, 2017 @12:48PM (#14867035)
Of course it depends on what the goal of the program is, but something like this should offer user interactivity. By solving whatever is necessary (or faking it).
Re:Show HN: A 2D Physics Simulator in JavaScript by xchip on Thursday July 27, 2017 @12:43PM (#14866992)
a) I know, that is still WIP :) b) There is no friction, and as Newton's First Law states "an object will remain at rest or in uniform motion in a straight line unless acted upon by an external force" :)
Re:Show HN: A 2D Physics Simulator in JavaScript by laythea on Thursday July 27, 2017 @12:49PM (#14867052)
Hey, again no offense meant. I just think the whole point of something like this is to mimic the real world. And after a couple of seconds my illusion is broken. Ps. It is cool, just needs to be cooler :)
Re:Show HN: A 2D Physics Simulator in JavaScript by xchip on Thursday July 27, 2017 @03:16PM (#14868446)
ahah, no worries it will get there! In the meanwhile you have to give me also some "suspension of disbelief" for the sake of enjoyment :D
Re:Show HN: A 2D Physics Simulator in JavaScript by laythea on Thursday July 27, 2017 @04:46PM (#14869313)
hehe. Best of luck dude! :D
Re:Show HN: A 2D Physics Simulator in JavaScript by hellofunk on Thursday July 27, 2017 @02:24PM (#14868027)
The link is for interactive demos.
Re:Show HN: A 2D Physics Simulator in JavaScript by danilocesar on Thursday July 27, 2017 @04:20PM (#14869077)
Out of curiosity: The chrome tab running the engine is using 55% of my CPU, an i7-4770K running at 3.5Ghz...

Box2D with many more objects consumes 22%.

Oimo, with a 3D rendering scene with TONS of objects: 35% CPU..

Matter.js, lots of objects with mouse interaction: 15% CPU.

Plank, lots of objects: 25%

I'm wondering how/why this ended up in the front page...

Re:Show HN: A 2D Physics Simulator in JavaScript by xchip on Sunday July 30, 2017 @07:16AM (#14885473)
Physics engines are like magic and it is pretty hard understand how the work even if you read the paper many times. Also physics engines are interesting not only because four the dynamics, but also for the way constraints are solved. There are many other problems were you could apply this same techniques. This JavaScript engine it's a couple of pages long, it's easy to understand and potentially could give you a new vision on how to solve problems. The reason why is uses do much CPU time is because the collision detection algorithm compares every single pair of objects, to speed up that part I'd need to add what is called a 'broad phase' but that would complicate the code and the point was to write clear code and leave the low hanging fruit for the reader.