Jump to content
  • Advertisement
Sign in to follow this  

Hexagon Map Subdivision Coordinate Problem

This topic is 2774 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I have a website based MMO space trading game I'm working on and I'm having trouble with hexagon math when it comes to finding the subdivision on the map a particular hex is located in. I have a working site to actually toy with and to better understand my problem but I will also explain here with screen shots first.

Bring this test page up and click "Show Hex" in the upper right to understand what I'm saying here:
PLEASE USE Internet Explorer! I just realized there's a problem with the mouseover using Chrome.


I have a large galaxy on a hexagon map and this map is subdivided by larger hexagons on a scale of 1 to 25. The big hexes are 25 "Smaller" hexes across. From the center of one subdivision to the center of another is 25 hexes. It is also subdivided 5 times as you zoom out to view the entire galaxy. I call these big subdivisions "Quadrants" then "Zones" etc down to the actual Galactic Grid coordinates that something is on.

It looks like this as you zoom in down to New York on Earth:


As you zoom in you are clicking on a coordinate in the big hex where the center of the hex is 17:17. As you click down to New York in the screenshot, it has a a set of coordinates that look like this: 17:24, 17:17, 17:17, 17:17, 18:12, 11:14 and New York is on galactic coordinate -19:-68359247 from center of the universe. I translate the coordinates to actual named objects if they exists in the navbar. My hexagon map coordinate system is laid out like this:


I can figure out the galactic coordinate by multiplying the other coordinates offset by 17:17 and added to (25 ^ <the zoom level>). Which is to say, for every top level "Quadrant" hex you are offset by, your galactic coordinate moves by about 9765625 (25 to the 5th power) and then for every second level "Zone" you are offset from 17:17 by, you are at an additional 390625 (25 to the 4th power) hexes in that direction etc.

Its done in the database where you pass in all the coordinates and it gives you the galactic coordinate. The SQL code looks something like this:

Declare @intGalacticHex_X int
SET @intGalacticHex_X = 0

SET @intGalacticHex_X = @intGalacticHex_X + ((17 - @Quad_X) * (POWER(25,5)))
SET @intGalacticHex_X = @intGalacticHex_X + ((17 - @Zone_X) * (POWER(25,4)))
SET @intGalacticHex_X = @intGalacticHex_X + ((17 - @Sector_X) * (POWER(25,3)))
SET @intGalacticHex_X = @intGalacticHex_X + ((17 - @System_X) * (POWER(25,2)))
SET @intGalacticHex_X = @intGalacticHex_X + ((17 - @Hex_X) * (25))
SET @intGalacticHex_X = @intGalacticHex_X + ((17 - @Grid_X))

-- Return the result of the function
RETURN @intGalacticHex_X


While I can figure out your galactic coordinate based on all the upper tier coordinates you've provided, I haven't figured out how to the reverse it correctly. As in, If I give you a galactic coordinate, say New York on -19:-68359247 , I need to return the Quadrant, Zone, Sector, System, Hex and Grid Coordinates back (i.e. 17:24, 17:17, 17:17, 17:17, 18:12, 11:14)

It's a similar problem to the way you can translate pixel coordinates of the mouse to Hexagon coordinates, which I've been able to figure out with a lot of help, but I can't figure this one out as I can't find anyone who has an example.

Take the following graphic, which is kind of a zoom in of earth near Jupiter from the other screenshot, to to see how it is not straightforward:


Much like pixel to hexagon coordinates solutions, you probably need to break up the hex and do a different algorithm depending which part of the bigger hex it is in. New york is a bad example because its towards the center, but a spot towards the tips on the left or right is more difficult to figure out. As its vertical coordinate changes it runs across one subdivision for a few hexes and then the next for several more hexes.

Also, there is a "border" between hexes where the two bigger hexes technically share half of the hex. If you are on that border (There will be ships flying around) I need to have that ship resolve its coordinates in a consistent way, most likely dropping a fraction and having them be in the subdivision to the upper left. As long as they resolve consistently it's irrelevant which way they go but they cannot be in two subdivision hexes at the same time.

I tried a few different ways of resolving this but I can't wrap my head around it. The irony is that I can determine your galactic coordinate given all the other coordinates but I couldn't reverse it. I feel the simplest solution is to simply REVERSE the process I used to get your galactic coordinate using algebra but again, I tried and failed. I can figure out your X coordinate by using the powers of 25 and adding them together but to reverse it and get back all the other coordinates, I think I need to know your Y coordinate also.

I've tried some stuff using MOD 25 to try to figure out which subdivision you are on but again, I think I need to use both coordinates or try to figure out your "offset" first or something.

You can also zoom to the center of the galaxy to bring the galactic coordinates down to a manageable size and understand the coordinate system I'm using better. Start in the Milky Way and choose the center hex 17:17 all the way down and you should be at galactic 0:0. then zoom out and move around a bit to see how the coordinate system works.

Share this post

Link to post
Share on other sites
Does this simplified example code help you? It's C++, but the meaning of the code is practically self-evident. What you did so far was pretty clever.

#include <iostream>
using std::cout;
using std::endl;

#include <cmath>

int main(void)
long unsigned int galactic = 6;
long unsigned int solar = 13;
long unsigned int planetary = 3;

const long unsigned int galactic_factor = 625; // 25^2
const long unsigned int solar_factor = 25; // 25^1
const long unsigned int planetary_factor = 1; // 25^0

// combine individual position components into one position
long unsigned int pos = galactic*galactic_factor + solar*solar_factor + planetary*planetary_factor;

cout << pos << endl;

// retrieve individual position components
long unsigned int g = pos / galactic_factor;
long unsigned int s = (pos - g*galactic_factor) / solar_factor;
long unsigned int p = (pos - g*galactic_factor - s*solar_factor) / planetary_factor;

cout << galactic << ' ' << g << endl;
cout << solar << ' ' << s << endl;
cout << planetary << ' ' << p << endl;

return 0;

It's worth noting that if you go to Wolfram Alpha and type in "solve for g where x = g*a + s*b + p*c", you'll get a basic idea of what I did here. Of course, since the factors are of a different order of magnitude, the solution presented in the code is thankfully simpler than that given by Wolfram Alpha. For what it's worth, algebra is evil and I feel your pain. Hope this helps.

Share this post

Link to post
Share on other sites
Sign in to follow this  

  • Advertisement

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!