Hex Pathfinding Algorithm

2 favourites
From the Asset Store
Finding the shortest path through the cells based on the A-star algorithm
  • I've created the template with a hex pathfinding algorithm. It's based on 2D Array object. I don't use any additional plugin and behavior (except "MoveTo").

    HOW IT WORKS

    1. At the beginning, an array is filled with numbers using the "Wave Algorithm" method. This means propagating "waves" around neighboring tiles until the destination point is reached.
    2. Then, a revers path is found using a simple subtruction of the index from the current value through a loop.

    Basically I used a wave algorithm to fill the array. The main feature is how to determinate neighboring hexes:

    Try this on Arcade

    Download template

    Tagged:

  • Another cool example!

  • This is great. Can you put hex sides into it--like rivers?

    good work

    yours

    winkr7

  • After looking more closely I was wondering how you could assign a movement cost to each hex? Like swamp takes two counts of move vs one for open areas.

    It is a very good start.

    yours

    winkr7

  • After looking more closely I was wondering how you could assign a movement cost to each hex? Like swamp takes two counts of move vs one for open areas.

    It is a very good start.

    yours

    winkr7

    I have implemented this mechanics in another turn-based example (sorry for bad English there). But in that case, I use a square grid of tiles. So, I will update hex-grid template to add movement cost mechanics in few days :)

  • Great. I look forward to this.

    yours

    winkr7

  • This algorithm is very cool - I didn't realize you could make something so complicated so easy to understand. Great stuff!

  • Try Construct 3

    Develop games in your browser. Powerful, performant & highly capable.

    Try Now Construct 3 users don't see these ads
  • I am very inspired by the fact that the post above has received so many likes. So I decided to continue developing this template and add new mechanics to it.

    So, let's say we want to create a turn-based game. Let's add numerical a variable "MP" (movement points) to the unit and set its value to 3. This way, the unit can move 3 hexes in one turn.

    Now let's add new types of hexes: forest and mountains. Let's make it so that a unit can move through all hexes, but each hex will have a different movement cost. For example, the "forest" hexes has a cost of 4 points, the "mountain" costs 6 points, and the regular hexes costs 3 points. Also, we'll set the unit's movement points to 9. And the "water" hexes will remain impassable for the unit.

    Now let's add a line to indicate the movement boundary for the unit. It's very simple to do this. We need to check the values of neighboring cells in the array, and if the value of a neighboring cell is greater then the unit's movement points, then we set a boundary line between two hexes.

    Like this post if you like my template. Perhaps I will continue to develop this example and add new mechanics to it ;)

    Try on Arcade

    Download this template

  • This is very good work. I would like to see rivers as hex-side terrain. These hex-sides could also be walls or doors in a dungeon setting. Also, If i pick three or more hexes (these could be the locations of enemies or treasure or whatever) could you add a find closest function? IE, if there are 4 enemies find the closest one, or if there are 3 places that are adjacent to two enemies find the closest one. IE, the user selects a set of hexes and then the function finds the closest hex to the unit.

    Great work.

    yours

    winkr7

  • This is really great! I have been working on my own turn based system using tilemaps and snapto but the pathfinding is very wonky. If this were more developed it would be something I would actually pay money for. Keep it up!

  • I thank all of you for the feedback! I've been studying Construct 2/3 for over 6 years. Very often I heard the opinion that this engine is only good for casual games and is not suitable for anything more. My goal is to break this stereotype. So I will continue working on the template. Also, I promise that this template will be free of charge always.

  • UPDATE

    Added the mechanics of infinity map panning, similar to the game Civilization. Imagine your map as a planet. As we know, planets have a spherical shape. You can continuously move in any direction and never reach the edge of map.

    How it works:

    • I do not use "scroll to" action for movement. Instead, I move the objects themselves.
    • When the objects go beyond the screen, I move them to new position.

    A more complex task is to adapt the pathfinding to an infinity map. The cells of the array can't be moved as easily as the hex sprites. It would have been an unnecessary complication. So, I came up with simpler solution. Now, the starting position for pathfinding is always at the center of the array.

    When a player clicks on a unit, the hexes adjust their virtual coordinates (instance variables) as if the unit were in the center of the map. Thank to this approach, I didn't have to make significant changes to the pathfinding (which is based on 2D array).

    Try on Arcade

    Download template

  • Hello;

    As we know, planets have a spherical shape. You can continuously move in any direction and never reach the edge of map.

    It doesn't matter, but the topology of the map you created with this wrap-around in x and y is a torus, not a sphere. In a sphere if you move north from any point you will come to the same point sooner or later (the north pole). A torus makes for an easier game surface anyway so good choice.

    yours

    winkr7

  • It doesn't matter, but the topology of the map you created with this wrap-around in x and y is a torus, not a sphere. In a sphere if you move north from any point you will come to the same point sooner or later (the north pole).

    You're very welcome for the explanation! My understanding of geometry is quite basic.

    I would like to see rivers as hex-side terrain. These hex-sides could also be walls or doors in a dungeon setting.

    Do you mean something like that?

  • Its just a quibble point about topology. You are doing fine work. The wrap around in both x and y, where going around and around leads you back to the same spot (but a unique spot--ie not all going through the north pole) is the topology of a doughnut, not a sphere.

    Anyway, I would like to see some rivers as hex sides, or a closest hex function (I don't care about the topology it is fine).

    good work

    yours

    winkr7

    PS

    To make your work more useful you need a family, call it something like--hexPathable. If a sprite is in this family then you can do the calculations. As it is, you are using a single sprite, called unit and referring to things like unit.x. You want to make unit a member of a family and refer to things like hexPathable.x.

    just a suggestion

Jump to:
Active Users
There are 1 visitors browsing this topic (0 users and 1 guests)