六角形瓷砖并找到它们相邻的节点邻居 [英] Hexagonal Tiles and finding their adjacent node neighbourghs

查看:79
本文介绍了六角形瓷砖并找到它们相邻的节点邻居的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个使用六角形瓷砖地图的简单2D棋盘游戏,我已经阅读了几篇文章(包括gamedev one',每次有关于六边形瓷砖的问题时都会链接)在屏幕上绘制六角形以及如何管理运动(尽管我之前已经做过很多)。我的主要问题是找到基于给定半径的相邻瓷砖。



这就是我的地图系统的工作方式:

(0,0 )(0,1)(0,2)(0,3)(0,4)

(1,0)(1,1)(1,2)(1,3)( 1,4)

(2,0)(2,1)(2,2)(2,3)(2,4)

(3,0) (3,1)(3,2)(3,3)(3,4)等...

我正在努力的事实是我不能只是'选择' '使用for的相邻tile(x-range; x + range; x ++);为(Y-范围; Y +范围; Y ++);因为它选择了不需要的瓷砖(在我给出的例子中,选择(1,1)瓷砖并给出1的范围也会给我(3,0)瓷砖(我实际需要的是(0,1)( 0,2)(1,0)(1,2)(2,1)(2,2)),有点与瓷砖相邻(因为数组的结构方式),但它并不是真的我想要选择的是什么。我可以强行说出来,但这不会很美,也许不会涵盖选择半径的东西的各个方面。



有人能指出我在正确的方向吗?



我找到了算法,但它有一个重要的错误

< pre lang =c ++> void select( int x, int y)
{printf( X:%d,Y:%d \\ n \\ n,x,y);}

void selectRange( int x, int y, int 范围)
{
int minX = x - range,maxX = x + range;

for int i = minX; i< = maxX; + + i)
if (i!= x)
select(i,y);

for int yOff = 1 ; yOff< = range; ++ yOff)
{
if (y + yOff% 2 == 1 ) - maxX;
else ++ minX;

for int i = minX; i< = maxX; ++ i)
{
select(i,y + yOff);选择(i,y-yOff);
}
}
}


int main()
{
selectRange( 2 2 2 < /跨度>);
}



结果:

X:0,Y:2

X:1,Y :2

X:3,Y:2

X:4,Y:2

X:1,Y:3
X:1,Y:1

X:2,Y:3

X:2,Y:1

X:3,Y:3

X:3,Y:1

X:4,Y:3

X:4,Y :1

X:2,Y:4

X:2,Y:0

X:3,Y:4
X:3,Y:0

X:4,Y:4

X:4,Y:0



结果必须是(0,1)(0,3)(1,4)但它是(3.4),(4,0),(4,4)

解决方案

你有问题不是因为任何特定的算法,而是因为你同时使用了错误的数据表示和几何。

你的数组只是很好地表示基于方形的瓷砖坐标,但是六边形瓷砖有点不同。



一种简单的方法是使用对称六角形瓷砖的真实瓷砖的几何中心表面。您可以在某些任意单位整数值中使用整数X,Y坐标。那么你可以使用笛卡尔距离。



这种方法可行,但它太几何而且很原始,对于编程来说仍然很尴尬。它最接近你目前的想法,但我认为错过了游戏的本质。 (此时,我意识到缺乏游戏描述是一个问题,但我将参考基于元胞自动机的游戏的一般概念;请参阅 http://en.wikipedia.org/wiki/Cellular_automaton [ ^ ]。)



这样的游戏,游戏场景结构的本质是代数拓扑。每次有6个固定方向的6个成员。这就是最重要的。因此,场景(或游戏板)的真实模型应该是图形,其中节点表示单元格,弧形表示最近单元格的路径。



有关一般概念,请参阅:

http://en.wikipedia。 org / wiki / Graph_%28mathematics%29 [ ^ ],

http://en.wikipedia.org/wiki/Graph_theory [ ^ ]。



图表可以在编程中表示为关系或作为集合,每个元素代表一个单元格,以及由一组引用表示的关系细胞。这种表示是统一的,易于处理。在这种方法中,真实几何被排除在数学之外,你只能将它用于游戏的屏幕演示。



-SA

I''m developing a simple 2D board game using hexagonal tile maps, I''ve read several articles (including the gamedev one''s, which are linked every time there''s a question on hexagonal tiles) on how to draw hexes on the screen and how to manage the movement (though much of it I had already done before). My main problem is finding the adjacent tiles based on a given radius.

This is how my map system works:
(0,0) (0,1) (0,2) (0,3) (0,4)
(1,0) (1,1) (1,2) (1,3) (1,4)
(2,0) (2,1) (2,2) (2,3) (2,4)
(3,0) (3,1) (3,2) (3,3) (3,4) etc...
What I''m struggling with is the fact that I cant just ''select'' the adjacent tiles by using for(x-range;x+range;x++); for(y-range;y+range;y++); because it selects unwanted tiles (in the example I gave, selecting the (1,1) tile and giving a range of 1 would also give me the (3,0) tile (the ones I actually need being (0,1)(0,2)(1,0)(1,2)(2,1)(2,2) ), which is kinda adjacent to the tile (because of the way the array is structured) but it''s not really what I want to select. I could just brute force it, but that wouldn''t be beautiful and would probably not cover every aspect of ''selecting radius thing''.

Can someone point me in the right direction here?

I''m found algorithm but it has an important error

void select(int x, int y) 
{ printf("X : %d, Y : %d\n",x,y); }

void selectRange(int x, int y, int range)
{
    int minX = x - range, maxX = x + range;

    for (int i = minX; i <= maxX; ++i)
        if (i != x) 
	select(i, y);

    for (int yOff = 1; yOff <= range; ++yOff)
    {
        if (y+yOff % 2 == 1) --maxX; 
        else  ++minX;

        for (int i=minX; i<=maxX; ++i)
        {
            select(i, y+yOff);  select(i, y-yOff);
        }
    }  
}


int main()
{
	selectRange(2,2,2);
}


result :
X : 0, Y : 2
X : 1, Y : 2
X : 3, Y : 2
X : 4, Y : 2
X : 1, Y : 3
X : 1, Y : 1
X : 2, Y : 3
X : 2, Y : 1
X : 3, Y : 3
X : 3, Y : 1
X : 4, Y : 3
X : 4, Y : 1
X : 2, Y : 4
X : 2, Y : 0
X : 3, Y : 4
X : 3, Y : 0
X : 4, Y : 4
X : 4, Y : 0

the results have to be (0,1)(0,3)(1,4) but it is (3.4),(4,0),(4,4)

解决方案

You have the problem not because any particular algorithm, but because you are using wrong data presentation and geometry at the same time.
Your array is only good to represent square-based tile coordinates, but hexagonal tile is a bit different.

One simple approach would be using geometrical centers of "real" tiles of symmetric hexagonal tile surface. You can use integer X, Y coordinates in some arbitrary-unit integer values. Then you can use Cartesian distances.

This approach would work, but it "too geometrical" and primitive, still awkward for programming. It is the closest to your current thinking, but I think missing the essence of the game. (At this point, I realize that the lack of game description is a problem, but I will refer to general idea of games based on cellular automaton; please see http://en.wikipedia.org/wiki/Cellular_automaton[^].)

It such game, the essence of the structure of the game scene is algebra-topological. Each time has 6 members in 6 fixed direction. This is all what matters. So, the real model of the scene (or a game board) should be a graph, where a node represent a cell, and an arc represent a path to a nearest cell.

For general ideas, please see:
http://en.wikipedia.org/wiki/Graph_%28mathematics%29[^],
http://en.wikipedia.org/wiki/Graph_theory[^].

A graph can be represented in programming as a relationship or as a collection, each element representing a cell, and relationship represented by a set of references between cells. Such representation is uniform and easy to process. In this approach, "real" geometry is excluded from the mathematics, you can use it only for a screen presentation of a game.

—SA


这篇关于六角形瓷砖并找到它们相邻的节点邻居的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆