2

我正在尝试创建一个基于体素的游戏,并且正在渲染数百万个立方体。

为了加快渲染速度,我将立方体组合成块,将 32x32x32 立方体组合成一个网格。这是为了减少对 GPU 的渲染调用次数并提高帧率。

我正在使用 ManualObject 来构建块,并且效果很好。但是,现在的问题是,由于各个块不是绑定到各个场景节点的实体,因此我找不到进行碰撞检测的方法。

食人魔有办法单独处理 ManualObject 的子网格吗?

// Build a face with triangles if the face is visible. Don't bother building faces for hidden faces.
void Chunk::createMesh()
{
    begin("BoxColor");

    int iVertex = 0;
    Block *block;
    Block *testingBlock;

    for (int x = 0; x < CHUNK_SIZE.x; ++x)
    {
        for (int y = 0; y < CHUNK_SIZE.y; ++y)
        {
            for (int z = 0; z < CHUNK_SIZE.z; ++z)
            {
                block = m_pBlocks[x][y][z];
                if (block == NULL) 
                {
                    continue;
                }

                    //x-1
                testingBlock = 0;
                if (x > 0) testingBlock = m_pBlocks[x-1][y][z];

                if (testingBlock == 0)
                {
                    position(x, y,   z+1);  normal(-1,0,0); textureCoord(0, 1);
                    position(x, y+1, z+1);  normal(-1,0,0); textureCoord(1, 1);
                    position(x, y+1, z);    normal(-1,0,0); textureCoord(1, 0);
                    position(x, y,   z);    normal(-1,0,0); textureCoord(0, 0);

                    triangle(iVertex, iVertex+1, iVertex+2);
                    triangle(iVertex+2, iVertex+3, iVertex);

                    iVertex += 4;
                }

                    //x+1
                testingBlock = 0;
                if (x < 0 + CHUNK_SIZE.x - 1) testingBlock = m_pBlocks[x+1][y][z];

                if (testingBlock == 0)
                {
                    position(x+1, y,   z);      normal(1,0,0); textureCoord(0, 1);
                    position(x+1, y+1, z);      normal(1,0,0); textureCoord(1, 1);
                    position(x+1, y+1, z+1);    normal(1,0,0); textureCoord(1, 0);
                    position(x+1, y,   z+1);    normal(1,0,0); textureCoord(0, 0);

                    triangle(iVertex, iVertex+1, iVertex+2);
                    triangle(iVertex+2, iVertex+3, iVertex);

                    iVertex += 4;
                }

                    //y-1
                testingBlock = 0;
                if (y > 0) testingBlock = m_pBlocks[x][y-1][z];

                if (testingBlock == 0)
                {
                    position(x,   y, z);        normal(0,-1,0);     textureCoord(0, 1);
                    position(x+1, y, z);        normal(0,-1,0);     textureCoord(1, 1);
                    position(x+1, y, z+1);      normal(0,-1,0);     textureCoord(1, 0);
                    position(x,   y, z+1);      normal(0,-1,0);     textureCoord(0, 0);

                    triangle(iVertex, iVertex+1, iVertex+2);
                    triangle(iVertex+2, iVertex+3, iVertex);

                    iVertex += 4;
                }


                    //y+1
                testingBlock = 0;
                if (y < 0 + CHUNK_SIZE.y - 1) testingBlock = m_pBlocks[x][y+1][z];

                if (testingBlock == 0)
                {
                    position(x,   y+1, z+1);        normal(0,1,0);  textureCoord(0, 1);
                    position(x+1, y+1, z+1);        normal(0,1,0);  textureCoord(1, 1);
                    position(x+1, y+1, z);          normal(0,1,0);  textureCoord(1, 0);
                    position(x,   y+1, z);          normal(0,1,0);  textureCoord(0, 0);

                    triangle(iVertex, iVertex+1, iVertex+2);
                    triangle(iVertex+2, iVertex+3, iVertex);

                    iVertex += 4;
                }

                    //z-1
                testingBlock = 0;
                if (z > 0) testingBlock = m_pBlocks[x][y][z-1];

                if (testingBlock == 0)
                {
                    position(x,   y+1, z);      normal(0,0,-1);     textureCoord(0, 1);
                    position(x+1, y+1, z);      normal(0,0,-1);     textureCoord(1, 1);
                    position(x+1, y,   z);      normal(0,0,-1);     textureCoord(1, 0);
                    position(x,   y,   z);      normal(0,0,-1);     textureCoord(0, 0);

                    triangle(iVertex, iVertex+1, iVertex+2);
                    triangle(iVertex+2, iVertex+3, iVertex);

                    iVertex += 4;
                }


                    //z+1
                testingBlock = 0;
                if (z < 0 + CHUNK_SIZE.z - 1) testingBlock = m_pBlocks[x][y][z+1];

                if (testingBlock == 0)
                {
                    position(x,   y,   z+1);    normal(0,0,1);      textureCoord(0, 1);
                    position(x+1, y,   z+1);    normal(0,0,1);      textureCoord(1, 1);
                    position(x+1, y+1, z+1);    normal(0,0,1);      textureCoord(1, 0);
                    position(x,   y+1, z+1);    normal(0,0,1);      textureCoord(0, 0);

                    triangle(iVertex, iVertex+1, iVertex+2);
                    triangle(iVertex+2, iVertex+3, iVertex);

                    iVertex += 4;
                }
            }
        }
    }

    end();
}
4

2 回答 2

0

我正在使用子弹物理库。它有一个看起来相当不错的碰撞检测系统。

有一个包装器,因为它最初是为与 Ogre 一起使用而编写的,所以它很好地与 Ogre 集成。

于 2013-02-20T20:54:45.583 回答
0

在使用块的情况下(我想它类似于在 Minecraft 中的操作方式)你很可能有一个 3d 数组来存储你的块数据。如何使用该 3d 数组中的数据来确定是否有碰撞一个块与否?这将相当容易和快速。

于 2013-03-16T02:48:22.493 回答