Ok, so it’s been a while since I posted the last entry onto the blog. But, no one really pays any attention to this site, so I figure no harm done. This is the final part in my ongoing series of creating a solar system using XNA.
The all important gravitational fomulas
There are a couple of different ways to do gravitational calculations, we will be using Newton’s law of Universal Gravitation to do the calculations required. I’m sure that if there are any science nerds out there reading this, or even people who’ve actually read the linked wikipedia article, they would be screaming about now saying Einstein’s theory of general relativity is much more precise. Unfortunately, I just plain don’t understand the maths right now. I’m sure with some work, and a few models and examples, I would be able to figure out how to implement the field equations into my little universe, but to be honest it’s just a little outside the scope of my understanding at this stage.
In any case, the formula we will be using former formula due to simplicity (F1 = F2 = G * ((m1 * m2) / d^2)). We can always modify the application later if this one is too inacrate.
The Ultimate Goal of an Evil Genius
Today, I had a great time starting the simulation to test out the gravity stuff. It would appear that I have some malevolent being inside of me that enjoys watching as an entire solar system is sucked directly into the sun (bwoohoohoohahaha). The planets have not yet alligned to give me the correct results (pun intended).
It’s no surprise really, considering that each planet moves at a some velocity around the sun, and is also affected by each other plant’s gravitation. What we need to do now is get some realistic initial position and velocity information for each planet, so we can place them in the right spot.. and give them a little boot to get started.
We need to find some kind of place that we can get the locations of planets without too much difficulty. What place would keep this information, so that I don’t have to use a telescope? Perhaps some kind of space agency… NASA appear to have something here.. but I have to learn some more maths (doh!). I belive a quick rundown of Kepler’s Laws is appropriate, followed by some sligtly more difficult reading from the JPL site above.
Or you could just do what I ended up doing and take a look at wikipedia for all your data needs. Clicking on the links from Part 1, I found a nifty Average Orbital Speed entry in the information bar (good ol’ wikipedia, always there with the goods). So, for future reference here are the approximate speeds along with an even more aproximate speed in lightseconds per second. Since I don’t know what the SI units are for lightseconds (and I am too lazy to do a simple search) I’m going to take a WILD stab in the dark and call the units “ls/s”.
| Planet | Approx. Orbital Velocity | |
| Mercury | 47,870 m/s | 1.5967e-4 ls/s |
| Venus | 35,080 m/s | 1.1701e-4 ls/s |
| Earth | 29,783 m/s | 9.9345e-5 ls/s |
| Mars | 24,077 m/s | 8.0312e-5 ls/s |
| Jupiter | 13,070 m/s | 4.3596e-5 ls/s |
| Saturn | 9,690 m/s | 3.2322e-5 ls/s |
| Uranus | 6,810 m/s | 2.2715e-5 ls/s |
| Neptune | 5,430 m/s | 1.8112e-5 ls/s |
And viola, we have orbiting planets… kind of. I think some of them still head directly for the sun, but that’s probably more due to my fudging the velocity vector. Look for “TODO: Calculate the vector properly” in the code. For now, I’ve just made the initial position for each planet sit at 90o so that the Matrix.Forward vector is the one to use. Stangely now (unlike at the start of this section), the planets ARE in alignment.
Time.. Never enough time…
Morose
It takes a whole year to get the earth around the sun, or at least it should if I’ve done the maths right. Unfortunately I don’t have enough time to sit and watch right now, so being creator of the universe, I simply sped up time :). The planets now move 86,400 times faster, so there is about 1 day in every second.
A Tale of Two Spheres
Testing wheather two spheres are colliding is actually very simple, you simply compare their distance away from each other with the sum of their radii (I never thought that I would have a use for that word.. radii.. sounds cool, no?). If their distance apart is less than the sum of their radii, they are colliding.
This is where we get stuck in the funky physics dance, trying to add an appropriate response into the collision. I was thinking something along the lines of Pool Hall Lessons would be appropriate, but it is not just a simple matter of pluging in an equation and going.
Unless we are doing something wrong (or fun), we shouldn’t have any collisions to deal with. So, for now I’ll put this in the too hard basket, and enjoy the super warp speed that you can get from flying directly into the sun.
As usual, there’s always something left to do
So, now we can take a look at our todo list for more fun things to do that will make this simulation spectacular and exciting.

- Ugly green planets - Yes, the planets are still ugly and green. I dont feel like chaning them right now, as I have the simulation working and it would take a bit of time to figure out how to skin them properly. If you feel so inclined, there are some free planet textures over at this site.
- Hilarious exploding results - no, not this time
- Stolen space ship? - It shouldn’t be too hard to replace the space ship model with another model, but I dont have another model to replace it with.
- Orbits do not work properly unless the plants are aligned (and yes, all the pots in my garden form a perfectly strait line). I should make the initial velocity work correctly at a right angle of the vector to the sun, but I’m too lazy and my head hurts.
- Flying into the sun (or a planet if you can catch them) causes a weird “Super Warp Fling” out the other side. This is something to do with getting too close to a single point gravitational field, and will be completely avoided with some collision detection and response.
- Strange distortions in space-time occur at distances far away from the sun. An example of this can be seen in the image to the right (above) where our ship is looking somewhat crumpled at 120 AU from the sun.
Source Code: Download
Compiled: Download



In this example, I’ve written several different node types (some of which can be reused in other projects).
After a bit of searching around for how to write text onto the screen, I found out about the SpriteFont content item. You can add a SpriteFont into your content folder by right clicking on the content folder, then choosing “Add -> New Item”, and selecting the SpriteFont item from the list. Once you have done this, it will add an XML file with the extension “.spritefont” to your content folder. Fill out this file with the font details, and the font will be imported into the project for use later on. You can then load the font during your LoadContent() routine (or whenever you like) by using the single line of code below.
After about 20 hours worth of trial and error of more and more complicated techniques, I was amost completely exhausted and thought about throwing in the towel and cancelling the blog entry :).
Ok, just between us, I’m all for Microsoft bashing.. I mean, they are a massive target, so you pretty much can’t miss.. and, you get cred for doing so