0

Okay guys, I am having a LOT trouble with this. I simply cannot figure out a way to implement tile picking in a hexagonal map in XNA. I have looked this up prior to asking this question, and all the answers involve complicated algorithms and diagrams my puny mind simply cannot comprehend. So my question for you guys is: How would i be able to hover over tiles, and select them if i wanted to?

If you need any reference as to how my program looks so far, just check out this link, its literally the same except i have a smaller map on mine.

http://www.xnaresources.com/default.asp?page=Tutorial:TileEngineSeries:3

Thanks!

1 Answers1

0

This is code i had stored but never used and its for hex grid where one edge is looking up, so by some minor tweaks it could work in your example. It's not my code, not sure who wrote it.

Hexagon[][] hexagons = new Hexagon[100][100];
double hexagonHeight = 30;
double hexagonWidth = 40;
double halfWidth = hexagonWidth / 2;


// Find rough coordinates of Hexagon at mousepoint
private Hexagon getSelectedHexagon(MouseEvent mouse)
    {
        // These will represent which box the mouse is in, not which hexagon!
        int row = (int) (mouse.y / hexagonHeight);
        int column;

        boolean rowIsOdd = row % 2 != 0;

        // Is the row an even number?
        if (rowIsOdd) // No: Calculate normally
            column = (int) (mouse.x / hexagonWidth);
        else // Yes: Offset mouse.x to match the offset of the row
            column = (int) ((mouse.x + halfWidth) / hexagonWidth);

        // column is more complex because it has to
        // take into account that every other row
        // is offset by half the width of a hexagon

        return hexagons[row][column];
    }

edit: i just found author Hexagonal Grids, how do you find which hexagon a point is in?

Community
  • 1
  • 1
Davor Mlinaric
  • 1,989
  • 1
  • 19
  • 26
  • Sorry, but since im quite new to XNA and C# in genereal, where would this code go, and what variable names/values would i have to change in order for this to work? – user3496292 Apr 11 '14 at 12:08