title: XNA Waterphysics Top-Down Hex-based grid
body: <p>I'm working on a project using C#/XNA and I'm having trouble creating water-physics in an top-down hex-based grid.</p>
  
  <p>We're using an hex-tilemap with help of: <a href="http://www.redblobgames.com/grids/hexagons" rel="nofollow">http://www.redblobgames.com/grids/hexagons</a>.
  So I thought I could implement an algoritm for the water flow, but I can't seem to get it right, and it seems to be very performance-heavy.</p>
  
  <pre><code>/// &lt;summary&gt;
  /// This method will level all the water to the same height. It does so by looking arround and make them all even
  /// &lt;/summary&gt;
  /// &lt;param name="tiles"&gt;The tile array needed to level arround&lt;/param&gt;
  public void Flow(List&lt;Tile&gt; tiles, Tilemap tileMap)
  {
      float waterAmountEachTile;
      List&lt;Water&gt; waterTiles = new List&lt;Water&gt;(7);
      //include self
      waterTiles.Add(this);
      float waterAmount = (this.waterHeight + this.ZPos);
  
      for (int i = 0; i &lt; tiles.Count; i++)//first loop to get all values
      {
          if (tiles[i].GetType() == typeof(Water))
          {
              waterTiles.Add((Water)tiles[i]);//check wich tiles are water and put them in a new array
              waterAmount += (waterTiles[waterTiles.Count - 1].waterHeight + waterTiles[waterTiles.Count - 1].ZPos);  //Increase the ammount - debuggen later werkt count goed
          }
      }
  
      waterAmountEachTile = waterAmount / waterTiles.Count; //Calculate how high each tile should be ( we need this for drycheck)
      dryCheck(ref waterAmount, waterTiles, waterAmountEachTile, tileMap);
      waterAmountEachTile = waterAmount / waterTiles.Count; //recalculate the ammount for each tile
  
      foreach (Water waterTile in waterTiles) //second loop to adjust the tile to the according hight
      {
          waterTile.waterHeight = (waterAmountEachTile - waterTile.ZPos);
      }
  }
  
  /// &lt;summary&gt;
  /// Checks if the tile should be dry or continue being a water tile.
  /// &lt;/summary&gt;
  /// &lt;param name="waterAmount"&gt; the ammount of water to divide among the tiles&lt;/param&gt;
  /// &lt;param name="waterTiles"&gt;The watertiles list to do the drycheck on&lt;/param&gt;
  /// &lt;param name="waterAmountEachTile"&gt;The height to set each water tile&lt;/param&gt;
  /// &lt;returns&gt;&lt;/returns&gt;
  private void dryCheck(ref float waterAmount, List&lt;Water&gt; waterTiles, float waterAmountEachTile, Tilemap tileMap)
  {
      //TODO dit fixen
      for (int i = 0; i &lt; waterTiles.Count; i++)
      {
          if (waterTiles[i].ZPos &gt; waterAmountEachTile) //is grond hoger dan water
          {
              waterAmount -= waterTiles[i].ZPos;
              tileMap.TileMap[waterTiles[i].XPos][waterTiles[i].YPos] = new Ground(this.graphics, waterTiles[i].XPos, waterTiles[i].YPos,
                  waterTiles[i].ZPos, this.size, Tilemap.HexVertices);
  
              waterTiles.Remove(waterTiles[i]);
              i--;
          }
      }
  }
  </code></pre>
  
  <p>Now for my question, does any of you know of a way to implement water-physics in a top-down environment, preferably with hex-based grids.</p>
  
  <p>I've looked in to several liberaries, and found Smoothed-particle hydrodynamics, but I'm not sure if it's implementable top-down, and I can't seem to find any guides in that direction.</p>
  
  <p>Any help would be great, even some pointers might be enough.</p>
  
  <p>Thanks in advance,
  C. Venhuizen</p>