Creating a TPT clone

  • qolizion
    19th Mar 2017 Member 0 Permalink

    Hello,

    I want to implement my own version of The Powder Toy, for practicing my C++ and maybe to make it better, faster, cooler (if I ever get that good). I'm looking for resources on creating such a game using SDL (I'm willing to learning OpenGL if it ever get necessary).


    The resources I'm looking for are about how these kind of simulation can be implemented, effectively. Are there books on the subject of simulations like TPT, on fluid simulations ? Any tutorial I could use ? Which technical choice would bring which advantage and disadvantage ?

     

    I know the source code is available but it lacks comments. Was the game designed from the ground up to have that many elements ? Are there any optimization I could make ? How could it be made more realistic, what would be the drawback ? As long as it stays 2D.

     

    To the devs, if you could make it again from the start, what would you do differently ?

     

    Thanks you.

  • Emperor_Bowserinator
    19th Mar 2017 Member 0 Permalink

    @qolizion (View Post) Here are some things you can do to TPT to make it more realistic, but they are SUPER challenging and no one has figured them out yet.

    1. Multithread the simulation while keeping particle orders to not break saves and syncing all the different parts of the simulation.
    2. Recode the ENTIRE particle engine to allow better supports for particle groups and moving objects, and more realistic fluid dynamics.
    3. Allow graphics code to use the GPU (Toggleable to support everyone) while keeping the game open source and readily avaliable to everyone
    4. Add magnetic fields, higgs fields, isotopes and all the elements of the periodic table. Requires somehow keeping the game not laggy, increasing the element limit and rewriting save formats.

    As you can see most of these suggestions are not realistic. If you somehow figure out how to do ONE of them you'll be a TPT god. 

     

    However recoding TPT would require a recode of the entire particle engine. Seeing how complex the game already is, you'll probably be better off trying to mod TPT yourself first. Start with creating elements, then maybe editing the simulation and GUI. 

     

  • qolizion
    21st Mar 2017 Member 0 Permalink

    Thanks you for your reply. So I'm better off by moding it first in order to understand the logic behind it ?

     

    1. Basically, what is multithreaded (seems to be the gravity, and other things like the client), and what is not in it ?

    2. Making particle group would be interesting but also hard. What algorithms does the simulation use to update particles ?

    3. So basically the application is currently not using the GPU ?

    4. I would pay for this. But this would be a complex one, sine simulating atom by atom wouldn't be realistic, and most things are combination of all the elements.

     

    I'm going to take a more serious look at the code. It seems to lack comments though, so any indication on how overall things work would be great.

  • mark2222
    22nd Mar 2017 Member 0 Permalink

    @qolizion (View Post)

     

    Unless you're very familiar with software and have lots (LOTS) of free time, I'd suggest not taking Bowserinator's feature requests seriously.

     

    If you're going to read the source, the wiki has some information to guide you: https://powdertoy.co.uk/Wiki/W/Main_Page.html#Development_Guides. "Variables in Source" and "Functions in Source" are probably what you're looking for.

     

    I assume you're mainly interested in the simulation aspects. Almost all the general code relating to the simulation is in Simulation.cpp, the most important functions being BeforeSim and UpdateParticles (these are the ones that are run each frame). Particle-specific code is in the individual element files, and the wind simulation is in Air.cpp. If you'd like real-time help with the code, you can ask on IRC. Don't worry about the lack of comments; the code is clean enough to understand without them, at least with some effort.

     

    I would think, though, that it'd be more instructive (and fun) if you wrote your clone from scratch, assuming you have the time. There's nothing all that advanced about TPT.

     

    There are tons of tutorials for SDL online. Given that it's mostly boilerplate, you probably won't go wrong following any of them. If you're interested in the fluid simulation, look for books specifically for games, like the one by Bridson. Industrial CFD techniques are a lot more accurate but are too slow for computer games.

     

    The graphics code already uses the GPU, though the gravity and fluid simulation might benefit from GPU acceleration. I would think that the bottleneck is still the particle updates, though, and you can't really process those in parallel without messing up particle update order. Unfortunately, almost all electronics saves depend on update order, and no one would be happy if that messes up. A TPT clone without such compatibility requirements would probably benefit from having particles processed in parallel right from the beginning. Speaking of compatibility, I'm sure there are a lot of things the devs wished were different, like the glut of useless elements and electronics inconsistencies.

     

    Interestingly, the game was designed from the beginning with a hard cap on the number of elements, which probably seemed like a lot then but annoys everyone now.

     

    By the way, TPT was never designed to be realistic, and will never be.

     

    Have fun with your project.

  • Aozora
    22nd Mar 2017 Member 0 Permalink

    Emperor_Bowserinator:

    Add magnetic fields, higgs fields, isotopes and all the elements of the periodic table. Requires somehow keeping the game not laggy, increasing the element limit and rewriting save formats.

     

    This one would be the easiest one code-wise, I think.

    We have newtonian gravity already, right? We just need an EM field and change PROT, NEUT, and ELEC to act more like GRVT. That way we don't need to make all the elements of the periodic table.

    Edited 3 times by Aozora. Last: 22nd Mar 2017
  • jacob2
    22nd Mar 2017 Member 0 Permalink
    Can't respond to everything right now, but I'd just like to say that TPT doesn't use the GPU, and is still mostly single threaded besides gravity and some misc UI stuff.

    And I might raise the element cap sometime... It isn't that easy