Modeling Physics in Javascript: Gravity and Drag
burakkanber.com
burakkanber.com
However, the simulation would slow down considerably after adding a few dozen balls. This was largely due to the quadratic nature of my naive collision detection. In order to remedy the speed problems I implemented[2] a quadtree I found online to assist me in culling a lot of my expensive collision checks. The bounds of the quadtree sections will appear as you begin to add balls.
Unfortunately the quadtree wasn't the performance savior I expected it to be. I didn't notice a dramatic improvement in the number of balls I could support. I also introduced a bug with the quadtree version where certain collisions are missed entirely.
Perhaps I should revisit this simulation and see where I was off.
[0] http://www.gamedev.net/page/resources/_/technical/game-progr...
As a cool feature, it maintains the energy of the system, so you can slow down, stop, and then rewind time and arrive at the same previous configuration, all by changing the delta T's inside the evaluation.
fwiw the only thing you need to do to be one of the best references on numerical integration in js on the net is translate the code to javascript...
Does anyone know how they do the approximation? There is a fudge factor you can set for say a 10% boundary around an object, where it's almost like it moves all of the objects and then takes a best guess where they should be placed for the next physics frame. But I'm curious what their approach is called, if it's a general solution.
Another interesting JS physics experiment that I found while looking into this stuff is this n-body gravity simulator: http://www.spacegoo.com/solar_system/
If it will help at all feel free to poke through the source code!
Your assumptions about speed might be true for something like a Nintendo DS which doesn't have a floating point unit but a 3DS does, a vectorised one no less. Also in Javascript integers that can fit into 32 bits are actually integers nowadays.
All in all, I believe your information is outdated.
It looks like I might be very wrong.
I will make my opinion later by calculating decimal of PI or something like that in C and in JS to benchmark.
I think I need to update my knowledge. :)
Havok, Bullet, PysX - as far as I know they all use floats.
>>> 10*1.1198
11.197999999999999
This limitation is intrinsic to IEEE 754, and no alternative are in the pipe (IEEE854 seems stalled).I have been working for the web ads industry and canvases were pretty disappointing in terms of performance when the number of sprites were growing up. The funnier part was the inconsistency in result amongst the browsers.
So simulate as much as you want, just don't expect to beat flash or native client in terms of performance...
Simulating physics for realsies takes a lot more than that.
The solver algorithm I used is called Euler's method, or ODE1 for short. It's the least accurate numerical ODE solver there is! When people do real physics simulations they use the Runge-Kutta (ODE45) or Adams-Bashforth-Moutlon (ODE113) solves, and they'll have 100,000 steps per second.
So while it's important to be cognizant of these things, it's also best not to stress to much about what's going on in the first of a series of educational articles :)
But as I stated earlier, I think I have to check first because I am not that convinced any more on the performance point of view that JS has a problem, and float representation is not a JS problem, but a problem global to all CPUs.
Well it can be more than 10^-12 sometimes and the problem is not in physics but in trading when errors stack up. That's why I prefer when e-commerce solutions are based on fixed point arithmetic.
Even though canvases are still not homogeneous in terms of performance from browsers to browsers, some works fairly well.
So no simulation will ever get exactly the "right" answer at that level. For practical purposes, all we care about is getting an answer that's within the ensemble of reasonable outcomes for initial conditions like ours.
If you can read an article, understand what's going on and immediately open up Firebug and start playing with the equations, then Javascript has cut out 10 unnecessary steps (buying Flash, installing, learning actionscript, etc) or your path to playing with physics and numerical models!