鉴于我知道等距菱形地图坐标,如何确定单击了哪个图块?

发布于 2024-10-11 03:50:01 字数 1225 浏览 3 评论 0原文

我的瓦片引擎即将推出。它可以绘制正方形、六边形和等距交错视点。我遇到困难的是等距旋转(或菱形)视点。下面是一张 10x10 菱形地图的图片以及用于绘制它的(简化)代码。瓷砖的尺寸为 128x64。

http://garrypettet.com/images/forum_images/5% 20col%20x%205%20rows.png

for row = 0 to rowLimit

  for column = 0 to columnLimit

    x = column * (TileWidth/2) + (row * (TileWidth/2)) + Origin.X
    y = (column * (TileHeight/2)) - (row * (TileHeight/2)) + Origin.Y

    // Draw the tile's image
    buffer.Graphics.DrawPicture(Tiles(column, row).Image, x, y)

  next column

next row

// Draw the buffer to the canvas
g.DrawPicture(buffer, 0, 0)

我知道这将绘制整个 Tiles() 的内容,而不仅仅是屏幕上可见的内容,但我试图首先了解基础知识。

我想不出一种将地图上的 x,y 坐标转换为平铺列、行坐标的简单方法。我尝试逆向:

x = column * (TileWidth/2) + (row * (TileWidth/2)) + Origin.X
y = (column * (TileHeight/2)) - (row * (TileHeight/2)) + Origin.Y

计算出给定 x 和 y 的列和行,并想出了这个:

column = ((x/2) - (Origin.X/2) + y + Origin.Y) / TileHeight
row = ((x/2) - (Origin.X/2) - y - Origin.Y) / TileHeight

但这似乎不起作用。有人能想出更好的方法来做到这一点吗?有没有更好的方法将矩形网格转换为菱形并再次转换回来(鉴于我对矩阵知之甚少......)。

谢谢,

My tile engine is coming along. It can draw square, hexagonal and isometric staggered viewpoints. Where I'm struggling is with the isometric rotated (or diamond) viewpoint. Below is a picture of a 10x10 diamond map and the (simplified) code used to draw it. The tiles are 128x64.

http://garrypettet.com/images/forum_images/5%20col%20x%205%20rows.png

for row = 0 to rowLimit

  for column = 0 to columnLimit

    x = column * (TileWidth/2) + (row * (TileWidth/2)) + Origin.X
    y = (column * (TileHeight/2)) - (row * (TileHeight/2)) + Origin.Y

    // Draw the tile's image
    buffer.Graphics.DrawPicture(Tiles(column, row).Image, x, y)

  next column

next row

// Draw the buffer to the canvas
g.DrawPicture(buffer, 0, 0)

I know that this will draw the contents of the whole of Tiles() and not just those visible on screen but I'm trying to get the basics first.

What I can't figure out is an easy way to convert x,y coordinates on the map to tile column,row coordinates. I tried to reverse:

x = column * (TileWidth/2) + (row * (TileWidth/2)) + Origin.X
y = (column * (TileHeight/2)) - (row * (TileHeight/2)) + Origin.Y

To work out column and row given x and y and came up with this:

column = ((x/2) - (Origin.X/2) + y + Origin.Y) / TileHeight
row = ((x/2) - (Origin.X/2) - y - Origin.Y) / TileHeight

But that doesn't seem to work. Can anyone think of a better way to do this? Is there a better way to transform a grid of rectangles into a diamond and back again (given that I know very little about matrices....).

Thanks,

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

毁虫ゝ 2024-10-18 03:50:01

我不确定我能否理解您的问题的详细信息,但如果您只是想根据 column 求解 xy 的公式code> 和 row,那么

column=(x + y - (Origin.X + Origin.Y))/TileWidth
row = (x - y - (Origin.X - Origin.Y))/TileHeight

获得这些表达式的最简单方法是首先添加 xy 的表达式,然后求解 ,然后减去它们并求解

I am not sure I can follow the details of your problem, but if you are just looking to solve your formulas for x and y in terms of column and row, then

column=(x + y - (Origin.X + Origin.Y))/TileWidth
row = (x - y - (Origin.X - Origin.Y))/TileHeight

The easiest way to get these expression is to first add the expressions for x and y and solve for column, then subtract them and solve for row.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文