- Home /
Instantiating Hexagonal Cube coordinate help
Hey there, new to unity and C#. I have managed to create a rectangular hex grid but I need to create a hexagonal map of hexagons. Creating a rectangular grid starts in the bottom right corner, where a hexagonal shaped map starts at 0,0,0 which is what I am aiming for. Using Redblobgames. Looking at the pseudo code for hexagonal shape:
  unordered_set<Hex> map;
 for (int q = -map_radius; q <= map_radius; q++) {
     int r1 = max(-map_radius, -q - map_radius);
     int r2 = min(map_radius, -q + map_radius);
     for (int r = r1; r <= r2; r++) {
         map.insert(Hex(q, r, -q-r));
     }
 }
I'm having a hard time understanding what to do with
  int r1 = max(-map_radius, -q - map_radius);
  int r2 = min(map_radius, -q + map_radius);
It doesn't appear to be a method, but more like a Vector2, but I had troubles implementing that.
my current code:
 public class CubeHexGrid : MonoBehaviour {
 
 
     public GameObject HexPrefab;
     public int x = 5;
     public int y = 5;
 
     private float offsetX, offsetY;
 
     // Use this for initialization
     void Start()
     {
         Vector3 bounds = HexPrefab.GetComponent<MeshRenderer>().bounds.extents;
         float hexWidth = bounds.x * 2;
         float horzOffset = hexWidth * (3 / 4);
         float sqrt = Mathf.Sqrt(3) / 2;
         float hexHeight = sqrt * hexWidth;
         float vertOffset = hexHeight;
 
 
         offsetX = hexHeight * Mathf.Sqrt(3);
         offsetY = hexHeight * .5f;
 
 
         for (int i = 0; i < x; i++)
         {
             for (int j = 0; j < y; j++)
             {
                 Vector2 hexpos = HexOffset(i, j);
                 Vector3 pos = new Vector3(hexpos.x, hexpos.y, 0);
 
                 Instantiate(HexPrefab, pos, Quaternion.identity);
             }
         }
     }      
 
     Vector2 HexOffset(int x, int y)
     {
         Vector2 position = Vector2.zero;
 
         if (y % 2 == 0)
         {
             position.x = x * offsetX;
             position.y = y * offsetY;
         }
         else
         {
             position.x = (x + 0.5f) * offsetX;
             position.y = y * offsetY;
         }
         return position;
     }
 }
Any thoughts on that first bit of pseudocode to help figure out what it means?
Answer by craftymaniac · Oct 05, 2016 at 05:26 AM
I've found the answer to this question.
  for (int q = -map_radius; q <= map_radius; q++) {
                 int r1 = Math.Max(-map_radius, -q - map_radius);
                 int r2 = Math.Min(map_radius, -q + map_radius);
                 for (int r = r1; r <= r2; r++) {
                     
                     
                     GameObject  myObject = Instantiate(hexPrefab, new Vector3(q,r,-q-r), Quaternion.identity)as GameObject;
                     myObject.transform.parent = transform;
                 }
             }
But now I am struggling to work with the lib that is provided on redblobgames libraries
I have copied the structs in and got rid of the errors by adding using System.Collections.Generic; which allowed for Math instead of Mathf(which doesn't work if you swap it out)
I'm not really sure how to use a struct like struct Point in the provided lib on redblobgames
 struct Point
 {
     public Point(double x, double y)
     {
         this.x = x;
         this.y = y;
     }
     public readonly double x;
     public readonly double y;
 }
Your answer
 
 
             Follow this Question
Related Questions
How can customize which prefabs are instantiated in a grid? 1 Answer
Hexagonal grid with 120 degree angles 1 Answer
Ground Plane when starting new project 2 Answers
Hexagonal grid 1 Answer
 koobas.hobune.stream
koobas.hobune.stream 
                       
                
                       
			     
			 
                