Z 排序几何
我正在制作一款完全由立方体组成的游戏。我注意到,当我向前走时,它跑得快如闪电,但如果我向相反方向旋转玩家,它的速度就会慢得要命。所以我所做的是根据角度排序,但我仍然得到一些有点慢的角度。我是这样做的:
我基本上在某些角度上进行反向迭代,但如何才能使其一致,以便每个角度都快如闪电,一些直角如 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
哎呀。
在不知道
GetCube
正在做什么的情况下,我无法开始猜测它如何影响您的性能,但很清楚为什么事情会显着减慢。你做了很多不必要的工作。您只需采取不同的方法即可消除大部分循环:忽略角度,并进行更简化的测试。对于
i
、j
和k
的任何值,如果它标记了“玩家”后面的立方体,则不执行任何操作。仅此一点就可以平衡您的帧速率并简化您的循环。 (您不需要四组不同的嵌套循环!)更好的是,使用空间图 - 类似于八叉树或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
, andk
, 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.