【问题标题】:Drawing Isometric Tilemaps绘制等轴测图
【发布时间】:2010-12-23 01:55:01
【问题描述】:

我一直在使用 C# / XNA Game Studio 绘制等距地图,虽然我已经走了很远,但它看起来不太正确,我想知道是否有人可以提供帮助。

这是我绘制地图的代码:

public void Draw(SpriteBatch theBatch, int drawX, int drawY)
    {

        if ((drawY % 2 == 0))
            theBatch.Draw(tileTexture, new Rectangle((drawX * width), (drawY * length / 2), width, length), Color.White);
        else
            theBatch.Draw(tileTexture, new Rectangle(((drawX * width) + (width / 2)), (drawY * length / 2), width, length), Color.White);
    }

此方法中的代码就像在嵌套的 for 循环中一样,从左到右、从上到下绘制。当 y 值为奇数时,该行将移到合适位置,但它看起来有点偏离。

这是生成的 8x5 地图输出:

如您所见,它看起来不太正确,我不确定这是否与我的代码中的数学问题有关,或者是否与绘制所有内容的顺序有关。我我对 C# 非常陌生并且使用 sprite,因此非常感谢任何帮助。

因为它可能会有所帮助,所以这里是绘制地图的其他相关代码部分。

整个 Tile 类:

public class Tile
{
    // Dimension variables
    int height;
    int width;
    int length;

    String type;
    Texture2D tileTexture;
    Vector2 coordinates;

    /// 
    /// Tile Constructor
    /// 
    public Tile(ContentManager theContent, String theType, Vector2 theCoordinates)
    {
        width = 68;
        length = 46;

        type = theType;
        coordinates = theCoordinates;

        // Sets the right texture to the texture ref
        if (theType == "grass")
            tileTexture = theContent.Load<Texture2D>(@"Tiles\iso_grass");
    }

    /// 
    ///  Draws the tile at the given location
    /// 
    public void Draw(SpriteBatch theBatch, int drawX, int drawY)
    {

        if ((drawY % 2 == 0))
            theBatch.Draw(tileTexture, new Rectangle((drawX * width), (drawY * length / 2), width, length), Color.White);
        else
            theBatch.Draw(tileTexture, new Rectangle(((drawX * width) + (width / 2)), (drawY * length / 2), width, length), Color.White);
    }


}

TileRow 类,它包含一排瓷砖。

public class TileRow
{
        public List<Tile> Row = new List<Tile>();
        public int rowLength;

        public TileRow(int theLength, int yIndex, ContentManager theContent)
        {
            rowLength = theLength;
            Tile thisTile;

            // Here the tiles are created and added to the row
            for (int x = 0; x < rowLength; x++)
            {
                thisTile = new Tile(theContent, "grass", new Vector2(x, yIndex));
                Row.Add(thisTile);
            }
        }

        /// 
        /// Draw -- invokes the draw method of each tile in the row
        /// 
        public void DrawRow(SpriteBatch theBatch, int currentY)
        {
            for (int x = 0; x < rowLength; x++)
            {
                Row[x].Draw(theBatch, currentY, x);
            }
        }
    }
}

以及包含所有行的 MapStruct 类

public class MapStruct
{

    public List<TileRow> allRows = new List<TileRow>();
    int depth;

    // Constructor
    public MapStruct(ContentManager theContent, int theDepth, int rowLength)
    {
        depth = theDepth;
        TileRow thisRow;

        // Here we make a row of tiles for each level of depth
        for (int y = 0; y < depth; y++)
        {
            thisRow = new TileRow(rowLength, depth, theContent);
            allRows.Add(thisRow);
        }
    }

    ///
    /// Draw - this method invokes the draw method in each tile row, which then draws each tile
    ///
    public void DrawMap(SpriteBatch theBatch)
    {
        for (int y = 0; y < depth; y++)
        {
            allRows[y].DrawRow(theBatch, y);
        }
    }
}

对于如何解决此问题的任何帮助,以及有关如何改进我的代码的建议,我们将不胜感激!

【问题讨论】:

    标签: c# xna 2d isometric


    【解决方案1】:

    看起来您的循环对每一行的 Y 增加了一点点。 我在你的 Tile 函数中找到了这个变量。

    length = 46;
    

    我没有检查,但我相信“长度”是瓷砖的高度?如果是这样,请尝试稍微调整一下。也许,您忘记减去瓷砖的高度。因此,如果图块的边长为 6 个像素,那么偏移量 pr 的长度。行只有 40 行。

    还记得从上到下绘制,因为最近的摄像机必须最后绘制,以产生深度错觉。

    【讨论】:

    • 是的,“长度”是文件的高度。调整变量的值并没有帮助,但是从奇数行的 Y 中减去 4 会有所帮助,但它仍然看起来不正确。 imgur.com/RAE9W(调整后)
    • 我现在也看到了最后一张照片。我想知道它现在是否欺骗了您的瓷砖的“z-index”?关于图块大小,在旧的“位图”时代,我们通过使用“二进制”大小获得了最佳性能。那是 32、64 或其他除以 8 位宽度的东西。如果您使用绘画程序并将其中一些并排放置,您应该会看到一个很好的图案形成。但是你最新帖子的问题不是因为大小。我相信这是算法使用 Z-index 创建它们的方式(就像在 Flash 中一样)。不确定,因为我对 XNA API 了解不够
    • 您能向我们解释一下您发布的算法吗?你是按行还是对角线阅读你的瓷砖地图?
    【解决方案2】:

    我相信 BerggreenDK 是对的,但您的评论让人觉得您误解了他的回答。您的瓷砖需要以仅包括绿色表面区域“屏幕高度”的 Y 偏移量绘制。因此,绘制完整的图块大小,但偏移长度为 5 的行(其中 10 是估计偏移量,5 是一半,因为每行应该只偏移一半距离)。

    theBatch.Draw(tileTexture, new Rectangle((drawX * width), (drawY * (length) / 2), width, length - 5), Color.White);
    

    ...如果我得到正确的参数。

    另外,行需要从后到前绘制,但它们似乎是从左到右绘制的?我不知道 XNA,所以无法显示代码来解决这个问题......当我仔细观察时,“更远”的一行中的一些图块被“更近”的图块过度绘制。我不知道 XNA 也不知道绘图顺序是如何确定的,所以无法提供解决方案...

    【讨论】:

    • 我已经将循环更改为从下到上绘制,并根据您的建议更改了平铺绘制线。 (输出 = imgur.com/GKq4N)。你和 BerggeenDK 所说的一切都是有道理的,所以可能是图像本身的问题?他们需要采用特定的纵横比吗?
    • 我想我弄错了参数。因为我从来没有做过任何 XNA 编程,所以我有点犹豫。我会尝试: theBatch.Draw(tileTexture, new Rectangle((drawX * width), (drawY * (length + 10) / 2), width, length), Color.White);和奇数行类似...
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-06-23
    • 2012-03-25
    • 1970-01-01
    • 1970-01-01
    • 2016-10-09
    相关资源
    最近更新 更多