# Water Physics In top down 2D game

I have been making a game like Minecraft or Terraria in which you add blocks and such to the world, and I came across this interesting debacle, the game is in a top down 2D perspective, and I would like to make it so users can add water to the world, however I would also like the water to behave like water would in the real world.

I'm guessing the code would work something like

if (space next to water block is open)
{
}


However, this would not stop the water at that point, flooding the underpart of the map and creating little islands where the players have made things.

I could also pre-make the map, and put the water in that way, allowing the land mass of the map to go unscathed, however this prevents the players from making their own rivers and whatnot.

So my question is, do I prevent players from using water? Or do I allow them to and try to fix any disasters?

Would there be any use to code that allows me to limit the amount of water generated?

The reason water is important is due to the fact that several water vehicles are being planned, and water could be used as a resource to feed the player (dehydration etc)

EDIT: I have worked in water as follows. It the player will add the water like a block, at this point the water will spread in an equal outward direction.

If the water intersects ANY block it will stop and proceed with phase two, which is to move back and forth to simulate waves.

I found that through using XNA's ability to locate the side of the rectangle intersected, I can make it continue on down a length

For example:

 if (water.Intersects(something) && something.position.Y >= water.Top)
{
\\allow the water to continue adding to the width until the width hits something

}


Here is an image of the partially completed function in action

• Can you refer to a video that demonstrates the effect you are looking for? Sep 9, 2014 at 0:53
• Are you after something like the water physics in minecraft? (just in 2d)
– War
Sep 12, 2014 at 9:39
• Kinda, I'm looking for it to spread but look okay, I've almost worked it out Sep 13, 2014 at 22:01

I'm not quite clear on what you meant in your question, but I believe that limiting the spread of a particular water source would indeed solve your problem.

Perhaps something along the lines of giving it a height and maximum water limit when spawned, and decrementing the height and max water each block it goes along, with spreading to a lower block (you mentioned it was 2D top-down but gave the impression that there was height to blocks, is that correct?) giving it more height but not maximum water. Or just height if you think that would help solve your problem.

I say go ahead with water, just keep experimenting until you find a balance that suits your game.

PS: I'm not sure if it's relevant (I would add this as a comment if I had the rep to), but I personally loved the fact that in Minecraft an ill placed water block could cause havoc, those parts of the engine were what helped make it so fun imo.

• I was thinking something along the lines of that, I just didn't want Noah's Ark to happen XD Sep 9, 2014 at 0:30
• I thought that was what you meant, glad I wasn't off the mark. Dynamic water is problematic like that, I would suggest just tweaking the balance of starting height vs. dropoff per block until it's manageable and doesn't frustrate players. Or if you want to go the other way, punish the player for not planning ahead and make placed water spread a lot. Sep 9, 2014 at 0:34
• There is an impression of height to the game, which is done through the ordering of how things are drawn at gametime, blocks lower than the player are drawn first, then the player, then blocks that are higher. In this case, the water will be drawn below the player. Sep 9, 2014 at 1:13

I'd just like to add a couple items to consider. The first is the speed at which water will transfer to an adjacent empty area. For example maybe one block of water will transfer 1/3 of it's volume per second to every open block next to it. If you open a second space for water, it would take two seconds (or whatever a tick of game time is) to get an equal amount of water in each space. If two spaces are opened, you would have all three spaces at an equal amount in one tick.

There shouldn't be much reason to keep it out. The player will likely learn to be careful. I would limit the amount of water generated. If you want to allow for a flowing source of water, I would make sure that there is some sort of outlet for every inlet.

The last item I would consider is an amount of water that is considered negligible and should be destroyed. Essentially evaporation of a very thin layer of water.