2

I'm trying to draw a map with hexagonal tiles. However, I'm struggling to get the tiles to line up properly, so there is no space inbetween them. Here's a picture of my problem. One with the tiles I use normally and one zoomed in on with another tile made to make sure it's not just the antialiasing playing tricks on me.

This is the code that's responsible to get the pixel coordinates for every tile. Apparently there's some miscalculation with the offset of every odd row, but simply adding or subtracting a pixel only increases the gaps.

private static final float HEX_WIDTH = 97; // 139 for colored tiles
private static final float HEX_HEIGHT = (float) (Math.sqrt(3)/2 * HEX_WIDTH);

public Vector2 getHexCoordinates(Hexagon hex) {
    float x = (float) HEX_WIDTH * hex.getGridX() * 3 / 4;
    float y = (float) HEX_HEIGHT * (hex.getGridX() / 2 + hex.getGridY());
    y = (hex.getGridX() % 2 == 0) ? y : y + HEX_HEIGHT / 2;
    return new Vector2(x, y);
}

In addition here's the tiles I'm using in case anyone is wondering.

Askr
  • 57
  • 7
  • I noticed that **y** does not increase constantly (as gridx/y increases). It's a very small deviation, but depending how you flooring/rounding is done, it might create the gaps. – Terje Jan 19 '16 at 13:34
  • There is no flooring/rounding. The hex sprites are given the coordinates without any further mathematical operation and the drawing function uses floats as well. Also the width of the gaps don't increase in any way that's noticeable. From what I can see checking the coordinates they also are appear recurring. – Askr Jan 19 '16 at 14:42
  • Did you try rounding off to 1 or 2 decimals? That should give you constant spacing at least. – Terje Jan 19 '16 at 14:44
  • Actually, rounding gives inconsistent spacing, as once the threshold is reached there are more pixels added/substracted to the offset. – Askr Jan 19 '16 at 14:46

2 Answers2

0

Have you tried simply decreasing HEX_WIDTH by 1?

private static final float HEX_WIDTH = 96;

Your Height is apparently fine because you have no horizontal gaps.

alamar
  • 18,729
  • 4
  • 64
  • 97
  • This only results in the individual hex tiles being smaller, [the gaps are still there](http://i.imgur.com/ESFdsjF.png). – Askr Jan 19 '16 at 13:25
0

Scaling the individual Sprites by 1.01f reduced the size of the gaps enough to consider this problem solved.

Askr
  • 57
  • 7