How to wisely update body trajectories in a 3D space simulation game?

Marco Giancotti
  • How to wisely update body trajectories in a 3D space simulation game? Marco Giancotti

    Suppose I have a Spacecraft object in 3D space, controllable by the player. I want it to update its own trajectory, so I give it a function for that (actually it might be inside a controller component associated with it). No problem until this point.

    For this function I want to use a scientifically tested, accurate N-body simulation algorithm (like this). It's my field so I already know how to do that.

    The challenge is how to make the game activate the function efficiently. This is what I've thought of so far:

    1. The algorithm can calculate a lot of time steps in a short time, so I could activate it in one loop, save the trajectory data, and go on for several game loops using that data. During this time the spacecraft's position is updated every loop, but the algorithm is sleeping.
    2. When the predefined number of loops has passed, or when the user gives a movement command, the algorithm is activated again, producing the new portion of the trajectory to use next.
    3. This might require double-buffering.

    The question is: who should be responsible for activating the spacecraft's algorithm? Should I do that directly in the game loop, or perhaps create a "timer object" that keeps track of the number of loops and user input and calls the function when appropriate? Something else?

    Note that the same thing would happen for all the other bodies orbiting in space. However the other bodies would simply have to follow predefined trajectories, no need for an algorithm. The timer object could take care of updating all the trajectories at the right times.

    Also, if you think there is a totally different and better way to do this, please let me know.

    Additional info: the game is in C++, and the graphics engine is Ogre3D.

    EDIT: changed the part about the trajectories of the non-interactive bodies, because comments here made me realize that it was plain stupid.

  • This is a interesting software engineering problem.

    First of all you need to identify what entities are involved (entities broadly speaking) and assign them responsibilities (-bility is better) and knowledge about the other entities (less is better).

    Here I can see: your trajectory algorithm, the generic position updater, the user interface or user control subsystem.

    Your algorithm does not need to know anything about user control subsystem or frame related updating stuff so it should be highly decoupled about the rest: it should keep the environment status and output the trajectory information and time validity by its own. You should implement it so you can run it without any game related component. This make your life easier in debugging and unit-testing phases.

    Your algorithm needs a bridge to let its outputs be used for your ludic purposes. This kind of bridge should be implemented in a way so a different algorithm may be used with easy.

    The interface side of the bridge (the side that will be used by the game) should give to its consumers a way observe the relevant environmental status (this prescinds the particular trajectory algorithm) and the user input while providing a query interface that can return a positional status, given a particular time coordinate.

    The implementation side of the bridge should be aware of the trajectory algorithm: this encapsulates how you transform a positional query to a positional computation or a projection of your trajectory).

    A possible implementation of the query functionality can be that the bridge uses the time coordinate along with the current computed trajectory to compute the position. If the trajectory is invalid than a new one is computed: a stored trajectory becomes invalid if used outside its time range of validity or if the environmental update methods detect an action by the user.

    About the way the non-interactive objects trajectory are computed, keep in mind that probably everything is better than read through a file. Here we are talking about to trade-off disk usage, memory occupation and CPU allocation.

    The answer to this trade-off is simple "compress": compressing basically means to store a fraction of the whole data and use CPU and RAM to transform compressed data into real and useful data.

    In your case, the knowledge of the data is the key: you can start by identify the minimum data that is required to compute your trajectory; how you can translate it into a real(raster) path?

    If the CPU usage for this is acceptable you are done, if not (as it should be reading your question) you need to look for where CPU intensive temporary data are computed.

    This kind of data can be stored along with the basic trajectory parameters to speed up the trajectory reconstruction (memoization-like). You may preload these data if the ratio memory_usage/usage_frequency is good or avoid it and discard used data(hence re-read if re-needed).

    In a nutshell: your custom specialized compression algorithm has to be tuned to achieve the better CPU/RAM/DISK trade-off.

c++ game-loop simulations
Related questions and answers
  • in the main function * not Finally.. but i believe that te numb_coll problem has something to do with the time and framrate. When running full speed i quickly die upon entering a wall as numb_coll... until you pop out into an open space, known as a ' ' (space) character. You lose one life for each INITIAL collision with the X wall, and do not lose another until you have popped out and been freed from it. As an added touch i have made it so after you collide while traveling down the randomly generated map (or rather as the walls move uppward while your character stays put) the X chars you've

  • hey so I've decided to Code my own 2D soft-body physics engine in C++ since apparently none exist and I'm starting only with a general idea/understanding on how physics work and could be simulated... examples/sites that could give me the specifics needed to actually make this such as equations and required physics knowledge. It would be great if anyone out there also would give me their attempts or ideas. finally I was wondering if it was possible to... use the source code of an existing 3D engine such as Bullet and transform it to be 2D based? use the source code of a 2D Rigid body physics

  • on exporting animation information, especially when the mesh changes its geometry? I would be thankful for advices that could point me into right direction. It would be nice to save some time instead...After careful consideration to use middleware, I have decided on creating my own 3d file format format to export meshes from 3D authoring application (Softimage) into my game. I will need to export the following: Vertices Indices Normals UVs Material Information Animation information (no clue, how to import it) Collision mesh Game Properties defined within 3D authoring tool (object

  • , that will let me import 3d meshes into my game, ready to use? The solution/middleware should be: easy to use easy to port efficient not consuming too much memory with unnecessary things containing all...Say I develop a game for mobile platform running OpenGL ES 2.0. I have done 2D part, and now I wish to import some 3D objects. The imported 3D objects must contain the following: Vertices positions Normals UVs Texturing information Animation information Collision mesh Maybe some other things... I am aware, that I could and (maybe should) create my own file format that brings these data from

  • I'm writing a game engine which is going very fine. However, I'm now posed with handling textures. My Engine is in 2D, for simplicity reasons mostly to get a good idea of how to work with OpenGL. I do this in C++, and I have a hierarchical set-up when it comes to classes. There's a base class and some other classes derive from that. So now I'm onto the part where I want to load up some textures... have to use glGenTextures to allocate a texture and that I then swap betwe--.. I actually have no clue. What's a good way to keep track of all the textures you loaded into the memory and switch

  • (indicating structure begin, structures end, etc.) and strings in a custom-defined format. What i want to know specifcally is how to do this with the STL and C++... Since the format is meant simply for the use of a game i would think it would be much easier though. The format should: Be traversable (i can look through it and find the start of structure and maybe check it's name Be able to hold multiple classes and data in a single file have identifiable starts and ends to sections: such as the space in text files Maybe have it's own icon to represent it?? How do i do this in c++ ?

  • Components. 3: Component registers itself in GameSubsystem(s). We know at compile-time that there is a GameSubsystemRenderer, so let's ComponentImageRender will call something like... GameSubsystems) can implement registerComponent(Component*). pro. Components and GameSubystems know nothing about each other. con. In C++ it would look like ugly and slow typeid-switch...I'm creating a component-based game object system. Some tips: GameObject is simply a list of Components. There are GameSubsystems. For example, rendering, physics etc. Each GameSubsystem contains

  • for other players should only be sent to a game client if their player is near the other players (i.e. you shouldn't get health updates for people you can't see on the other side of the map). The main problem is that if you had to compare all the players every time an update is sent, it would just take way too long as the number of players scales (think up to a few thousand players per map and each...? My idea is to have a separate thread that compares the space difference of all the players every few seconds or so and stores references to all nearby players in the player's object for easy access

  • know if my speculations are ok, as I don't have much experience with 3d animations yet. I want to make a well decision as any option I choose would require a lot of work to get it to render and I don't want to find out in the end that I have to rewrite everything again, as a lot of other objects will be working with these data. Sorry if it's a too subjective matter, but I need some insight. I'm... the Diffuse color variation or making it store any value that can change but making it so random that I woulnd't know how to relate the data to the other frames, or need a complex system for it. That's why

Data information