Skip to content

paullaffitte/gravity

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

26 Commits
 
 
 
 
 
 
 
 

Repository files navigation

Gravity ☄️

A 2D gravitational simulation in the browser.

An online version is available on gravity.plaffitt.com.

How to

To create an object, click on the canvas. To give it an initial velocity, click on the canvas, drag the mouse and release it.

Shortcuts


Description Key
Move the camera W A S D
Zoom in and out E Q <SCROLL>
Repeat last action R
Follow the closest object F
Unfollow object U

Technical details

Resolution

The resolution is a parameter that affect the maximum number of iterations performed when applying forces between objects. Ideally, with x objects, the simulation would require to perform T(x-1) iterations, with T(x) being the xth triangular number (T(x) = (x * (x + 1)) / 2). However, the resolution parameter allows to reduce the number of iterations when x > resolution.

Iterations are distributed between objects in order to have the top M most massive objects applying forces to all objects, thoses forces being reciprocal, it always means that all objects apply forces to the top M most massive objects, where M is defined by M = min(ceil(resolution² / x), x).

This strategy of optimisation allows to keep a low complexity with T(x-1) iterations when x <= resolution and only T(x-1) - T(x-1-M) iterations otherwise.r²-1) iterations, where only the most masive object apply forces to other objects. The main advantage is that T(x-1) - T(x-1-M) = T(x) * 2 - x - 1 = r²-1 for r < x < r², so it's only about 2x slower to run a simulation with objects than a simulation with x objects for a same resolution. When x <= resolution, the complexity of the alorithm is O(x²), while when resolution < x < resolution², the complexity of the algorithm is O(log2(x)), when x >= resolution², the complexity is linear. The main drawback is that it produce only an approximation of the real simulation when x > resolution, but filtering objects that are more capable of having an effect on others by their mass allows to mitigate the approximation and make it imperceptible to the human eye.

About

A 2D gravitational simulation in the browser

Topics

Resources

License

Stars

Watchers

Forks