Deciding on how to approach hex grid in my project

I have a concept and main ideas for a game ready, today I sat to start bringing the thing to live, but I got myself into trouble right at the beginning. I am not an expert when it comes to coding, just an amateur making games for fun, so I like to have everything planned.

The game is a 3D puzzle/adventure with elements of RPG. The levels consist of hexagonal tiles placed at varying heights and with different accessibility. I think Olive by Beck Sebenius is a good reference point to what I’m planning.

The three things about the grid I’d like to introduce are:

  • tiles have different logics (land, water, ice, lava etc.) affecting their walk ability and interactions with game objects,
  • if the height difference between neighbouring tiles is higher than a set limit, the two tiles are considered disconnected and cannot be walked from one to another,
  • certain objects (fences, walls etc.) also disconnect two tiles. NOTE: some of these objects (e.g. gates) change their state, so they don’t disconnect the tiles for the whole time.

The whole disconnecting thing is giving me headache. Because the game uses pathfinding I have to store the walk ability data in some form (e.g. as an array) which changes dynamically. With a possibility of neighbouring tiles both being accessible but disconnected from each other this becomes hard. Having another array with tile connection data means a lot of additional data to handle and complicates the pathfinding to the point beyond my programming capability. What would you do?

Hex grids can be stored in a 2D array. The neighbours of each hex can be calculated based on tile position.

I would build a custom class to store the data at each hex tile, including the height and the terrain type.

I would use A* for pathfinding. When considering the neighbours to each hex I would compare heights to see if they are connected.

If you are new to pathfinding building a prototype on a square grid is useful as a learning exercise, it drops out one layer of complexity.

This was moved to the wrong forum. Should be in scripting or general I think…

Anyway, hex maps are actually surprisingly difficult to code stuff against. The coordinate systems are difficult to reason with, and that makes it way harder to work with than a conventional grid.

I know it sounds kinda dumb and surprising, but it’s true. Here’s a primer on hex coordinates: http://www.redblobgames.com/grids/hexagons/

He covers obstacles and the like.

If this is a little too much - you can probably pick up something on the asset store.

Nice article. The axial coordinate system looks the easiest to code against for this use case, it has the cleanest relationship between neighbours.

But I agree, I certainly wouldn’t target a hex grid unless you can solve every problem for a square grid first. No point trying to learn pathfinding on a hex system.

1 Like

I am aware of all the problems related to hex against square issue, the coordinate change isn’t that much of a problem (I’m far better skilled as a mathematician than a programmer), in fact it’s just about changing the coordinate system and related equations. The real problem is the disconnecting tiles via objects.

The Asset Store may prove helpful, I haven’t looked at RAIN AI for Unity before (I hope it will be able to work with my grid, it would mean the rest of features would work as well), there’s also outdated Simply A* offered for free.

EDIT: Oh my, I’ve just instantiated a Waypoint Network with RAIN and there’s a huge Connect/Disconnect button in their editor, I think I’m getting closer to solving the problem.

That link includes pathfinding examples with code, obstacles, all that stuff.

If you’re trying to learn how to do stuff, it might be worth trying to implement something relatively simple like this. It could teach you a lot.

Anyway, good luck!

I’ve used the “Grids by GameLogic” from the asset store for hex grids with great success. It’s a nice easy to use solution and it has A* pathfinding.

Just a thought.

This!

A buddy of mine pointed me to that Red Blog article a couple of weeks ago. It is simply fantastic, I wish I could find articles that comprehensive on other random topics. Everything you need to know is right there in one place. I knew virtually nothing about hex grids, and was able to hit the ground running with that article. I built a hex map editor and have pathing, and basic pathfinding in about a week thanks to it.

2276474--152728--hex_wip_004.jpg
A few more pics:
http://zombiegorilla.tumblr.com/post/128140965307/hex-based-game-builder-wip
http://zombiegorilla.tumblr.com/post/128144953492/more-hex-builder-images

Working with hex based grid is a little odd at first to wrap your head around, but after a bit it starts to make more sense.

1 Like

Damn bro. You disney guys don’t mess around with your editors, I’m not sure what’s more beautiful the art or the editor.

In fairness, the models are from the asset store, the Moba pack from k4. I needed something to jazz it up a bit while my buddy works out the style.

Damn, these are very good looking screens, they make me want to steal your code somehow…

How’s the performance? I got myself to a point where a 40x40 grid caused massive FPS drop, RAIN is not suitable for grids with its waypoint system. I think I’ll have to set up a system from scratch (heavens help me).