Tuesday, February 4, 2014

Game Physics: To Scale Solar System in Open-Gl

    Space,  the final frontier,  these are the voyages of programmer Ian Percoco. In this blog I will cover the adventures, methods, successes, and failure while creating a life sized scale version of our Solar System. This was not an easy task and put a lot of my programming and math knowledge to the challenge.

    When I stated life sized, I really meant life sized,  the first part of this process was to find a unit of measurement that my solar system to live in.  I needed to figure out what to use for distance, mass, and radius.  Seems simple right? Just use like the metric standard or something, I mean we get to do all of our massive calculations on a computer, it should be no problem!  Wrong!  Using a type of unit that is too small or too large literally makes the significant digits used in the math, too many for floats or even doubles to hold.  So after a lot of calculations and making a small spread sheet I came to the conclusion I would use Megameters (1000000 meters) and Yottagrams (1.0x10^24 grams) This way the computer can actually preform the calculations needed to make our solar system move.   

    After deciding on units the challenge of making the planets actually move begins.  I set up what is known as a force registry system.  The force registry system takes the job of applying forces out of the hands of my planets and into the Registry.  Basically I make a force registry system that holds a list of forces.  The planets get linked up to the forces and then added to the registry.  When update physics is called the registry loops through and applies force upon the planets based on in this main case the gravitational pull of one planet onto another and the amount of time that has passed since the last call.  The planet takes this updated force and applies it to its velocity and then updates the position, clears out the force and waits for the next force update. This way the planets don't have to keep track of one another or calculate distance or even the total gravitational force (gravitationalConstant * mM/distance^2)

    And just like that I had movement,  I kept adding planets setting up forces and they just started to move around as expected. The hardest part was getting earths moon to keep up with earth but all that really needed to happen was have the moon be affected by both the earth and the sun as well as changing the moons initial velocity to match that of the earths + it's own.  It doesn't make the most stable orbit for long but it does work.   

  Now that I could see individually this planets moving, there was no way I would be able to get objects so far apart and at such varying sizes to appear on the screen.  So I needed to implement a distance scaling basically saying if a planet such as Neptune is too far away to be drawn,  bump up the scale a little bit to allow us to actually see it. This way we can actually see the orbits as shown above.

  From there the project was a lot of camera fixing, adding debug text, free form camera rotation and such all on this massive scale which surprisingly was one of the hardest parts (excluding finding the right units). My debug text still doesn't work right due to the changing angle of the camera but It suits me just fine right now as a proof of concept.   

No comments:

Post a Comment