[java] Trouble with Hex Map

Started by
1 comment, last by DarkMortar 17 years, 2 months ago
Tryin to draw my temp hex tiles, but i am having problems getting them to 'stick' horizontially, ive read tuts. but my problem is that i dont know how to find the length of each side on the hexagon, i need that for the other problems to work, but ive gone this far with just dividing the rectangular hexagon image. code: // even row if (i%2==0) { hexGraphic[j].draw(g, (int)xPos+i*HEX_SIZE, (int)yPos+j*HEX_SIZE); } // odd row else { hexGraphic[j].draw(g, (int)xPos+i*HEX_SIZE, (int)yPos+j*HEX_SIZE+HEX_SIZE/2); } picture: http://img295.imageshack.us/img295/8981/hexhelpni9.png
DarkTech Software.
Advertisement
Don't think of them as hexes. Think of them as 6 equalateral triangles joined.

So if you draw a line between the center points of 2 hexes, you are actually bisecting a triangle in each. And since all your hexes/triangles are the same size, you can figure out the length of one to find the distance between the 2. Just us basic geometry.

d = b / 2
e2 = b2 - d2

So if b is 6 then
e = sqrt(62 - 32)
e = sqrt(27)
e = 5.2

Hope that helps.

ps. I tried to draw a picture, but the forum won't let me draw preformatted text anymore. Check out this link instead and the pics there should help. The second picture shows the 6 triangles and the text helps explain a bit of what I am talking about.

http://mathforum.org/pow/solutio90.html
"None of us learn in a vacuum; we all stand on the shoulders of giants such as Wirth and Knuth and thousands of others. Lend your shoulders to building the future!" - Michael Abrash[JavaGaming.org][The Java Tutorial][Slick][LWJGL][LWJGL Tutorials for NeHe][LWJGL Wiki][jMonkey Engine]
I was looking at some other tutorials and replaced the code with this: i use 0.75 to represent the overlapping part:

// even row
if (i%2==0) {
hexGraphic[j].draw(g,
(int)xPos+i*(int)(HEX_SIZE*0.75),
(int)yPos+j*HEX_SIZE);
}
// odd row
else {
hexGraphic[j].draw(g,
(int)xPos+i*(int)(HEX_SIZE*0.75),
(int)yPos+j*HEX_SIZE+HEX_SIZE/2);
}
}

is there anything wrong with this? it seems to work fine, i just hope it doesnt mess up other calculations for placing objects on certain hex coords.
DarkTech Software.

This topic is closed to new replies.

Advertisement