We are constantly exploring what is possible, and working on exercises to extend our knowledge of various platforms & techniques

Here, we implemented a simple n-body simulation in Javascript. The initial conditions are set as various trigonometric functions. Every body has the same mass. The "gravitational constant" is just set to a convenient value.

Some examples of the engine in action (paste in the console area below):

demo(0) demo(1) demo(2)

Type go! And hit return to run your code.

Console > go!

if you want to get more creative, you can define your own initial conditions like this

ctx.clearRect(0,0,windowWidth,windowHeight); //Clear the window! masses = initmasses(numPoints,windowWidth,windowHeight, function(theta) { return Math.cos(theta); },function(theta) { return Math.tan(2*theta); });

You can modify the functions ( theta ) to provide your own parametric function providing the initial spacing of the masses. Additionally, you can change numPoints for some fixed number like 10, 100, 1000 (if you make the number much bigger than 1,000 your browser might get pretty slow.) See the explanation below for details.

How it works:

We use a simplified version of the Leapfrog method for numerical integration. Each step, we take the position of a point (x,y) and add to it a "velocity" (vx,vy). We increment this velocity in turn by an acceleration (ax,ay) which is calculated by looking at all of the other bodies in the system and adding up the forces for each body on each other body. As you might imagine, this operation is computationally pretty slow: O(n**2). There are a number of ways of improving this, most of them involve indexing objects by distance, building a tree and reducing the calculation using some good approximations. There are also a few reasons that with our current step size, the simulation is not accurate ( energy is not conserved, even )