n_body Flutter program
Hello! This web site to introduces the n_body program. This
is a little program written in Flutter, meant to inspire people learning
programming. This program plays with physics, astronomy, orbital dynamics,
and numerical simulation. It can be built for the web, for desktop (MacOS,
Windows and Linux), and as a mobile app (Android or Ios).
You should be able to run the application, at https://billf-pshs.github.io/n_body/run/, or by clicking the image below.
lib/main.dart.
It's about 300 lines long, plus comments.
You might notice another source file:
lib/graph.dart.
This is some code that was added for debugging. It can display a graph of the velocity and
acceleration of the orbiting bodies.
A lot is missing! If you want to make this program better, grab a copy and start playing. Some things to consider...
You might notice that the app doesn't do anything intelligent about the screen size. Maybe there should be a way to change the scaling and scroll around, so you can find things when they zoom of the screen?
Right now, there's just the one universe, with the Sun, the Earth, Saturn and a little green comet. It would be nice to have other universes to pick from.
One of the earlier simulations had four equivalent bodies
that seem to behave the same way, until they don't. We
talked about how this is a result of the limited precision
of the math that the computer does. For example, the number
2/3 is represented in the computer as about
0.66666666666666663. That's accurate to about
sixteen decimal digits, which is pretty good. But we're
re-calculating 10,000 times a second, so little errors add
up.
This kind of problem comes up in the field of
Numerical
Analysis.
One thing you can do is to try adding more precision. Instead of
numbers that are accurate to 16 decimal digits, you can try
with numbers accurate to 50 digits, or 100, or 1000. This
might be an interesting thing to play with. To do that, you'd
probably want to find a software library that offers
what's called arbitrary precision math operations.
One that looks promising is
computable_reals.
Maybe the orbit calculations could be made into a game, by adding a spaceship with a rocket whose force and direction can be controlled.
The beauty of writing software is that you can make it do whatever you want! All you need is a computer, some kind of development environment, time, and patience. Many wonderful development environments are available for free. For Flutter, https://flutter.dev can point you to some. If this little program seems fun, grab a copy and start modifying it!