View more

View more

View more

### Image of the Day Submit

IOTD | Top Screenshots

### The latest, straight to your Inbox.

Subscribe to GameDev.net Direct to receive the latest updates and exclusive content.

# Hex Coordinate Math - Deriving the center of a "Superhex"

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

8 replies to this topic

### #1Telcontar  Members

Posted 17 July 2013 - 02:37 PM

Alright, so I'm building a game that uses hexagons for its maps. For my dark and mysterious reasons the world also needs to be split up into "Supertiles" (or Superhexes as I call them in the thread title) composed of nineteen hexes as shown in the picture below.

Assuming the origin tile of (0,0) is the center of a supertile, I'm trying to figure out a formula to calculate whether or not any other particular tile is the center of a supertile. (If no such exists, I can fall back on recursive algorithms to determine it after awhile using "jump towards origin by supertile-centers and see if you can hit 0,0", but I'd love to be able to calculate it directly).

I have the following tables for determining the centers of any NEIGHBORING supertile based on the coordinates for the center of the CURRENT supertile:

(N1 labeled in picture, go around clockwise for N2-6)

If the X-Coordinate is even:

• N1: (+3, +3)
• N2: (-2, +4)
• N3: (-5, 0)
• N4: (-3, -4)
• N5: (+2, -4)
• N6: (+5, -1)

If The X-Coordinate is odd:

• N1: (+3,+4)
• N2: (-2, +4)
• N3: (-5, +1)
• N4: (-3, -3)
• N5: (+2, -4)
• N6: (+5, 0)

I have not yet found a solution to the problem by internet searches or using my own meager skills with math. If anyone can point me to one or figure it out, I will be eternally thankful. Furthermore if any part of the info above is incorrect or unclear please let me know - my brain hurts a bit, so it wouldn't surprise me.

I'm going to go sit in a corner and look at some pictures of kittens to soothe my brain.

I Create Games to Help Tell Stories | Writing Blog

Posted 17 July 2013 - 05:26 PM

Your best bet for hex grids is to represent them as a rectangular grid but displace every other row by half a tile.

Like so:

That will probably make things easier for you to think about...

(You can still draw the hexes how you like, just represent the map like that, it will make the maths easier).

"Most people think, great God will come from the sky, take away everything, and make everybody feel high" - Bob Marley

### #3Telcontar  Members

Posted 17 July 2013 - 08:34 PM

Yes! Actually, that is in fact how they are arranged - as a simple 2D array. The graphic and all coordinates are calculated using that. Apologies for not mentioning that in the original post.

I Create Games to Help Tell Stories | Writing Blog

### #4Álvaro  Members

Posted 18 July 2013 - 05:32 AM

I find the arithmetic much easier to handle if you use coordinates where moving down is always adding (0,1) and moving down and to the right is always adding (1,0) (or any other such convention). Those are still fairly standard coordinates in an affine plane, which means a lot of familiar things still work.

So start by transforming from your coordinates to the ones I describe (it should be as easy as taking (x,y) and mapping it to (x, y-floor(x/2)), or something like that).

Now the list of neighboring offsets is (3,2), (-2,5), (-5,3), (-3,-2), (2,-5), (5,-3)

The grid is now much more understandable, and the centers of the hexagons are simply of the form A*(3,2) + B*(-2,5), for some integers A and B.

From here there are several ways to finish the job. For instance, taking the matrix with columns (3,2) and (-2,5) and inverting it, I get this rule: A point (x,y) is the center of a hexagon if and only if 5x+2y is a multiple of 19 and 2x-3y is a multiple of 19.

### #5haegarr  Members

Posted 18 July 2013 - 06:40 AM

My approach was to look at the 6 directions in which the neighboring hexagons can be reached as 3 axes of a space. Hence one gets co-ordinates to the 6 surrounding super-hexagons as (0,3,2), (3,2,0), (2,0,-3) and their negatives, of course.

However, this leads to a similar system as in Avaro's solution above but with 3 equations, and hence seems not be worth to be investigated further. Probably this is because Alvaro's co-ordinates and mine can be transformed into each other.

### #6Telcontar  Members

Posted 18 July 2013 - 09:26 AM

I find the arithmetic much easier to handle if you use coordinates where moving down is always adding (0,1) and moving down and to the right is always adding (1,0) (or any other such convention). Those are still fairly standard coordinates in an affine plane, which means a lot of familiar things still work.

I'm not certain I understand this. Can this be done while still using a 2D array of values to store the hexes (as a heightmap) in memory?

I Create Games to Help Tell Stories | Writing Blog

### #7Álvaro  Members

Posted 18 July 2013 - 12:30 PM

I'm not certain I understand this. Can this be done while still using a 2D array of values to store the hexes (as a heightmap) in memory?

Yes, you only needed to read one more line in my post:

So start by transforming from your coordinates to the ones I describe (it should be as easy as taking (x,y) and mapping it to (x, y-floor(x/2)), or something like that).

### #8Álvaro  Members

Posted 18 July 2013 - 12:38 PM

Try this:

bool is_center(int x, int y) {
y -= (x - (x&1)) / 2;
return (5*x + 2*y) % 19 == 0 && (2*x - 3*y) % 19 == 0;
}

### #9Telcontar  Members

Posted 18 July 2013 - 01:29 PM

Oh, I read it - I just didn't understand it either. I believe I mentioned my meager math skills.

Either way, I think I get it now. Many thanks! That ought to about be the solution I need.

I Create Games to Help Tell Stories | Writing Blog

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.