问题描述
|
我正在尝试将半现实的水添加到基于图块的2D平台器中。水必须表现得栩栩如生,其压力模型必须完全在本地运行。 (即,只能使用来自其附近单元的数据。)由于我的游戏的性质,因此需要这种模型,在这种情况下,您不能确定所需的数据不在内存中没有的区域内。
到目前为止,我已经尝试了一种方法,但是我无法对其进行足够的改进以适应我的约束。
对于该模型,取决于上面的单元格中的水量,每个单元格都可以稍微压缩。当一个单元格的含水量大于正常容量时,该单元格会尝试向上扩展。这有时会创建一个相当不错的模拟,尽管速度很慢(没有滞后;水中的变化需要一段时间才能传播)。当我尝试将其实现到引擎中时,我发现自己的局限性不足以使其无法正常工作。如果您愿意,我可以提供更深入的解释或原始概念的链接。
我的约束:
水位只有256个离散值。 (没有浮点变量:()-编辑。浮点数很好。
固定网格大小。
仅2D。
U弯配置必须正常工作。
我使用的语言是C#,但我可能可以使用其他语言并将其翻译为C#。
问题是,有人能尽可能严格地遵循我的约束条件,给我一个水的压力模型吗?
解决方法
尝试将每个连续的水区域视为一个区域(例如洪水填充),并跟踪1)可以漏水的最低单元格和2)可以从中流出水的最高单元格,然后将水从从上到下。这不是本地的,但我认为您可以将要影响的区域的边缘视为未连接,并处理所需的任何子集。重新评估每个帧上哪些区域是连续的(在每个帧上重新淹没),以便当斑点聚集时,它们可以开始被视为一个区域。
这是我从Windows Forms演示中获得的代码。它可能需要进行一些微调,但在我的测试中似乎工作得很好:
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,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,y] == 1;
}
private bool IsEmpty(int x,int y)
{
return IsEmpty(tiles,x,y);
}
public static bool IsEmpty(byte[,] tiles,int 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,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;
}
}
}
,如何使用另一种方法?
忘掉浮动,从长远来看,这就是要求四舍五入的问题。相反,一单位水怎么样?
每个单元包含一定数量的水。每次迭代时,您将单元与它的4个邻居进行比较,并说出单位水量差的10%(更改此值以更改传播速度)。映射功能将水的单位转换为水位。
为避免计算顺序问题,请使用两个值,一个用于旧单位,一个用于新单位。计算所有内容,然后将更新后的值复制回去。 2个整数=每个单元8个字节。如果您有一百万个单元,那么仍然只有8mb。
如果您实际上是在尝试模拟波浪,则还需要存储flow-4值16 mb。为了使波浪产生一定的惯性-在计算了所需的流量之后,将前一个流量向所需值的方向移动10%。
,从流体动力学的观点来看,一种相当流行的基于晶格的算法家族是所谓的莱迪思·玻尔兹曼方法。一个简单的实现应该相对简单和快速,并且忽略所有使学者感到高兴的细节,并且应该具有合理正确的动力。