Air & pressure

  • QuanTech
    5th Dec 2016 Member 0 Permalink

    I've checked the file 'Air.cpp' and it really confuses me. I want to find out how pressure works in this game. Can someone "translate" this c++ code into English for me??? Thanks.

    EDIT: I probably should've put this in my 'Pressure Cells' thread. Stupid me

    Edited once by QuanTech. Last: 5th Dec 2016
  • jacob1
    6th Dec 2016 Developer 0 Permalink
    Well the function you want to look at is update_air

    I might not be the best one to explain it but I can try. To start, the air simulation involves both pressure and velocity. These are separate maps which are linked together. You can see them using 1 / 2 (velocity / pressure) view modes, they match each other in movement mostly, because they both feed off each other.


    There are some loops that say //reduces pressure/velocity on the edges every frame, that does exactly what it says, it decreases the pressure on the outer edges.

    Underneath that it "//clear some velocities near walls" which is again exactly what it says.

    After that is "//pressure adjustments from velocity" and "//velocity adjustments from pressure". The first uses velocity to modify pressure and mostly make it move in the direction velocity moves in. It also decreases pressure here a little, I think. The second does the same thing, it has velocity move in the direction of pressure differences.

    Then, "//update velocity and pressure" is the main important thing. There is a double for loop that uses "kernel", which is some really fancy thing I don't understand but I think adds some amount of variation in the pressure grid, so that it isn't always perfectly uniform and boring. It might have some other uses?
    Then it ... does some other stuff with velocity. I think it uses the velocity and tries to look that far in whatever direction the velocity is going, to update pressure/velocity from the current position. I don't fully understand this part either. There is a long comment about a bugfix we made, where velocity could move through walls. I think another bug like this still technically exists.


    Finally, there is "switch (airMode)", which will use air mode to just completely nullify pressure or velocity if necessary. And finally, it updates the actual pressure and velocity grids. Before this it uses a temporary copy to make all the calculations and store them in a new grid.
    Edited once by jacob1. Last: 6th Dec 2016
  • QuanTech
    6th Dec 2016 Member 0 Permalink

    Ah that explanation was awesome! Thank you

  • jacksonmj
    6th Dec 2016 Developer 0 Permalink

    There's a rewritten version which has been split up into separate functions and has a few more comments at https://github.com/jacksonmj/The-Powder-Toy/blob/master/src/simulation/air/AirSimulator_v1.cpp#L611

     

    The kernel thing is a 2D Gaussian blur which skips air blocking walls.