質問

I am experimenting with creating a voxel based game and am rendering millions of cubes.

In order to speed up the rendering, I am grouping up the cubes into chunks that group 32x32x32 cubes into one mesh. This is to reduce the number of render calls to the GPU and increase the framerate.

I am using a ManualObject to build up blocks and it works perfectly. However, the problem now is that since the individual blocks are not entities that are tied to individual scenenodes, I cannot find a way to do collision detection.

Does ogre have a way to separately work with submeshes of a 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();
}
役に立ちましたか?

解決

I'm using the bullet physics library. It has what looks like a pretty good collision detection system.

There's a wrapper and since it was originally written for use with Ogre it integrates nicely with Ogre.

他のヒント

In the case of using blocks (i guess it is similar to how it is done in Minecraft) you most likely have a 3d array where your block data is stored in. What about using the data from that 3d array to determine if something collided with a block or not? It would be fairly easy and fast.

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top