Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cell-Based Liquid Simulation: Local pressure model?

Tags:

c#

simulation

I'm attempting to add semi-realistic water into my tile-based, 2D platformer. The water must act somewhat lifelike, with a pressure model that runs entirely local. (IE. Can only use data from cells near it) This model is needed because of the nature of my game, where you cannot be certain that the data you need isn't inside an area that isn't in memory.

I've tried one method so far, but I could not refine it enough to work with my constraints.

For that model, each cell would be slightly compressible, depending on the amount of water in the above cell. When a cell's water content was larger than the normal capacity, the cell would try to expand upwards. This created a fairly nice simulation, abeit slow (Not lag; Changes in the water were taking a while to propagate.), at times. When I tried to implement this into my engine, I found that my limitations lacked the precision required for it to work. I can provide a more indepth explanation or a link to the original concept if you wish.

My constraints:

  • Only 256 discrete values for water level. (No floating point variables :( ) -- EDIT. Floats are fine.
  • Fixed grid size.
  • 2D Only.
  • U-Bend Configurations must work.

The language that I'm using is C#, but I can probably take other languages and translate it to C#.

The question is, can anyone give me a pressure model for water, following my constraints as closely as possible?

like image 509
Steffan Donal Avatar asked May 24 '11 11:05

Steffan Donal


2 Answers

How about a different approach?

Forget about floats, that's asking for roundoff problems in the long run. Instead, how about a unit of water?

Each cell contains a certain number of units of water. Each iteration you compare the cell with it's 4 neighbors and move say 10% (change this to alter the propagation speed) of the difference in the number of units of water. A mapping function translates the units of water into a water level.

To avoid calculation order problems use two values, one for the old units, one for the new. Calculate everything and then copy the updated values back. 2 ints = 8 bytes per cell. If you have a million cells that's still only 8mb.

If you are actually trying to simulate waves you'll need to also store the flow--4 values, 16 mb. To make a wave put some inertia to the flow--after you calculate the desired flow then move the previous flow say 10% of the way towards the desired value.

like image 196
Loren Pechtel Avatar answered Nov 14 '22 12:11

Loren Pechtel


Try treating each contiguous area of water as a single area (like flood fill) and track 1) the lowest cell(s) where water can escape and 2) the highest cell(s) from which water can come, then move water from the top to the bottom. This isn't local, but I think you can treat the edges of the area you want to affect as not connected and process any subset that you want. Re-evaluate what areas are contiguous on each frame (re-flood on each frame) so that when blobs converge, they can start being treated as one.

Here's my code from a Windows Forms demo of the idea. It may need some fine tuning, but seems to work quite well in my tests:

public partial class Form1 : Form
{
  byte[,] tiles;
  const int rows = 50;
  const int cols = 50;
  public Form1()
  {
     SetStyle(ControlStyles.ResizeRedraw, true);
     InitializeComponent();
     tiles = new byte[cols, rows];
     for (int i = 0; i < 10; i++)
     {
        tiles[20, i+20] = 1;
        tiles[23, i+20] = 1;
        tiles[32, i+20] = 1;
        tiles[35, i+20] = 1;
        tiles[i + 23, 30] = 1;
        tiles[i + 23, 32] = 1;
        tiles[21, i + 15] = 2;
        tiles[21, i + 4] = 2;
        if (i % 2 == 0) tiles[22, i] = 2;
     }
     tiles[20, 30] = 1;
     tiles[20, 31] = 1;
     tiles[20, 32] = 1;
     tiles[21, 32] = 1;
     tiles[22, 32] = 1;
     tiles[33, 32] = 1;
     tiles[34, 32] = 1;
     tiles[35, 32] = 1;
     tiles[35, 31] = 1;
     tiles[35, 30] = 1;
  }

  protected override void OnPaint(PaintEventArgs e)
  {
     base.OnPaint(e);
     using (SolidBrush b = new SolidBrush(Color.White))
     {
        for (int y = 0; y < rows; y++)
        {
           for (int x = 0; x < cols; x++)
           {
              switch (tiles[x, y])
              {
                 case 0:
                    b.Color = Color.White;
                    break;
                 case 1:
                    b.Color = Color.Black;
                    break;
                 default:
                    b.Color = Color.Blue;
                    break;
              }
              e.Graphics.FillRectangle(b, x * ClientSize.Width / cols, y * ClientSize.Height / rows,
                 ClientSize.Width / cols + 1, ClientSize.Height / rows + 1);
           }
        }
     }
  }

  private bool IsLiquid(int x, int y)
  {
     return tiles[x, y] > 1;
  }

  private bool IsSolid(int x, int y)
  {
     return tiles[x, y] == 1;
  }

  private bool IsEmpty(int x, int y)
  {
     return IsEmpty(tiles, x, y);
  }

  public static bool IsEmpty(byte[,] tiles, int x, int y)
  {
     return tiles[x, y] == 0;
  }

  private void ProcessTiles()
  {
     byte processedValue = 0xFF;
     byte unprocessedValue = 0xFF;

     for (int y = 0; y < rows; y ++)
        for (int x = 0; x < cols; x++)
        {
           if (IsLiquid(x, y))
           {
              if (processedValue == 0xff)
              {
                 unprocessedValue = tiles[x, y];
                 processedValue = (byte)(5 - tiles[x, y]);
              }
              if (tiles[x, y] == unprocessedValue)
              {
                 BlobInfo blob = GetWaterAt(new Point(x, y), unprocessedValue, processedValue, new Rectangle(0, 0, 50, 50));
                 blob.ProcessMovement(tiles);
              }
           }
        }
  }

  class BlobInfo
  {
     private int minY;
     private int maxEscapeY;
     private List<int> TopXes = new List<int>();
     private List<int> BottomEscapeXes = new List<int>();
     public BlobInfo(int x, int y)
     {
        minY = y;
        maxEscapeY = -1;
        TopXes.Add(x);
     }
     public void NoteEscapePoint(int x, int y)
     {
        if (maxEscapeY < 0)
        {
           maxEscapeY = y;
           BottomEscapeXes.Clear();
        }
        else if (y < maxEscapeY)
           return;
        else if (y > maxEscapeY)
        {
           maxEscapeY = y;
           BottomEscapeXes.Clear();
        }
        BottomEscapeXes.Add(x);
     }
     public void NoteLiquidPoint(int x, int y)
     {
        if (y < minY)
        {
           minY = y;
           TopXes.Clear();
        }
        else if (y > minY)
           return;
        TopXes.Add(x);
     }
     public void ProcessMovement(byte[,] tiles)
     {
        int min = TopXes.Count < BottomEscapeXes.Count ? TopXes.Count : BottomEscapeXes.Count;
        for (int i = 0; i < min; i++)
        {
           if (IsEmpty(tiles, BottomEscapeXes[i], maxEscapeY) && (maxEscapeY > minY))
           {
              tiles[BottomEscapeXes[i], maxEscapeY] = tiles[TopXes[i], minY];
              tiles[TopXes[i], minY] = 0;
           }
        }
     }
  }

  private BlobInfo GetWaterAt(Point start, byte unprocessedValue, byte processedValue, Rectangle bounds)
  {
     Stack<Point> toFill = new Stack<Point>();
     BlobInfo result = new BlobInfo(start.X, start.Y);
     toFill.Push(start);
     do
     {
        Point cur = toFill.Pop();
        while ((cur.X > bounds.X) && (tiles[cur.X - 1, cur.Y] == unprocessedValue))
           cur.X--;
        if ((cur.X > bounds.X) && IsEmpty(cur.X - 1, cur.Y))
           result.NoteEscapePoint(cur.X - 1, cur.Y);
        bool pushedAbove = false;
        bool pushedBelow = false;
        for (; ((cur.X < bounds.X + bounds.Width) && tiles[cur.X, cur.Y] == unprocessedValue); cur.X++)
        {
           result.NoteLiquidPoint(cur.X, cur.Y);
           tiles[cur.X, cur.Y] = processedValue;
           if (cur.Y > bounds.Y)
           {
              if (IsEmpty(cur.X, cur.Y - 1))
              {
                 result.NoteEscapePoint(cur.X, cur.Y - 1);
              }
              if ((tiles[cur.X, cur.Y - 1] == unprocessedValue) && !pushedAbove)
              {
                 pushedAbove = true;
                 toFill.Push(new Point(cur.X, cur.Y - 1));
              }
              if (tiles[cur.X, cur.Y - 1] != unprocessedValue)
                 pushedAbove = false;
           }
           if (cur.Y < bounds.Y + bounds.Height - 1)
           {
              if (IsEmpty(cur.X, cur.Y + 1))
              {
                 result.NoteEscapePoint(cur.X, cur.Y + 1);
              }
              if ((tiles[cur.X, cur.Y + 1] == unprocessedValue) && !pushedBelow)
              {
                 pushedBelow = true;
                 toFill.Push(new Point(cur.X, cur.Y + 1));
              }
              if (tiles[cur.X, cur.Y + 1] != unprocessedValue)
                 pushedBelow = false;
           }
        }
        if ((cur.X < bounds.X + bounds.Width) && (IsEmpty(cur.X, cur.Y)))
        {
           result.NoteEscapePoint(cur.X, cur.Y);
        }
     } while (toFill.Count > 0);
     return result;
  }

  private void timer1_Tick(object sender, EventArgs e)
  {
     ProcessTiles();
     Invalidate();
  }

  private void Form1_MouseMove(object sender, MouseEventArgs e)
  {
     if (e.Button == MouseButtons.Left)
     {
        int x = e.X * cols / ClientSize.Width;
        int y = e.Y * rows / ClientSize.Height;
        if ((x >= 0) && (x < cols) && (y >= 0) && (y < rows))
           tiles[x, y] = 2;
     }
  }
}
like image 2
BlueMonkMN Avatar answered Nov 14 '22 13:11

BlueMonkMN