Z 排序几何

发布于 2024-09-19 13:26:56 字数 2808 浏览 8 评论 0原文

我正在制作一款完全由立方体组成的游戏。我注意到,当我向前走时,它跑得快如闪电,但如果我向相反方向旋转玩家,它的速度就会慢得要命。所以我所做的是根据角度排序,但我仍然得到一些有点慢的角度。我是这样做的:

我基本上在某些角度上进行反向迭代,但如何才能使其一致,以便每个角度都快如闪电,一些直角如 88-92 或 178-182 真的很慢(透支) )。

谢谢

SetPlayerPosition();


PlayerPosition.x -= 70;

PlayerPosition.y -= 20;

PlayerPosition.z -= 70;


collids.clear();
CBox* tmp;
float offset = -10;
if( Wrap(Camera.roty + offset) > 180 && Wrap(Camera.roty + offset) < 270)
{
    for(int i = 140; i > 0; --i)
    {
        for(int j = 0; j < 40; ++j)
        {
            for(int k = 0; k < 140; ++k)
            {

                tmp = GetCube(PlayerPosition.x + i, PlayerPosition.y + j, PlayerPosition.z + k);



                if(tmp != 0)
                {
                    if(frustum.sphereInFrustum(tmp->center,25) != NULL)
                    {
                        collids.push_back(tmp);
                    }
                }

            }
        }
    }
}
else if(Wrap(Camera.roty + offset) > 0 && Wrap(Camera.roty + offset) < 90 )
{
    for(int i = 0; i < 140; ++i)
    {
        for(int j = 0; j < 40; ++j)
        {
            for(int k = 140; k > 0; --k)
            {

                tmp = GetCube(PlayerPosition.x + i, PlayerPosition.y + j, PlayerPosition.z + k);



                if(tmp != 0)
                {
                    if(frustum.sphereInFrustum(tmp->center,25) != NULL)
                    {
                        collids.push_back(tmp);
                    }
                }

            }
        }
    }
}

else if(Wrap(Camera.roty + offset) > 90 && Wrap(Camera.roty + offset) < 180 )
{
    for(int i = 0; i < 140; ++i)
    {
        for(int j = 0; j < 40; ++j)
        {
            for(int k = 0; k < 140; ++k)
            {

                tmp = GetCube(PlayerPosition.x + i, PlayerPosition.y + j, PlayerPosition.z + k);



                if(tmp != 0)
                {
                    if(frustum.sphereInFrustum(tmp->center,25) != NULL)
                    {
                        collids.push_back(tmp);
                    }
                }

            }
        }
    }
}
else if (Wrap(Camera.roty + offset) > 270 && Wrap(Camera.roty + offset) < 360)
{
    for(int i = 140; i > 0; --i)
    {
        for(int j = 0; j < 40; ++j)
        {
            for(int k = 140; k > 0; --k)
            {

                tmp = GetCube(PlayerPosition.x + i, PlayerPosition.y + j, PlayerPosition.z + k);



                if(tmp != 0)
                {
                    if(frustum.sphereInFrustum(tmp->center,25) != NULL)
                    {
                        collids.push_back(tmp);
                    }
                }

            }
        }
    }
}


}

I'm making a game that is entirely made of cubes. I notice that when I walk forward it runs lightening fast, but if I rotate the player in the opposite direction, its cripplingly slow. So what I did is ordered based on angle, but I still get some angles that are a bit slow. Here is how I did it:

I basically reverse iterate in certain angles, but how could I make it consistent, so that every angle is lightening fast, some of the straight angles like 88-92, or 178-182 are really slow (overdraw).

Thanks

SetPlayerPosition();


PlayerPosition.x -= 70;

PlayerPosition.y -= 20;

PlayerPosition.z -= 70;


collids.clear();
CBox* tmp;
float offset = -10;
if( Wrap(Camera.roty + offset) > 180 && Wrap(Camera.roty + offset) < 270)
{
    for(int i = 140; i > 0; --i)
    {
        for(int j = 0; j < 40; ++j)
        {
            for(int k = 0; k < 140; ++k)
            {

                tmp = GetCube(PlayerPosition.x + i, PlayerPosition.y + j, PlayerPosition.z + k);



                if(tmp != 0)
                {
                    if(frustum.sphereInFrustum(tmp->center,25) != NULL)
                    {
                        collids.push_back(tmp);
                    }
                }

            }
        }
    }
}
else if(Wrap(Camera.roty + offset) > 0 && Wrap(Camera.roty + offset) < 90 )
{
    for(int i = 0; i < 140; ++i)
    {
        for(int j = 0; j < 40; ++j)
        {
            for(int k = 140; k > 0; --k)
            {

                tmp = GetCube(PlayerPosition.x + i, PlayerPosition.y + j, PlayerPosition.z + k);



                if(tmp != 0)
                {
                    if(frustum.sphereInFrustum(tmp->center,25) != NULL)
                    {
                        collids.push_back(tmp);
                    }
                }

            }
        }
    }
}

else if(Wrap(Camera.roty + offset) > 90 && Wrap(Camera.roty + offset) < 180 )
{
    for(int i = 0; i < 140; ++i)
    {
        for(int j = 0; j < 40; ++j)
        {
            for(int k = 0; k < 140; ++k)
            {

                tmp = GetCube(PlayerPosition.x + i, PlayerPosition.y + j, PlayerPosition.z + k);



                if(tmp != 0)
                {
                    if(frustum.sphereInFrustum(tmp->center,25) != NULL)
                    {
                        collids.push_back(tmp);
                    }
                }

            }
        }
    }
}
else if (Wrap(Camera.roty + offset) > 270 && Wrap(Camera.roty + offset) < 360)
{
    for(int i = 140; i > 0; --i)
    {
        for(int j = 0; j < 40; ++j)
        {
            for(int k = 140; k > 0; --k)
            {

                tmp = GetCube(PlayerPosition.x + i, PlayerPosition.y + j, PlayerPosition.z + k);



                if(tmp != 0)
                {
                    if(frustum.sphereInFrustum(tmp->center,25) != NULL)
                    {
                        collids.push_back(tmp);
                    }
                }

            }
        }
    }
}


}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

乱了心跳 2024-09-26 13:26:56

哎呀。

在不知道 GetCube 正在做什么的情况下,我无法开始猜测它如何影响您的性能,但很清楚为什么事情会显着减慢。你做了很多不必要的工作。

您只需采取不同的方法即可消除大部分循环:忽略角度,并进行更简化的测试。对于 ijk 的任何值,如果它标记了“玩家”后面的立方体,则不执行任何操作。仅此一点就可以平衡您的帧速率并简化您的循环。 (您不需要四组不同的嵌套循环!)

更好的是,使用空间图 - 类似于八叉树或kd树 - 来存储多维数据集(或其他对象)的实例,并在树的节点上进行交叉测试。这样,您就可以一次性消除大量永远不会位于截锥体内的对象,并显着提高性能。

Yikes.

Without knowing what GetCube is doing, I can't begin to guess how it's affecting your performance, but it's pretty clear why things are slowing down so significantly. You're doing a lot of unnecessary work.

You can eliminate most of your loops simply by taking a different approach: Ignore the angle, and do more simplified tests. For any value of i, j, and k, do nothing if it marks a cube behind the "player." That alone will even out your framerate and simplify your loops. (You don't need four different sets of nested loops!)

Even better, use a spatial graph - something like an octree or kd-tree - to store instances of your cubes (or other objects), and do your intersection tests on nodes of the tree. That way, you can eliminate huge swaths of objects all at once that will never be inside the frustum, and improve performance significantly.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文