0

好吧,所以我一直在研究类似 Minecraft 的建造者,其想法是能够快速建造巨大的结构,而无需采矿等。这是为了学习经验。

所以现在我被困在一个恼人的地方......

如果我从一侧摧毁一个街区。它工作正常。

在此处输入图像描述 在此处输入图像描述

但是一旦我把相机移到另一边,一切都错了。

在此处输入图像描述 在此处输入图像描述

所以基本上我被困在这一点上,不知道如何解决它。

我计算射线:

Ray getRay(MouseState mouseState)
        {
            Viewport vp = mainController.engine.graphics.GraphicsDevice.Viewport;

            Vector3 nearPoint = new Vector3(mouseState.X, mouseState.Y, 0);
            Vector3 farPoint = new Vector3(mouseState.X, mouseState.Y, 1);



            nearPoint = vp.Unproject(nearPoint, camera.getProjectionMatrix(), camera.getViewMatrix(), Matrix.Identity);
            farPoint = vp.Unproject(farPoint, camera.getProjectionMatrix(), camera.getViewMatrix(), Matrix.Identity);

            Vector3 direction = Vector3.Normalize(farPoint - nearPoint);

            return new Ray(nearPoint, direction);
        }

然后用所有模型的边界框粗暴地检查它:

for (int y = world.settings.regionHeight - 1; y >= 0; y--)
    {
          gotblock = false;
          for (int x = 0; x < world.settings.regionWidth; x++)
              for (int z = 0; z < world.settings.regionLenght; z++)
              {
                  f = ray.Intersects(block[x, y, z].boundingBox);
                  if (f != null)
                  {
                       if (f < lowestDistance && world.block[(int)position.X * world.settings.regionWidth + x, y, (int)position.Y * world.settings.regionLenght + z] != BlockTypes.none)
                           {
                               result = new intVector3((int)position.X * world.settings.regionWidth + x, y, (int)position.Y * world.settings.regionLenght + z);
                                    gotblock = true;
                           }
                  }
              }
         if (gotblock)
             break;
    }

如果您需要更多信息,请告诉。提前致谢。

4

1 回答 1

0

问题是,在检查最低距离时,距离可以用射线中的负数来计算。因此,我只需要比较距离的绝对值,而不仅仅是正数。

像这样:

for (int y = world.settings.regionHeight - 1; y >= 0; y--)
                {
                    gotblock = false;
                    for (int x = 0; x < world.settings.regionWidth; x++)
                        for (int z = 0; z < world.settings.regionLenght; z++)
                        {
                            f = ray.Intersects(block[x, y, z].boundingBox);

                            if (f != null)
                            {
                                f = Math.Abs((float)f);
                                if (f < lowestDistance && world.block[(int)position.X * world.settings.regionWidth + x, y, (int)position.Y * world.settings.regionLenght + z] != BlockTypes.none)
                                {
                                    lowestDistance = (float)f;
                                    result = new intVector3((int)position.X * world.settings.regionWidth + x, y, (int)position.Y * world.settings.regionLenght + z);
                                    gotblock = true;
                                }
                            }
                        }
                    if (gotblock)
                        break;
                }
于 2012-12-15T06:53:13.950 回答