鉴于我知道等距菱形地图坐标,如何确定单击了哪个图块?
我的瓦片引擎即将推出。它可以绘制正方形、六边形和等距交错视点。我遇到困难的是等距旋转(或菱形)视点。下面是一张 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我不确定我能否理解您的问题的详细信息,但如果您只是想根据
column
求解x
和y
的公式code> 和row
,那么获得这些表达式的最简单方法是首先添加
x
和y
的表达式,然后求解列
,然后减去它们并求解行
。I am not sure I can follow the details of your problem, but if you are just looking to solve your formulas for
x
andy
in terms ofcolumn
androw
, thenThe easiest way to get these expression is to first add the expressions for
x
andy
and solve forcolumn
, then subtract them and solve forrow
.