Building a Solar System in XNA (Part 1)

by Chris at 1:52 pm

Building a virtual solar system cant be too hard, right? Well, I can imagine how people might think that it would just be a matter of whacking in a couple of planets, perhaps a little space ship, and Bob’s your uncle. I’m quite sure that by the end of this, I’ll have proven those people wrong.. I’m thinking that I’ve actually bitten off a bit more than I can chew with this one :)

Anyway.. the topic that this article will attempt to demonstrate is how to build a solar system, using Microsoft XNA as the basic framework. The reason for using XNA, as opposed to DirectX or Open GL, is that (if done correctly), games compiled with XNA can work directly on your Xbox 360. And since I have one of these nifty devices, and I bought it under the premise of writing programs for it.. I really thought I should try and learn a bit about.. well.. writing programs for it :)

I plan to separate this article into three parts, so that the information can be more easily assimilated, and so that I dont end up writing a HUGE blog entry containing too much information, which no one will read. (Who am I kidding.. I’m the only one who reads this blog anyway).

Part 1 - Will cover the basic preparations, and end with a simple application that we will use to build on.

Part 2 - We will do a bit of work with a Scene Graph, add some Planets (and maybe some sattelites), and get things moving.

Part 3 - We will add in some collision detection, and take a look at the gravity of the situation.

The Solar System

The zones of the Solar system: the inner solar system, the asteroid belt, the giant planets (jovians) and the Kuiper Belt. Orbits not to scale.(cue cool space theme music of your choice)

You all remember remember the lessons from school about our Solar System, and it’s 8 planets. I obviously dont remember correctly, cause when I went to school there were 9 planets, but aparently one of those planets exploded got re-categorised as a “dwarf planet”. In any case, our version of the Solar System is going to have 8 planets, and 1 asteroid belt, some sattelites(moons) if we get round to it, and we will let the astronomers argue about what happens out further.

Now, with our picture in hand, plus a little extra information, we can make a table of objects that will be included in the simulation.

Object Distance Radius Mass
Sun 0 AU 6.955 ? 105 km 1.9891 ? 1030 kg
Mercury 0.47 AU 2,439.7 km 3.3022 ? 1023 kg
Venus 0.73 AU 6,051.8 km 4.8685 ? 1024 kg
Earth 1.02 AU 6,371.0 km 5.9736 ? 1024 kg
Mars 1.67 AU 3,396.2 km 6.4185 ? 1023 kg
Asteroid Belt 2.80 AU N/A N/A
Jupiter 5.46 AU 71,492 km 1.8986 x 1027 kg
Saturn 10.12 AU 60,268 km 5.6846 x 1026 kg
Uranus 20.08 AU 25,559 km 8.6810 x 1025 kg
Neptune 30.44 AU 24,764 km 1.0243 x 1026 kg

One of the immediate problems that you can see here is that these objects are fairly big (one might say big enough to have their own gravitational pull). The problem with them being so large is that they are also visible from a loooonnnng way off. In most 3d games and whatnot, you would use a viewing frustrum to cut things off that were a long way away, because they would be simply too small to see. Everyone knows that the Sun is visible from Earth, even though it is 150 million km away, so you just wouldn’t get away with removing it from the scene. I’m thinking that perhaps we could use some kind of scaling algorigthm to determine if an object is visible, since the objects shown are basically going to be big spheres (really really big spheres), it shouldn’t be too difficult to do this. I’d be thinking that this will be requiring some trial and error testing out between Part 2 and Part 3.

Ixnay on Dissin’ the XNA

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 :D

For all the Microsoft hating going around, I do like my Xbox, and I do like being able to potentially write games for it. However, I don’t like the need to pay membership to the XNA Creators Club, just to be able to play with your own applications on your own console. Fortunately, for us cheap soandsos, they have made XNA Game Studio 2.0 free to download and use. This means that you can write Xbox compatable code, and test it on your own system. Then, if you feel it is good enough to pay for, get a membership and show it off on your Xbox. Thus confirming (again) to all your friends that you are a complete nerd.

Since I own a legal a copy Visual Studio 2005, all the sample code should be compatable with that version. However, it should (in theory) work with any other version that you can install XNA Game Studio against. You might just have to do a bit of hammering to get it to work.

Now, I’m not going to walk through all of the “this is how you set up the project” to “here’s your first triangle” stuff.. there are plenty of other sites, including the XNA Creators Club which will run through this for you, and save ME the trouble of introducing you to 3d. So.. now.. for your homework, go download XNA, install it, run through some tutorials, and I’ll see you some time next week.

I’ve written a basic game that I will modify as we create the solarsystem. You can download the source here, or the binaries here. Just in case you were wondering, yes, that is the space ship model from the Spacewar starter kit. I’m no artist, nor do I have any art packages, so for this little example I hope it’s ok to use that model :)

Source: Download

Binaries: Download.