Over the holidays I finally got around to doing some recreational coding again. The results even surprised me so I thought I’d share it with my blog-readers. So here it is, your very own planitarium. Planets flying around with real gravity just like Newton taught us (who needs all that fancy new-fangled relativity anyway), and if you feel like it you can even play God and slingshot planets into outer space. Just try clicking anywhere in or around the window.
I also included the source code although I’m not particularly proud of it. I’ve spent more time playing with the program than actually writing nice code. It’s been a long time since I’ve actually written code without tests. The code itself is quite simple. There’s a galaxy object that contains and handles all the planet interaction. There’s a body object that comes in two flavours, a normal one and a mousebody that represents the mouse position. They both implement IBody so the Galaxy doesn’t care what bodies there are. The normal body objects fly around. The MouseBody always has the position of the mousepointer and only has mass when you push a mousebutton.
That’s about it. There’s a planetariumview and a presenter object that handle painting the whole planet stuff. There’s a Time object that wraps a timer for the heartbeat and there is a program that wires it all together.
Enjoy! and let me know if you do anything fun with it.