Gravity Toy

I made a little toy using the Paper javascript library. This is still a draft and will be refined in the coming weeks, but for now have fun. You can edit the source code and run your own version by clicking the "Source" button at the top right. The code is a bit hackish at the moment (inevitable when playing with a new library), but the purpose of the functions and variables should be clear enough. Feel free to request any features in the comments!

Source


  • Click and drag to add gravity bodies
  • Spacebar - pause the simulation
  • Backspace - reset the simulation
  • [Shift-click] to center the viewport on a single body
  • - / + - zoom in and out
  • Up / Down - change mass
  • Left / Right - change simulation speed
  • [ / ] - change accuracy of solver
  • w - toggle the barrier at the edge of the screen
  • q - normalize the view-radius of the circles
  • t - toggle trails
  • r / y - change duration of trails
  • s - spawn system
  • a / d - cycle through the systems


I would like to add buttons to make the controls more user friendly, but for now we are stuck with keyboard controls. The barrier at the edge of the screen (w) will shrink or expand as you zoom (-/+). If any bodies are outside the viewport, turning the barrier on will project them back into the viewport by a series of reflections. Currently, there is no model for collision. The "bounces" on the barrier are simple reflections. As far as gravity is concerned, the bodies are modeled as spheres, and the gravitational force is adjusted accordingly when two bodies intersect (the calculation of which can be found here). Currently, there are two predefined systems that you can cycle through (a/d) and spawn (s) -- videos found here. You can follow their example to add your own system to the spawn cycle (at the top of the source code). Eventually I will make this more convenient by adding a save/load feature, but in the meantime be sure to save your systems locally to your machine. If you reload the page they will be gone! I also encourage you to share any systems you create by putting the code into a Gist and linking to it in a comment.

I made this app to stress test the Paper library as well as to start building the foundation of a basic 2D physics engine. The predictable stable orbits, fast relative speeds, ODE stiffness, and expected conservation of energy all make an n-body simulator great for bringing a physics engine to its knees. Indeed, even the 8th order symplectic method needs a smaller timestep when two massive bodies are intersecting (one of the reasons I made the radii so large). Eventually I would like to add more integrators and an ability for substepping (fixed and adaptive). It would be interesting to compare an adaptive Runge-Kutta scheme to the symplectic integrators. Or test the practicality of using automatic-differentiation in an implicit scheme. The details about the implemented symplectic integrators can be found on wikpedia. I specifically used the 3rd and 4th references listed on the page: [Forest and Ruth, 1990] and [Yoshida, 1990].

It is quite difficult to make collision robust in an n-body sim. Most other gravity toys you see on the web handle collision by simply checking if any circles are overlapping at the end of each frame. The smaller body then gets swallowed by the larger one and the velocity gets updated so as to conserve momentum (I personally don't think this is very fun). The problem is that any two bodies often have a large relative speed. They then jump over each other from one frame to the next and the collision test fails. So we would need continuous collision detection, which can be quite slow when implemented to work with multi-step integrators. Indeed, enforcing collision constraints would more than double the computation time of the symplectic integrators already implemented. It would be interesting to test the practicality of a collision-detection system using a marching algorithm in conjunction with a space-localization technique (eg. a dynamic quadtree). In any case, that is a test for down the road.



No comments:

Post a Comment