Jump to content

  • Log In with Google      Sign In   
  • Create Account


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.

  • You cannot reply to this topic
8 replies to this topic

#1 Telcontar   Members   -  Reputation: 720

Like
0Likes
Like

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)

hex_arrangement.png

 

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


Sponsor:

#2 Paradigm Shifter   Crossbones+   -  Reputation: 4784

Like
0Likes
Like

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:

 

hexgrid.png

 

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

#3 Telcontar   Members   -  Reputation: 720

Like
0Likes
Like

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   Crossbones+   -  Reputation: 10629

Like
2Likes
Like

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.



#5 haegarr   Crossbones+   -  Reputation: 3260

Like
0Likes
Like

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.



#6 Telcontar   Members   -  Reputation: 720

Like
0Likes
Like

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   Crossbones+   -  Reputation: 10629

Like
1Likes
Like

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   Crossbones+   -  Reputation: 10629

Like
1Likes
Like

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;
}


#9 Telcontar   Members   -  Reputation: 720

Like
0Likes
Like

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.



PARTNERS