Hi,
Does anyone know of any Defold games (paid, free, tutorials etc that use hex based tile systems? I am interested in developing one with Defold and curious to see previous examples.
Hi,
Does anyone know of any Defold games (paid, free, tutorials etc that use hex based tile systems? I am interested in developing one with Defold and curious to see previous examples.
Hereâ€™s a sample of how I work with hex grids in my project. Itâ€™s early days, so Iâ€™m still experimenting. Iâ€™m using a three-axis coordinate system as described here: Red Blob Games - Hexagonal Grids
I chose to use vmath.vector3
for coordinates. This allows you to, for example, add two coordinates with the + operator (instead of having to pass two tables to a custom add
function).
Unfortunately, coordinates in vector3 format canâ€™t be used as keys in tables. I hash coordinates to produce integer identifiers which can be used as table keys. Though, itâ€™s not really a hash, just some bit operations to pack the x and y components into an integer. No information is lost, so the vector3 can be recreated from its hash.
Each edge (the border between two hexes) has the coordinate equal to the sum of the two hexes divided by two. To avoid non-integer values (which would screw up the hashing function) I only divide by two for display purposes. Edge coordinates (or hashes) can be split to get the coordinates of the two hexes.
I use the following terms in code:
nc
: node coordinate (I use the term â€śtileâ€ť for the gameplay piece the player sees)nch
: node coordinate hash, used as a node identifier as described aboveec
: edge coordinateech
: edge coordinate hashhalf_axis
: the X half-axis is the line where X is zero, which is perpendicular to the X axis, useful when walking along edgesedge_directions
: the vectors from one edge to its four neighbor edgesparallel_edge_directions
: the vectors from one edge to the two edges across its two hexesIâ€™ve made changes in the repository to address a few issues Iâ€™ve found:
My current solution represents each coordinate as a plain integer, encoding the x and y axial components with their bits interleaved. This produces a low integer, allowing it to be used as an array key. Iâ€™ve removed the use of vector3, and instead I pass and get back the x and y components from the module functions.
A lookup (array) table using these keys/coordinates requires an array of about twice the capacity relative to the number of keys (as determined by the greatest key value). Since my maps arenâ€™t huge, this works great for me. Hereâ€™s keys vs capacity for hexagon-shaped maps with radius 1 through 12:
Thanks, this is very helpful.