0

さて、私はマインクラフトのようなビルダーに取り組んできました。アイデアは、マイニングなどをせずに巨大な構造物をすばやく構築できるようにすることです.それは学習体験のためです.

だから今、私は厄介な点で立ち往生しています...

片側からブロックを破壊した場合。正しく動作します。

ここに画像の説明を入力 ここに画像の説明を入力

しかし、カメラを反対側に移動すると、すべてがうまくいきません。

ここに画像の説明を入力 ここに画像の説明を入力

したがって、基本的に私はこの時点で立ち往生しており、修正方法がわかりません。

私は光線を計算します:

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 に答える