# Creating a hexagonal wraparound map

I'm trying to create a wraparound map for a hexagonal map, so when you exit a tile on one side of the big hexagon-shaped map, you enter a tile on the other side.

In the image, the pale tan hexagonal chunk in the center is our map. The greenish/reddish chunks around it are shifted copies of the same map. If we stand on the blue tile under the cursor and walk off the main map into the green copy to the right, that's the same as wrapping around our map to the corresponding point on the other side: here, walking from the blue tile in the lower-right-hand red copy into the bottom-right corner of the center map.

I used an approach with a lookup table first. But as soon as those maps get too big, the lookup tables get even bigger. I'm searching for a solution which works without a lookup table.

• Civilization only wraps around horizontally, there is no need of lookup table for that (you just store it as a rectangular map, and represent it with offset coordinates to make it a hex grid). From reading the linked article I think you want to wrap around on three directions (connecting each side of the hexagon with the opposite). I suggest to make that clear in the question. – Theraot Feb 19 '17 at 2:04
• @Theraot thank you, updated the question – InsOp Feb 19 '17 at 9:57

Here's a simple method which does not become more complex as the map grows in radius:

1. Keep a list of the center coordinates of your main map (0, 0, 0), and its 6 shifted copies.

2. After a move that might take you off the edge of the map, calculate the distance to your destination tile from each of these center points.

3. Stop when you find a center point whose distance away is less than or equal to your map radius.

4. Subtract this center point from your destination point. Now your destination is correctly expressed as an offset from the center of your original map (0, 0, 0).

Implemented directly, this is at most 7 distance checks, no matter how big your map gets.

But, if your distance from the original center is less than your map radius, you can skip the other checks entirely, since you haven't crossed an edge.

• Thanks for the edit and for this answer. I did not fully understand the last step, but Im sure i will, when im implementing it. (Then this answer gets marked as the correct one) – InsOp Feb 19 '17 at 16:30
• What part is unclear? I'm happy to edit and clarify it. – DMGregory Feb 19 '17 at 16:33
• No everything is fine. I calculated it with an example and it seems to be a superb solution. On step 4 I just couldnt do the math in my head, thats why I was confused. – InsOp Feb 19 '17 at 16:41

You can actually do a rectangular projection without wasted space in the array. This enables yo has the added benefit, that the character visually goes upwards instead of in an angle.

You can get the center of a hex with the following formula (width and height is the hexagon width and height, x and y are the current position):

centerX = x * width + (y % 2 + 1) * width / 2
centerY = y * height + height / 2


If you have a map with the width n and height m, then every hexagon on the left side will have the x coordinate 0, on the right side the x coordinate n, in the top row they'll have a y coordinate 0 and and on the bottom a y coordinate m.

This also means the four corners are (0; 0), (0; m), (n, 0), (n, m).

So, to achieve a wrapping effect, you need to check if the x coordinate of the player is bigger than n, and if it is, then set it to 0, if it's smaller than 0, then set it ton, if the y coordinate is smaller than 0, then set it to m and if it's bigger than m, than set it to 0.

In code:

if (x < 0) x = n;
if (x >= n) x = 0;
if (y < 0) y = m;
if (y >= m) y = 0;

• did you consider that the map shape is also a hexagon? So there is no width and height, only a radius – InsOp Feb 19 '17 at 13:59
• @InsOp you never mentioned it's a hexagon before I posted my answer – Bálint Feb 19 '17 at 20:32
• Yes i am sorry, i appreciate your answer – InsOp Feb 20 '17 at 11:25