Results 1 to 6 of 6

Thread: Hexagons

  1. #1

    Hexagons

    The hexagon is a cool shape but somewhat cumbersome to code. At least not as straightforward as the square.

    For my current project, which will be a successor of the Dark Vesta game, I'm intending to use a hex-grid. Not really necessary but I like the honeycomb pattern.
    Coding is ongoing. Nothing to show yet but the game will again be pretty simple. VGA size, few colours. Mostly text based, except for the hexagons.

    Have you used hexagons in your game programming? Any thoughts?

  2. #2
    Hexagons are not as hard to code as you may think. Unfortunately I can't find and old article from where I learned most about using Hexagonal grids. And my old laptop on which I have a stored link to it does not want to boot up.

    I did manage to find another website that offers tons of information about working with Hexagonal grids at: https://www.redblobgames.com/grids/hexagons/

    I hope it will help you get started.

  3. #3
    Thanks for the link and feedback.

    The hexes are not too hard to code, I can handle that part, but more angles and lines to keep track of so code will be bulkier and more risk of messing up. And just like that link thoroughly described, there are different ways of managing hex cell coordinates. In my case the hexes will mostly be used as a fancy way of showing the progress and not so much for navigating around. Not like those ancient hex-grid based board games that were popular pre computer era. So I'll use a column, row coordinate system with extra code to determine even or odd columns.


    It will be a tiny grid of 3*6 hexes, because VGA size and concept test. Mouse of arrow keys will have to do for navigating around.


    Anyway. The question was more about general use of hexes in computer games. For example there is the hex minesweeper game. Have any of you coded one? I haven't tried that, and probably never will. Have you used hexes in any other computer game or game concept?

  4. #4
    Quote Originally Posted by Jonax View Post
    I can handle that part, but more angles and lines to keep track of so code will be bulkier and more risk of messing up. And just like that link thoroughly described, there are different ways of managing hex cell coordinates.
    You can easily render hex grid using sprites almost as easy as you would with square based grid. All you need to take care is to offset every even line. I don't remember the offset values right from my head.
    When calculating which hexagon your mouse is over you can just do so by calculating distance to the center of the nearby hexagons. The closest center belongs to the hexagon that is bellow your mouse.



    Another clever approach that is not mentioned on the page that I linked is to treat the hexagonal grid as normal square grid, like shown in the picture bellow.
    Hexagonal grid.png
    So for getting the row number you just divide the mouse Y position by the row height. Now I can't remember what size this is in comparison to the overall size of the individual hexagon.
    For getting column number you just divide mouse X position by the width of the hexagon. And yes you do need to add half of the hexagon width to the mouse X position whenever you are on an even numbered row to account for for offset positions of hexagons on even lines.

    Now you might be wandering: "But how do you deal with slanted edges of the hexagon?"
    Here is where this approach is the most clever. Instead of dealing with bunch of complex math you simply figure out where within this one grid cell your mouse is positioned and simply perform a lookup of a pixel colour value of a special cached texture (as shown below) whose dimensions are same as one grid cell. In the above image you can see how this texture would fit into a grid.
    Hexagonal grid - pick template.png
    So if the returned colour is Yellow then the row and column positions are the same as you got in previous step.
    If the returned pixel is red then you simply shift your row position up by one.
    If the returned colour is green then you shift both your row position up by one and row position to the right by one (provided that even lines are shifted to the left).

    This approach avoids the need to use any complex math and is thus extremely fast. It was used in several old games since back in the days making square root calculations was pretty slow since it relies in floating point based math. But this approach can be entirely done using integer based math so no FPU support was required.


    Quote Originally Posted by Jonax View Post
    Anyway. The question was more about general use of hexes in computer games. For example there is the hex minesweeper game. Have any of you coded one? I haven't tried that, and probably never will. Have you used hexes in any other computer game or game concept?
    I have't actually used hexagonal based maps in any of my projects yet. But as I mentioned I did read a good article on this matter in the past so I do have some knowledge about how it works.
    And yes I have played several games that make use of hexagons. In some games use of hexagons actually improves the game-play in comparison of how it might be if it was limited to square based grid. But in some use of hexagons doesn't bring much to the game. So it all depends of how it is incorporated into the game-play.

  5. #5
    That cached texture thing was a neat trick. Really cool trick.

    I won't use that method but I think generally it's a good thing to stick to integers when possible. Modern computers are very powerful compared to ancient ones but it's still good if you can keep the code lean and not tax the processors unnecessarily. Of course not at the cost of making spaghetti code. When a square root really is needed I never hesitate to use the sqrt() function.

    As for the current game I'm dabbling with the hexagons are not necessary for the gameplay but I like the shape. I guess I have to first make some super easy concept study program to show what I got. But even that takes time despite re-using code from last game. Coding is ongoing. Better not promise too much but hopefully I can present something playable next month or so.

  6. #6
    Quote Originally Posted by Jonax View Post
    That cached texture thing was a neat trick. Really cool trick.
    Yes it is. You can also use similar approach when working with diamond shaped isometric grids. Except in that case the cached texture would have 5 different colours

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •