Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Waypoint · Apr 23, 2013 at 03:55 AM · c#2dprocedural generationhexagon

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.

alt text

 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;
     }
 }



2hex.jpg (43.6 kB)
Comment
Add comment · Show 4
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image whydoidoit · Apr 23, 2013 at 06:17 AM 0
Share

Need to see your hex drawing code.

avatar image Waypoint · Apr 23, 2013 at 06:38 AM 0
Share

Excised -- content is now in OP

avatar image whydoidoit · Apr 23, 2013 at 07:26 AM 0
Share

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.

avatar image Waypoint · Apr 23, 2013 at 10:37 PM 0
Share

Hex Drawing code is now in. Thank you for looking at this.

1 Reply

· Add your reply
  • Sort: 
avatar image
0
Best Answer

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;
 }
Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image whebert · Apr 24, 2013 at 12:18 AM 0
Share

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.

avatar image Waypoint · Apr 24, 2013 at 01:14 AM 0
Share

Thanks for pointing this out!

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

14 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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

Best way to go about top down procedural 2d city 0 Answers

How to make object explode upon collision 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges