- Home /
Gap appears between two 2D hexagons sharing two vertices
Here are two scripts. Map is attached to a plane and instantiates objects of type Hex. The Hex.ToString() function prints vertices in (x,z) format because y is constant. Here is the result of the Hex.ToString() operations in advance:
Center: (0,0)
v1: (-6,0)
v2: (-3,5)
v3: (3,5)
v4: (6,0)
v5: (3,-5)
v6: (-3,-5)
Center: (-9,5)
v1: (-15,5)
v2: (-12,10)
v3: (-6,10)
v4: (-3,5)
v5: (-6,0)
v6: (-12,0)
The first hexagon's vertex v1 = the second hexagon's vertex v5 = (-6,0)
The first hexagon's vertex v2 = the second hexagon's vertex v4 = (-3,5)
The hexagons should overlap at a line drawn between the two shared vertices, however, they are spaced out. The size of these gaps also seems to scale with the radius of the hexagon.
public class Map : MonoBehaviour
{
private int radius = 6;
void Awake()
{
Texture2D hexTexture = (Texture2D) Resources.Load ( "Hex" );
Hex center = (Hex) this.gameObject.AddComponent ( "Hex" );
center.Draw ( radius, 0, 0, hexTexture );
Hex neighbor = (Hex) this.gameObject.AddComponent ( "Hex" );
neighbor.Draw ( radius, -9, 5, hexTexture );
}
}
public class Hex : MonoBehaviour
{
private const float y = 0.01f;
public void Draw( int r, float x, float z, Texture2D hexTexture )
{
gO = new GameObject();
gO.transform.position = new Vector3( x, y, z );
gO.name = ( "Hex (" + x.ToString () + "," + z.ToString () + ")" );
Vector3[] vertices = ComputeVertices( r, x, z );
hexInfo = new HexInfo( r, x, z, vertices );
Vector2 uvCenter = new Vector2( .5f, .5f );
Vector2 uvV1 = new Vector2( 0, .5f );
Vector2 uvV2 = new Vector2( .333f, 0 );
Vector2 uvV3 = new Vector2( .666f, 0 );
Vector2 uvV4 = new Vector2( 1, .5f );
Vector2 uvV5 = new Vector2( .666f, 1 );
Vector2 uvV6 = new Vector2( .333f, 1 );
Vector2[] uv = { uvCenter, uvV1, uvV2, uvV3, uvV4, uvV5, uvV6 };
int[] indeces = {
6, 0, 5,
5, 0, 4,
4, 0, 3,
3, 0, 2,
2, 0, 1,
1, 0, 6,
};
Vector3[] normals = {
Vector3.up,
Vector3.up,
Vector3.up,
Vector3.up,
Vector3.up,
Vector3.up,
Vector3.up
};
Mesh mesh = new Mesh();
mesh.vertices = vertices;
mesh.triangles = indeces;
mesh.uv = uv;
mesh.normals = normals;
mesh.Optimize();
MeshFilter meshFilter = (MeshFilter) gO.AddComponent( "MeshFilter" );
meshFilter.mesh = mesh;
MeshRenderer meshRenderer = (MeshRenderer) gO.AddComponent( "MeshRenderer" );
meshRenderer.material.mainTexture = hexTexture;
Debug.Log ( "Drew Hexagon: " + this.ToString () );
}
// v2 -- v3
// / \
// / \
// v1 C v4
// \ /
// \ /
// v6 -- v5
// v2
// /|\
// c / | \
// / |b \
// v1 <-------Center
// a
//
// All triangles in the hexagon have the properties of
// triangle (v1,v2,center), where a = c = radius
//
// Angle ab is a right angle whose line b bisects line a:
// The x coordinate of v1 is the center's x coordinate - radius
// The x coordinate of v4 is the center's x coordinate + radius
// The x coordinate of v2 and v6 is the center's x value - 1/2 radius
// The x coordinate of v3 and v5 is the center's x value + 1/2 radius
//
// line b's length is 1/2 of the hexagon's height H:
// The z coordinate of v1 and v4 is the center's z coordinate
// The z coordinate of v2 and v3 is the center's z value + b
// The z coordinate of v6 and v5 is the center's z value - b
private Vector3[] ComputeVertices( int r, float x, float z )
{
// Cast b to an integer to force rounding
int b = (int) HexInfo.GetHeight ( r ) / 2;
float halfRadius = r / 2;
Vector3 center = new Vector3( x, y, z );
Vector3 v1 = new Vector3( x - r, y, z );
Vector3 v4 = new Vector3( x + r, y, z );
Vector3 v2 = new Vector3( x - halfRadius, y, z + b );
Vector3 v3 = new Vector3( x + halfRadius, y, z + b );
Vector3 v5 = new Vector3( x + halfRadius, y, z - b );
Vector3 v6 = new Vector3( x - halfRadius, y, z - b );
Vector3[] vertices = { center, v1, v2, v3, v4, v5, v6 };
return vertices;
}
}
I'm betting that math you didn't post uses x,z - given you are passing values for x,z to the second hexagon I'm guessing something in the code that you've omitted it making the 2nd hex far from the first one. Without seeing it, this is of course a guess.
Hex Drawing code is now in. Thank you for looking at this.
Answer by whebert · Apr 24, 2013 at 12:14 AM
The reason there is a gap is that you are using the object's center coordinates (x,z) as part of it's vertex coordinates, and then you are also positioning it. So the second Hex gets positioned at (-9,.01,5), but it's actual center coordinate is (-9,.01,5) relative to that, so the center coordinate is in world space at (-18, .02, 10).
Your vertices should be centered around your "pivot" point for each Hex, and should be the same for each Hex - since you place them with .position.
To fix, just change your ComputeVertices to:
private Vector3[] ComputeVertices( int r, float x, float z )
{
// Cast b to an integer to force rounding
int b = (int) HexInfo.GetHeight ( r ) / 2;
float halfRadius = r / 2;
Vector3 center = new Vector3( 0, 0, 0 );
Vector3 v1 = new Vector3( - r, 0, 0 );
Vector3 v4 = new Vector3( r, 0, 0 );
Vector3 v2 = new Vector3( - halfRadius, 0, b );
Vector3 v3 = new Vector3( halfRadius, 0, b );
Vector3 v5 = new Vector3( halfRadius, 0, - b );
Vector3 v6 = new Vector3( - halfRadius, 0, - b );
Vector3[] vertices = { center, v1, v2, v3, v4, v5, v6 };
return vertices;
}
Of course, you don't need the x and z parameters to build the vertices, just left em in since I didn't want to modify your method signature.
Your answer
![](https://koobas.hobune.stream/wayback/20220613100841im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
How to darken a procedurally added texture (explained below) 0 Answers