Ray Intersection无法正常工作

时间:2012-12-14 19:02:10

标签: c# xna

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

所以现在我陷入了烦人的地步......

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

enter image description here enter image description here

但是一旦我将相机移到另一边,就会出错。

enter image description here enter image description here

所以基本上我已经陷入困境,并且不知道如何解决它。

我计算射线:

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;
    }

如果您需要更多信息,请告诉我们。在此先感谢。

1 个答案:

答案 0 :(得分: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;
                }