Minecraft克隆的最佳盒子选择方法 [英] Best box selection method for a Minecraft clone
问题描述
作为我的第一个OpenGL项目,我正在制作Minecraft副本,并停留在选择框部分.进行可靠的盒子选择的最佳方法是什么?
I'm making a Minecraft clone as my very first OpenGL project and am stuck at the box selection part. What would be the best method to make a reliable box selection?
我一直在研究一些AABB算法,但是没有一个能很好地解释它们的确切功能(尤其是经过超级调整的算法),并且我不想使用我不了解的东西.
I've been going through some AABB algorithms, but none of them explain well enough what they exactly do (especially the super tweaked ones) and I don't want to use stuff I don't understand.
由于世界是由多维数据集组成的,所以我使用八叉树消除了射线投射计算中的某些应变,基本上我唯一需要的就是此函数:
Since the world is composed of cubes I used octrees to remove some strain on ray cast calculations, basically the only thing I need is this function:
float cube_intersect(Vector ray, Vector origin, Vector min, Vector max)
{
//???
}
射线和原点很容易获得
Vector ray, origin, point_far;
double mx, my, mz;
gluUnProject(viewport[2]/2, viewport[3]/2, 1.0, (double*)modelview, (double*)projection, viewport, &mx, &my, &mz);
point_far = Vector(mx, my, mz);
gluUnProject(viewport[2]/2, viewport[3]/2, 0.0, (double*)modelview, (double*)projection, viewport, &mx, &my, &mz);
origin = Vector(mx, my, mz);
ray = point_far-origin;
min和max是立方体的相对角.
min and max are the opposite corners of a cube.
考虑到即使使用八叉树,我也必须检查的多维数据集数量,我什至不确定这是否是正确的方法.
I'm not even sure this is the right way to do this, considering the number of cubes I'd have to check, even with octrees.
我也尝试过gluProject
,它可以工作,但是非常不可靠,并且无法为我提供选定的立方体面.
I've also tried gluProject
, it works, but is very unreliable and doesn't give me the selected face of the cube.
编辑
这就是我要做的:用射线计算空间中的位置:
So this is what I've done: calculate a position in space with the ray:
float t = 0;
for(int i=0; i<10; i++)
{
Vector p = ray*t+origin;
while(visible octree)
{
if(p inside octree)
{
// then call recursive function until a cube is found
break;
}
octree = octree->next;
}
if(found a cube)
{
break;
}
t += .5;
}
实际上速度惊人,并且在找到第一个多维数据集后便停止了运行.
It's actually surprisingly fast and stops after the first found cube.
如您所见,射线必须先经过多个八叉树才能找到一个立方体(实际上是在空间中的位置)-屏幕中间有一个十字准线.增量步长越低,选择越精确,但速度也越慢.
As you can see the ray has to go trough multiple octrees before it finds a cube (actually a position in space) - there is a crosshair in the middle of the screen. The lower the increment step the more precise the selection, but also slower.
推荐答案
使用框作为原语在内存需求和处理能力方面过高. 多维数据集非常适合渲染,甚至在那里,您还可以找到更高级的算法,从而为您提供更好的最终图像(行进多维数据集).从这种意义上来说,Minecraft的图形非常原始,因为体素渲染已经存在了很长时间,并且已经取得了重大进展.
Working with boxes as primitives is overkill in memory requirements and processing power. Cubes are fine for rendering and even there you can find more advanced algorithms that give you a better final image (Marching cubes). Minecraft's graphics are very primitive in this sense as voxel rendering has been around for a long time and significant progress has been made.
基本上,您应该利用所有盒子均等间距且大小相同的事实.这些称为体素. 与您所拥有的相比,网格中的光线投射是微不足道的-宽相八叉树和窄相AABB测试.我建议您对体素和体素集碰撞检测/光线投射进行一些研究,因为您会发现这两种算法都更易于实现且运行速度更快.
Basically you should exploit the fact that all your boxes are equally spaced and of the same size. These are called voxels. Raycasting in a grid is trivial in comparison to what you have - a broad-phase oct-tree and a narrow phase AABB test. I suggest you research a bit on voxels and voxel set collision detection/raycasting as you will find both algorithms that are easier to implement and that would run faster.
这篇关于Minecraft克隆的最佳盒子选择方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!