- Home /
Hexagon Grid
Hello,
I've built a hexagon grid using some coordinates that I input into the game. However, the algorithm I'm using to place these doesn't appear to be how I want it.
This is how it places them currently:
I would like the X axis to be horizontal, however it currently goes diagonal lower-right. This is how I would like it to be:
As you can see, the X axis is horizontal, with every odd on a negative placement (1,0.. 3,0.. 5,0.. etc).
This is the algorithm I'm using:
public static Vector3 HexCoordinateToWorld(Vector2 _coordinates){
float _sqrt32 = (float)Mathf.Sqrt(3)/2;
float _size = 40.24f;
float _x = _size * (float)_coordinates.x * 1.5f;
float _y = _size * (2 * _coordinates.y - _coordinates.x) * _sqrt32;
return new Vector3(_x, 0, _y);
}
Hi, there is a very interesting article about hex grids here: http://www.gamasutra.com/blogs/HermanTulleken/20140912/225495/20_Fun_Grid_Facts_Hex_Grids.php It contains a link to a document (h)exploring the subject: http://www.gamelogic.co.za/downloads/Hex$$anonymous$$ath2.pdf
Answer by ASPePeX · Feb 01, 2017 at 03:44 PM
Build it like it was a rectangular grid and then offset every odd row.
The Y calculation would be looking something like this:
if (_coordinates.x % 2 == 0)
{
//don't offset Y position
}
else
{
//offset Y position
}
This is correct, but to expand on the concept, you ought to be able to boil it down to one line, like so:
float _y = _size * (2 * _coordinates.y + ((_coordinates.x % 2 == 1) : 1 : 0)) * _sqrt32;