You need to enable JavaScript to run this app. 如何修复C++/SDL2 Falling Sand模拟水体流动左偏向与消失Bug
优惠活动
大模型
产品
解决方案
定价
更多

如何修复C++/SDL2 Falling Sand模拟水体流动左偏向与消失Bug

奇怪水体物理效果演示

我目前在用C++和SDL2开发一款简易的「落沙(Falling Sand)」模拟游戏,在实现更真实的水体流动效果时遇到了问题。我用网格单元存储模拟状态,按从下到上、从左到右的顺序遍历单元格;检测到WATER类型单元格时,会依次检查它下方、左下方、右下方、左侧、右侧是否为EMPTY空单元格,找到第一个符合条件的位置就移动过去(如果两个对角方向或两个水平方向都空闲,就随机选一个),同时把移动后的单元格标记为已处理,避免本轮循环重复检测。

当前核心问题是粒子移动存在左偏向:如果在障碍物上方生成一块WATER区域,粒子接触障碍物后会整体向左偏移,右侧的单元格按正常方式下落,最终没法形成向两侧均匀流淌的三角形形态,整个水体只会向左移动。如果把遍历顺序改成从右到左,偏向效果就会反转,说明问题和遍历逻辑相关;但我测试了好几个小时,一开始怀疑是已处理单元格标记系统的问题,没发现明显bug,至今没定位到原因。想问问有没有开发者做这类模拟系统时碰到过类似问题,或者我是不是漏了什么关键逻辑,求解决思路。

补充说明:
我最近取得了一些进展,把原逻辑改成:先复制旧的单元格状态(m_oldCells),基于旧状态判断更新操作,再更新原单元格(m_cells)并渲染。这个优化已经让SAND的模拟效果改善了很多,但WATER在执行水平移动时会出现「消失」的bug,我测试了一上午都没找到解决方案。一开始我怀疑是数组复制逻辑的问题,但检查下来复制操作运行正常。以下是相关代码片段:

Simulation.cpp

void Simulation::update()
{
    copyStates(m_cells, m_oldCells); // 此时oldcells存储上一帧的最新状态

    for(int y = m_height - 1; y >= 0; y--)
    for(int x = 0; x < m_width; x++)
        {
            Cell* c = getOldCell(x, y); // 基于旧状态判断是否需要更新
            switch(c->m_type)
            {
                case EMPTY:
                    break;
                case SAND:
                    if(c->m_visited == false) update_sand(x, y);
                    break;
                case WATER:
                    if(c->m_visited == false) update_water(x, y);
                    break;
                default:
                    break;
            }
        }
}

void Simulation::update_water(int x, int y)
{
    bool down = (getOldCell(x, y+1)->m_type == EMPTY) && checkBounds(x, y+1) && !getOldCell(x, y+1)->m_visited;
    bool d_left = (getOldCell(x-1, y+1)->m_type == EMPTY) && checkBounds(x-1, y+1) && !getOldCell(x-1, y+1)->m_visited;
    bool d_right = (getOldCell(x+1, y+1)->m_type == EMPTY) && checkBounds(x+1, y+1) && !getOldCell(x+1, y+1)->m_visited ;
    bool left = (getOldCell(x-1, y)->m_type == EMPTY) && checkBounds(x-1, y) && !getOldCell(x-1, y)->m_visited ;
    bool right = (getOldCell(x+1, y)->m_type == EMPTY) && checkBounds(x+1, y) && !getOldCell(x+1, y)->m_visited ;

    // 两个方向都可移动时随机选择
    if(d_left && d_right)
    {
        int r = rand() % 2;
        if(r) d_right = false;
        else d_left = false;
    }

    if(left && right)
    {
        int r = rand() % 2;
        if(r) right = false;
        else left = false;
    }
    
    if(down)
    {
        getCell(x, y+1)->m_type = WATER; // 更新新状态
        getOldCell(x, y+1)->m_visited = true; // 标记为已处理,本轮循环不再检测
    } else if(d_left)
    {
        getCell(x-1, y+1)->m_type = WATER;
        getOldCell(x-1, y+1)->m_visited = true;
    } else if(d_right)
    {
        getCell(x+1, y+1)->m_type = WATER;
        getOldCell(x+1, y+1)->m_visited = true;
    } else if(left)
    {
        getCell(x-1, y)->m_type = WATER;
        getOldCell(x-1, y)->m_visited = true;
    } else if(right)
    {
        getCell(x+1, y)->m_type = WATER;
        getOldCell(x+1, y)->m_visited = true;
    }
    
    // 如果发生了移动,原单元格置为空,更新新状态
    if(down || d_right || d_left || left || right)
    {
        getCell(x, y)->m_type = EMPTY;
    }
}

void Simulation::copyStates(Cell* from, Cell* to)
{
    for(int x = 0; x < m_width; x++)
    for(int y = 0; y < m_height; y++)
    {
        to[x + y * m_width].m_type = from[x + y * m_width].m_type;
        to[x + y * m_width].m_visited = from[x + y * m_width].m_visited;
    }
}

Main.cpp

sim.update();

Uint32 c_sand = 0xedec9a00;
for(int y = 0; y < sim.m_height; y++)
for(int x = 0; x < sim.m_width; x++)
{
    sim.getCell(x, y)->m_visited = false;
    if(sim.getCell(x, y)->m_type == 0) screen.setPixel(x, y, 0);
    if(sim.getCell(x, y)->m_type == 1) screen.setPixel(x, y, c_sand);
    if(sim.getCell(x, y)->m_type == 2) screen.setPixel(x, y, 0x0000cc00);
}

screen.render();

从附的演示动图可以看到,沙粒的放置效果正常,但水生成后会出现异常的流动模式——和沙粒不同,水生成后会整体向左偏移。


内容的提问来源于stack exchange,提问作者James Mclaughlin

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.31 07:36:00