Jump to content
  • Advertisement
Sign in to follow this  
ambershee

Mapping / Projecting a Pixel to a Hexagon?

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

If you intended to correct an error in the post then please contact us.

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 this post


Link to post
Share on other sites
Advertisement

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:

 

HexagonalGrid_700.gif

 

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 this post


Link to post
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 :)

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!