AnnouncementsFunnyVideosMusicAncapsTechnologyEconomicsPrivacyGIFSCringeAnarchyFilmPicsThemesIdeas4MatrixAskMatrixHelpTop Subs
2

HOLY SHIT - I HATE MATH. That being said, i love math, its just hexes. i hate hexes.

public void GenerateChunks() { int N = chunkGridRadius; // Number of chunks from center to edge

for (int chunkQ = -N; chunkQ <= N; chunkQ++)
{
    int chunkR = 0; // Fixed R-axis for a line of chunks
    GenerateChunk(chunkQ, chunkR, chunkSize);
}

Debug.Log("Chunks generated.");

}

public void GenerateChunk(int chunkQ, int chunkR, int K) { int radius = (K - 1) / 2;

  // Calculate the size of a single hex
  float hexWidth = hexSize * Mathf.Sqrt(3f);
  float hexHeight = hexSize * 1.5f;

  // Calculate the size of the chunk in world units
  float chunkWidth = hexWidth * (2 * radius + 1);
  float chunkHeight = hexHeight * (2 * radius + 1);

  // Calculate the chunk's initial world position
  float chunkX = chunkWidth * (chunkQ * 0.75f);
  float chunkZ = chunkHeight * (chunkR + chunkQ / 2f);

  Vector3 chunkWorldPos = new Vector3(chunkX, 0, chunkZ);


  // Create a new GameObject for the chunk
  GameObject chunkObj = new GameObject($"Chunk_{chunkQ}_{chunkR}");
  chunkObj.transform.parent = this.transform;
  chunkObj.transform.position = chunkWorldPos; // Set the chunk's position

  // Add ChunkData component and assign a random color
  ChunkData chunkData = chunkObj.AddComponent<ChunkData>();
  Color chunkColor = Random.ColorHSV();
  chunkData.chunkColor = chunkColor;

  // Generate hexes within the chunk
  for (int q = -radius; q <= radius; q++)
  {
      int r1 = Mathf.Max(-radius, -q - radius);
      int r2 = Mathf.Min(radius, -q + radius);
      for (int r = r1; r <= r2; r++)
      {
          // Calculate the local position of the hex within the chunk
          Vector3 hexLocalPos = HexCoordinateConverter.AxialToWorld(q, r, hexSize, isPointyTopped);

          // Instantiate hex prefab at the correct position relative to the chunk
          GameObject hexObj = Instantiate(hexPrefab, chunkObj.transform);
          hexObj.transform.localPosition = hexLocalPos;

          // Rotate hex if needed
          if (hexPrefabNeedsRotation && isPointyTopped)
          {
              hexObj.transform.Rotate(0, 30, 0);
          }

          // Set the color
          SpriteRenderer[] renderers = hexObj.GetComponentsInChildren<SpriteRenderer>();
          foreach (var renderer in renderers)
          {
              renderer.color = chunkColor;
          }

          // Calculate global axial coordinates of the hex
          int globalQ = chunkQ * K + q;
          int globalR = chunkR * K + r;

          // Create a label for the hex
          CreateHexLabel(Vector3.zero, globalQ, globalR, hexObj.transform);
      }
  }

  // Create a label for the chunk at local position (0, 0, 0)
  CreateChunkLabel(Vector3.zero, chunkQ, chunkR, chunkObj.transform);
  DrawChunkBoundaries(chunkQ, chunkR, K, chunkObj.transform);

}

public static Vector3 AxialToWorld(int q, int r, float hexSize, bool isPointyTopped) { float x, z;

 if (isPointyTopped)
 {
     x = hexSize * Mathf.Sqrt(3f) * (q + r / 2f);
     z = hexSize * 1.5f * r;
 }
 else
 {
     x = hexSize * 1.5f * q;
     z = hexSize * Mathf.Sqrt(3f) * (r + q / 2f);
 }

 return new Vector3(x, 0, z);

}

i have spent 2 days trying to get a fractal hex grid going - fuck i am frustrated. AxialToWorld is fine, that just converts QR to XYZ. GenerateChunk creates a chunk of hexes, as you would expect from the name. Generate chunks currently makes a line of chunks. The issue is as shown here. https://files.catbox.moe/ug3dz1.png. its as if the hex chunks are being generated without regards to the offset mandated by the grid itself. https://files.catbox.moe/wv0kpy.png here it is with a manual correction. I admit that my grasp on math is weak, and there are some dumb ass mistakes i make, but im sooo close and yet not there. https://www.redblobgames.com/grids/hexagons/ is what i am using as a reference. This is for chunking in my game BitRot and will let users explore an infinite world without as many concerns for having thing get to big for the computer to handle. Thank you for your time!

Comment preview