Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

If this is not the right place for this question, could you please direct me to a more suitable site/forum?

I'm a beginner in game development (and in the use of this site). I've worked with C++ only for rather small scientific simulations, so I can "code" but I've never done a full-blown design of an OO program, much less of a game. You can tell from what I'll write next :P

I've decided to write an open source educational game that simulates accurately the motion of the Sun, the planets and of one or more spacecraft controlled by the player. By "simulates accurately" I mean that I want to use a scientifically valid and precise algorithm to obtain the forces and thus the motions of the bodies.

The game will be in 3D and will use the Ogre3D engine for the graphics. Graphically it will look similar to Celestia.

The first difficulty is that I've never designed the structure and the classes of any game. By reading a few references and guides I think I have a vague idea of how the dynamics could work, but it's vague: each entity (planet or spacecraft) could store its own state information and then have an "update" function that updates the state based on some input (like user commands). There are probably many other ways to do this, I guess.

The main problem, however, comes from the particular needs of this project:

  1. my objects must not only respond to user input, but also obey accurate physical laws, and the algorithm that computes them must be centralized. This is because it needs to know the positions of all the bodies at the same time to perform its computations;
  2. I plan to add different kinds of spacecraft with different types of propulsion, and some of them would react to the other bodies in special ways (think antigravity).

The Question

Is there a design pattern or architecture suitable for this problem?

The best I can think of is to have a singleton Universe class containing the central algorithm for the dynamics. The various objects of the solar system then communicate with Universe. They would contain basic parameters like mass and size, and they would listen to Universe telling them where to stand.

However this doesn't sound right. Where do I put the ever-changing trajectory data of all the bodies, in Universe? What's the point of having all those objects then? Only for the graphical part of the game?

And what about the special dynamical properties of some spacecraft? I can't possibly include them all in the central algorithm from the beginning.


Ok, I think you can tell how confused I am about the matter much better than me. I would be grateful for any kind of input.

share|improve this question
    
The main planetary system is called an N-Body Simulation problem, if you use that to google for ideas on how to do the math part of your problem. –  Patrick Hughes Aug 20 '11 at 18:55
    
Fortunately the math is not a problem :) –  Marco Giancotti Aug 22 '11 at 7:12
add comment

3 Answers

Don't conflate the physics modeling of the universe with the graphics, that way leads to madness =) For something like what you're talking about an MVC architecture might work well.

M = model. Your model is the physics simulation of the universe. While this may be a singleton you'll find many more arguments to making it a class that can stand alone but of which you could make many. Your model is responsible for number crunching the simulation, that's it. This is completely independent of Ogre and would exist as pure data.

V = view. Your view gets its parameters from the Model and decides how to render them. There can be many different views of a model; maybe you have one that shows a condensed overhead, a second view that follows a spaceship around, and a third that, well you get the idea. A view simply looks at the model and shows it to the end user, and each view is specialized so you don't have a mega-view with a million options. This is where you would extract the numbers from the model and set them into your Ogre objects to render.

C = controller. The controller is where you interact with the model, adding new physics objects or piloting spaceships. Note that there can be many different controllers, one for each spaceship that an AI or end user controls, you get the idea.

The rest of your program is now a framework to setup and run the MVC.

There are many other ways to design your idea, MVC is mainly suited towards information displays and games like Eve but it does let you neatly organize how your program works.

You'll get many more ideas from other people but no matter how you choose your final design please separate the simulation from the graphics.

share|improve this answer
    
Your point is very clear and useful. I'll certainly keep it in mind, thank you! I'd still be interested in more detailed patterns describing how the bodies should interact with Universe, though. –  Marco Giancotti Aug 22 '11 at 7:09
3  
Try using the "Is a" and "Has a" relationships to lay out classes and responsibilities. The Universe "Has a" solar system, many of them, therefore the Universe has a container of solar systems. Each Solar system "has a" bunch of planets, and planets "has a" bunch of moons. So those are all classes with containers. All those planets and moons can be seen as simply "body with gravity" so that's a super class to them, while each has its own properties which become member variables. Physics I'll cover in the next comment. –  Patrick Hughes Aug 22 '11 at 18:08
add comment

You need to define "accurate". Even Newtonian Mechanics is not accurate enough to simulate Mercury properly. Newton is off by 43 arc seconds per tropical century. This was discovered with technics used in 1859, so its not a small deviance. In fact this was the first test case for Einstein's Relativity Theory, and he succeeded explaining mercuries perihelion precession using this theory. Most likely you don't want to use Relativity Theory for your simulation, so you probably need to relax a bit on "accurate".

share|improve this answer
    
+1 so true of any physical simulation. Great point. –  Patrick Hughes Aug 20 '11 at 21:17
1  
Numerical N-body simulations and their accuracy are the only parts I'm already experienced in, because I graduated in Astrophysics. What I'm trying to figure out is how to set this up in an object-oriented architecture for a game. I will probably leave relativity out, because it's not very relevant for my purposes. –  Marco Giancotti Aug 22 '11 at 7:06
add comment

In PseudoCode

   GBody {
       Velocity as (x,y,z)
       Name
       Mass
       Acceleration as (x,y,z)
       Position as (x,y,z)
       Diameter

       void update(){
       //do update math here
       }
   }

   GBody Mercury=new GBody( Mass=10,name=Mercury,...)

   do{
     //iterate through GBodies
      Mercury.update()
    }while (sim is running)
  • gbody is gravitational body
  • use a list to keep all the masses you are tracking in the n-body problem.
  • the ship should probably be a camera with seperate physics. (wouldn't want to have the ship crash into the sun.
  • use simple spheres with bitmaps for the planets/moons etc.
  • to simplify the math sun is fixed at 0,0,0.
share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.