c# - 等距,仅在屏幕图 block 上绘制

标签 c# xna drawing isometric tiles-game

我只需在屏幕上绘制图 block 即可提高性能。什么是可行的方法来做到这一点?

我尝试了这个,这让我很接近,但最终遇到了一个我似乎无法解决的错误。

//world position to start drawing from
        int sPosX = (int)(cam.Position.X - (device.Viewport.Width / 2));
        int sPosY = (int)(cam.Position.Y - (device.Viewport.Height / 2));

        //tile position to start drawing from
        int sTileX = (int)((sPosX + (2*sPosY) - (Map.TileWidth/2)) / Map.TileWidth -1);
        int sTileY = (int)((sPosX - (2 * sPosY) - (Map.TileHeight / 2)) / -Map.TileWidth - 1);

        //amount of rows/collumns to draw
        int rowCount = (int)(device.Viewport.Height / (Map.TileHeight / 2) + 2);
        int colCount = (int)(device.Viewport.Width / Map.TileWidth + 2);

        //current tile to draw
        int tileX;
        int tileY;


        for (int row = 0; row < rowCount; row++)
        {
            for (int col = 0; col < colCount; col++)
            {
                tileX = sTileX + col + (int)((row / 2));
                tileY = sTileY - col + (int)((row / 2));
                if (tileX >= 0 && tileX < tileMap.GetLength(0) && tileY >= 0 && tileY < tileMap.GetLength(1))
                {
                    batch.Draw(grid, new Rectangle((tileX * (TileWidth / 2)) - (tileY * (TileWidth / 2)), (tileX * (TileHeight / 2) + (tileY * (TileHeight / 2))), TileWidth, TileHeight), Color.White);
                    batch.DrawString(tiny, tileX + "," + tileY, new Vector2(tileX * (TileWidth / 2) - (tileY * (TileWidth / 2)) + 24, tileX * (TileHeight / 2) + (tileY * (TileHeight / 2)) + 12), Color.White);
                }
            }
        }

在这里,我尝试逐行绘制我的瓷砖 map 。首先,我找出要绘制的第一个图 block 是什么(左上角),然后找出需要绘制多少列和多少行。

起初我只使用 float 并在最后一刻强制转换为结束,同时将 float 逐步转换为 int 我能够在屏幕上获得完整的 X=0 和完整的 Y=0 行,但其余的行仍然显示为这张图片。

Isometric display problem

移动相机时,显示/渲染的图 block 在不均匀和均匀之间切换,因此,如果我向下移动少量,突然图片中显示的所有图 block 都会消失并显示空白。再进一步移动,它就会反转,如果我继续移动,它就会在这两种状态之间闪烁。我认为它与 (row/2) 有关,但我似乎无法修复它。

我计算要绘制的图 block 的方式似乎不错。当我缩小时我得到这个(缩放还没有改变公式)。在下图中,您可以看到第二种状态以及正在绘制的其他图 block 。

enter image description here

最佳答案

tileX = sTileX + col + (int)((row / 2) + (row%2));

模数修复了这个问题,因为除了第一行之外,每一第二行都应该增加一。

尽管如此,我认为这是有值(value)的信息,因为我花了几个小时寻找如何正确执行此操作,并最终自己完成了。我认为这是绘制所有内容的最有效方法,因为它允许将菱形样式等轴测图渲染为锯齿形正方形。

关于c# - 等距,仅在屏幕图 block 上绘制,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16883337/

相关文章:

c# - ASP.NET Core 2 中 HttpContext.Current.Items.Contains(DataContextKey) 的替代方案

c# - A*(A 星)算法帮助

ios - OpenGL ES 2D 绘图性能评估

Android绘图方法运行缓慢

c# - 如何改进缺少右大括号的 Sprache 解析器错误消息?

c# - 使用 XmlDocument 从 XML 中提取数据

c# - XNA 中的 Unicode 字符串显示

C# XNA 鼠标位置

c# - XNA - 以正确的顺序绘制四边形(和基元)

c# - ThreadAbortException 与 C# 中的优雅事件句柄退出