好吧,所以我一直在研究类似 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;
}
如果您需要更多信息,请告诉。提前致谢。