I'm trying to simulate fluid / gas mechanics in an array. The centre of the array has a large value, topped up to max constantly and I want it to spread out from that point.
My first attempt was to run through the array and on each square, test the 4 neighbouring squares to see if they're lower. If they are, the final value of the square + lower neighbours is averaged out, so e.g If this square has 1000 fluid, one neighbouring square has zero (others are higher so ignored), then this square and the lower neighbour now have 500 each.
This sort of works and does have the advantage that the values 'flow' through the array and around objects. But it also has 2 problems.
Firstly, if I just run through the array with a foreach, it goes top-to-bottom/left-to-right and there's an obvious bias in certain directions.
Secondly, and more fatally, some of the outside edges never receive anything, even if I use standard values with lots of decimal places. They don't even receive a tiny fraction. So somewhere along the way, the math obviously balances out and the flow is no longer pushing 'forwards'.
One solution that I can think of in my head is to even out every square that's connected, all at the same time. Problem is, working out which squares are connected would probably require some sort of searching algorithm and I don't know how that would work. Plus that would lose the 'flow' feel, which I like.