【问题标题】:Ray Intersection doesn't work correctly光线相交无法正常工作
【发布时间】:2012-12-15 14:53:13
【问题描述】:

好吧,所以我一直在做一个类似 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;
    }

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

【问题讨论】:

    标签: c# xna


    【解决方案1】:

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

    像这样:

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

    【讨论】:

      猜你喜欢
      • 2014-01-27
      • 1970-01-01
      • 1970-01-01
      • 2013-04-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多