Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save general-games/bef218f80069141860fa26e8efc3e2ae to your computer and use it in GitHub Desktop.
Save general-games/bef218f80069141860fa26e8efc3e2ae to your computer and use it in GitHub Desktop.
Pixel art friendlt hex dimensions
using System;
using Gridr.Datastructures;
using UnityEngine;
//Created by Carl Hinas | https://www.generalgames.org
//https://twitter.com/generalgames9
namespace Gridr.Utils
{
public static class HexUtil2D
{
private const float widthOffset = 0.13333f;
private const float heightOffset = 0.0f;
public static GridPosition GetGridPosition( Vector3 origin, Vector3 position, float innerRadius, float outerRadius, int gridWidth, int gridHeight)
{
var cubeCoords = CubeCoordsFromWorldPosition(origin, position, innerRadius, outerRadius);
var index = GetIndex(cubeCoords, gridWidth, gridHeight);
return new GridPosition(cubeCoords, index);
}
public static float GetInnerRadius(float outerRadius)
{
return (outerRadius * .5f) * (float) Math.Sqrt(3f);
}
public static int GetIndex(Vector3Int cubeCoords, int gridWidth, int gridHeight)
{
var offset = CubeCoordsToOffset(cubeCoords);
var index = offset.y * gridWidth + offset.x;
return index;
}
public static Vector3[] GetCorners(Vector3 origin, float innerRadius, float outerRadius)
{
var corners = new Vector3[]
{
new Vector3(0, outerRadius, 0) + origin,
new Vector3(innerRadius+widthOffset, .5f * outerRadius, 0) + origin,
new Vector3(innerRadius+widthOffset, -.5f * outerRadius, 0) + origin,
new Vector3(0, -outerRadius, 0) + origin,
new Vector3(-innerRadius-widthOffset, -.5f * outerRadius, 0) + origin,
new Vector3(-innerRadius-widthOffset, .5f * outerRadius, 0) + origin
};
return corners;
}
public static Vector3 GetWorldPositionEven(Vector3 hexPosition, float innerRadius, float outerRadius)
{
float xPos = (hexPosition.x + hexPosition.y * .5f - hexPosition.y / 2) * ((innerRadius+widthOffset) * 2);
float yPos = hexPosition.y * (outerRadius * 1.5f);
float zPos = 0f;
return new Vector3(xPos, yPos, zPos);
}
public static Vector3 GetWorldPositionOdd(Vector3 hexPosition, float innerRadius, float outerRadius)
{
float xPos = (hexPosition.x + hexPosition.y * .5f - hexPosition.y / 2) * ((innerRadius+widthOffset) * 2) + innerRadius+widthOffset;
float yPos = hexPosition.y * (outerRadius * 1.5f);
float zPos = 0f;
return new Vector3(xPos, yPos, zPos);
}
public static Vector3Int CubeCoordsFromWorldPosition(Vector3 origin, Vector3 position, float innerRadius, float outerRadius)
{
float x = position.x / ((innerRadius+widthOffset) * 2f) - origin.x;
float y = -x;
float offset = position.y / (outerRadius * 3) - origin.z * .5f;
x -= offset;
y -= offset;
int ix = Mathf.RoundToInt(x);
int iy = Mathf.RoundToInt(y);
int iz = Mathf.RoundToInt(-x - y);
if (ix + iy + iz != 0)
{
float dx = Mathf.Abs(x - ix);
float dy = Mathf.Abs(y - iy);
float dz = Mathf.Abs(-x - y - iz);
if (dx > dy && dx > dz)
ix = -iy - iz;
else if (dz > dy)
iz = -ix - iy;
}
return new Vector3Int(ix, iy, iz);
}
public static Vector3Int CubeCoordsToOffset(Vector3Int cubeCoords)
{
var col = cubeCoords.x + (cubeCoords.z - (cubeCoords.z & 1)) / 2;
var row = cubeCoords.z;
return new Vector3Int(col, row, 0);
}
public static bool IsWithinOffsetGrid(Vector3Int cubeCoords, int width, int height)
{
var offsetCoords = CubeCoordsToOffset(cubeCoords);
return offsetCoords.x >= 0 && offsetCoords.x < width && offsetCoords.y >= 0 && offsetCoords.y < height;
}
public static Vector3 CalculateHexCenterFromGridCoords(int x, int y, Vector3 origin, float innerRadius, float outerRadius)
{
Vector3 worldPosition;
if (y % 2 == 0)
{
worldPosition = new Vector3() + origin + new Vector3(x * ((innerRadius+widthOffset) * 2f), 0, y * (outerRadius + (outerRadius / 2f)));
}
else
{
worldPosition = new Vector3() + origin + new Vector3(x * ((innerRadius+widthOffset) * 2f) + (innerRadius+widthOffset), 0, y * (outerRadius + (outerRadius / 2f)));
}
return worldPosition;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment