# Water Physics In a Tile Based Platformer

Im adding water physics to my XNA C# game, it is Tile based (grid System).

I asked a question about this before. But I actually attempted it and it seems easier than I thought, I already got some water to flow. But My problem is, When it hits a wall, I need it to go up. And I need a way to get the position of the last tile on the column/row So I can check if its empty or filled

```      [W]
[W][W][W][B]
[B][B][B][B][B]
Should be...

[W][W][W][W][B]
[B][B][B][B][B]
```

etc, and I need to get the end of a column so I can add to it, ex:

```   [W]
[W]
[W]

[B]   [B]
[B]   [B]
[B][W][B]
[B][W][B]
[B][B][B]
Should be

[W]
[B][W][B]
[B][W][B]
[B][W][B]
[B][W][B]
[B][B][B]
```

Right now my method cant stack them like that

What Ive done so far is...

``` private void UpdateWater()
{ // Calculate the visible range of tiles.
int left = (int)Math.Floor(cameraPosition / 16);
int right = left + 1024 / 16;
right = Math.Min(right, Width - 1);

int top = (int)Math.Floor(cameraPositionYAxis / 16);
int bottom = top + 768 / 16;
bottom = Math.Min(bottom, Height - 1);

//For Each tile on screen ///WATER CODE IS BELOW///
for (int y = top; y < bottom; ++y)
{
for (int x = left; x < right; ++x)
{
if (tiles[x, y].blockType.Name == "Water")
{
if (tiles[x, y + 1].blockType.Name == "Blank")
{
tiles[x, y ] = new Tile(BlockType.Blank, null, x,y );

tiles[x, y + 1] = new Tile(BlockType.Water, null, x, y + 1);
}
else if ((tiles[x + 1, y].blockType.Name != "Blank") && (tiles[x + 1, y].blockType.Name != "Water"))
{

}
else if ((tiles[x - 1, y].blockType.Name != "Blank") && (tiles[x - 1, y].blockType.Name != "Water"))
{

}

else
{

tiles[x, y] = new Tile(BlockType.Blank, null, x, y);
tiles[x - 1, y] = new Tile(BlockType.Water, null, x - 1, y);

}
}
}
}
}
```

I'm still probably considered a "novice" coder, any help is appreciated! If you need any more info or clarification, Please tell!

EDIT: Maybe I need a Pressure parameter for Water tiles? Or not... Trying to figure this out now.. still having a little trouble with the stacking.

Are you trying to simulate something resembling real physics, or something more like "Boulderdash" physics? If the latter, I would suggest that you might want to play around with different approaches to see what "feels" right; I'd suggest that instead of thinking in terms of "pressure", you think in terms of volume, assigning each cell a value from e.g. 1/8 full to completely full. Then on each movement frame perform the following two operations:

1. From bottom to top, if a cell which contains water is over a cell which could contain water but is not full, move as much water as can be moved from the upper cell to the lower one.
2. From bottom to top, for each cell which contains water, if the cell below cannot accommodate any more water, transfer 1/4 of the 'starting amount' of water to each side if there's room.

I'd guess that performing those two steps every frame should probably yield some reasonable-looking water effects. They won't represent anything remotely resembling real physics, but for a lot of platform games, playability is more important then realism.

Here is a link to some example code I wrote.

http://youtu.be/ZXPdI0WIvw0