在分组网格上进行Ogre碰撞检测?

时间:2013-02-04 21:31:32

标签: c++ ogre ogre3d voxel

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

为了加快渲染速度,我将多维数据集分组为将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();
}

2 个答案:

答案 0 :(得分:0)

我正在使用子弹物理库。它看起来像一个非常好的碰撞检测系统。

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

答案 1 :(得分:0)

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