Mapping / Projecting a Pixel to a Hexagon?

This topic is 2034 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

Recommended Posts

Hello!

I'm working on a hexagonal grid based setup in an engine that doesn't have any kind of native support for it (in fact, I'm quite limited) - at present I'm looking to build the grid into the world shaders; projecting it downwards over the game world. This part is relatively cheap and easy and doesn't worry me. Doing more complicated stuff, such as drawing ranges (such as character movement, or weapon ranges or whatever you might expect in a 2d grid based game) by shading the appropriate tiles is seemingly quite a lot more difficult.

One suggestion that seems valid, is to draw out the selection to be shaded to a render target. Since a hexagonal grid is structured more or less the same way as a square grid, you could represent each hexagonal cell with a single pixel - meaning that a selection that is a maximum of 8x8 cells square wouldn't need to use a render target any larger than 64 total pixels.

My question is thus - how would one go about the maths to project each pixel in the render target into a hexagon in world space?

Or is there a more valid approach to my problem?

Thanks!

Luke

Share on other sites

Well it depends how exactly you need your hexagon to be defined, but you can do it as follows. First find the x/y coordinate of the pixel. Then, locate the center of the associated hexagon. Because a hexagonal grid makes an "interleaved" pattern, the x-coordinate is simply the same as the pixel's x-coordinate whereas the y-coordinate depends on parity - for even x, it's the same as the pixel's y-coordinate, and for odd x it's the pixel's y-coordinate offset by half a pixel:

Then once you have that center point for a given (x, y) hexagon, you know the hexagon has a normalized "radius" of half a pixel, so you can calculate its bounds that way, and then scale it up and map it on the surface of your choice.

At least that's how I would go about it first. But again, it depends what information you need about the hexagon.

Share on other sites

Thanks Bacterius, that's quite helpful. I have no specific intended direction for the visual outcome (instead it needs to be flexible). I'm thinking that the best option may be to generate a 32-bit mask, which means simply drawing straight over the top of where the hexagon should be within the shader - from there I should be able to blend in / out anything that I might later desire based on whatever data I sneak into each channel (simple colours, icons, whatever). This means I only need to work out the geometry of said hexagon; but based on the origin of each, I should be able to work it out using a predefined radius :)

1. 1
2. 2
Rutin
18
3. 3
khawk
15
4. 4
A4L
14
5. 5

• 10
• 13
• 26
• 10
• 11
• Forum Statistics

• Total Topics
633745
• Total Posts
3013667
×

Important Information

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!