Infinite hexagonal board with axial coordinates

When it comes to hexagonal boards, the choice of a coordinate system is primordial. From that choice derives the complexity of the underlying algorithms, like pathfinding for example.

Hexode uses a system of axial coordinates. Each hexagon has its position defined by two integers (U,V). This naming convention may be a bit misleading as those are the same letters used for describing texture coordinates. But I think the code itself is not ambiguous as I have defined a custom Unreal structure “HexCoordinate”, which is really handy when it comes to using it with Blueprints.

hexagonal_coordinates

Transformation to world location (X,Y,Z) from hexagonal coordinates (U,V) is straightforward (for positioning the hexagon in the world):

And the opposite transformation:

To save drawcalls and thus improve performance, I use an “instanced static mesh component”. Because there may be potentially a lot of hexagon meshes on the screen at the same time, it actually makes a big difference on frame rate.

DisplayTile is then called by the HexBoard_BP Blueprint. This is allowed because in the header file, the function is annotated as a UFUNCTION.

The construction script below builds an hexagonal board. It is basically two for loops. Note the use of “Make HexCoordinate” nodes that are custom structures, as evocated before.

hexboard_construction_script

And here is how the custom HexCoordinate structure is defined in code.

Another important component for the hexagonal board system is the Blueprint method to get the hexagonal coordinates corresponding to the the mouse cursor on the screen. The key function to make that possible is “Get Hit Result Under Cursor for Objects”. It is actually quite simple but I made it a separate function to make the event graph clearer.

get_cursor_uv

This concludes this (quite technical) post. It does not cover every details and functions but it tries to aim for the core functionalities of the HexBoard Blueprint. Incidentally, it is a good example of how to achieve procedural generation in Unreal Engine. The implementation combines both the power and expressiveness of raw C++ with the easiness and fast prototyping capabilities of the Blueprint system. I have to say that I am quite impressed by the latter. Though I miss a little bit Unity3D with C# and the glorious fast compilation times, the Unreal blueprint system has proven quite flexible and intuitive to use. So far so good… 🙂

The dev.

Leave a Reply

Your email address will not be published. Required fields are marked *