Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
2 captures
12 Jun 22 - 14 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 Starleg2 · May 28, 2018 at 11:21 PM · meshpolygon collider 2drandom gen

How to make a 2D polygon collider from a mesh

I have a grid of Tiles that are under one chunk gameobject and each tile is a small square mesh. I was wondering how to generate a 2d polygon collider from this 3d mesh on only the dark tiles. I can separate the dark and light tiles into 2 meshes if need be.

alt text

unitycollisionissue.png (217.9 kB)
Comment
Add comment
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

1 Reply

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

Answer by Starleg2 · Jun 01, 2018 at 04:18 PM

After spending a huge amount of time, I finally found the solution to the collide generationalt text

     void MakeCollisions()
     {
         List<Edge> edges = new List<Edge>();
         edges = GetEdges(meshCollision.triangles);
         outsideEdges = FindBoundary(edges, meshCollision.vertices);
 
         sortedOutsideEdges = SortEdges(outsideEdges);
         print(sortedOutsideEdges.Count);
         collisionPoints = convertEdgePathsToVector2Arrays(sortedOutsideEdges);
         PC2D.pathCount = collisionPoints.Length;
 
         for (int i = 0; i < collisionPoints.Length; i++)
         {
             PC2D.SetPath(i, collisionPoints[i]);
         }
 
 
     }
 
     public struct Edge
     {
         public int v1;
         public int v2;
         public int triangleIndex;
         public Edge(int aV1, int aV2, int aIndex)
         {
             v1 = aV1;
             v2 = aV2;
             triangleIndex = aIndex;
         }
     }
 
     public static List<Edge> GetEdges(int[] aIndices)
     {
         List<Edge> result = new List<Edge>();
         for (int i = 0; i < aIndices.Length; i += 3)
         {
             int v1 = aIndices[i];
             int v2 = aIndices[i + 1];
             int v3 = aIndices[i + 2];
             result.Add(new Edge(v1, v2, i));
             result.Add(new Edge(v2, v3, i));
             result.Add(new Edge(v3, v1, i));
         }
         return result;
     }
 
     public static List<Edge> FindBoundary(List<Edge> aEdges, Vector3[] verts)
     {
         List<Edge> result = new List<Edge>(aEdges);
         for (int i = result.Count - 1; i > 0; i--)
         {
             for (int n = i - 1; n >= 0; n--)
             {
                 if (verts[result[i].v1] == verts[result[n].v2] && verts[result[i].v2] == verts[result[n].v1])
                 {
                     result.RemoveAt(i);
                     result.RemoveAt(n);
                     i--;
                     break;
                 }
             }
         }
         return result;
     }
 
     public List<List<Edge>> SortEdges(List<Edge> bEdges)
     {
         List<Edge> borderEdges = bEdges;
 
         List<List<Edge>> paths = new List<List<Edge>>();
         List<Edge> currentPath = new List<Edge>();
 
         while (borderEdges.Count > 0)
         {
             currentPath.Add(borderEdges[0]);
             borderEdges.RemoveAt(0);
             makePath(currentPath, borderEdges);
             paths.Add(currentPath);
             currentPath = new List<Edge>();
             print(borderEdges.Count);
         }
         return paths;
     }
 
     public void makePath(List<Edge> currentPath, List<Edge> borderEdges)
     {
         bool continuePath = true;
 
         List<Vector3> verts = VerticiesCollide;
 
         while (continuePath)
         {
             for (int i = 0; i < borderEdges.Count; i++)
             {
                 if (verts[currentPath[currentPath.Count - 1].v2] == verts[borderEdges[i].v1] || verts[currentPath[currentPath.Count - 1].v1] == verts[borderEdges[i].v2])
                 {
                     currentPath.Add(borderEdges[i]);
                     borderEdges.RemoveAt(i);
                     continuePath = true;
                     break;
                 }
                 else
                 {
                     continuePath = false;
                 }
             }
             if (borderEdges.Count == 0)
             {
                 continuePath = false;
             }
         }
     }
 
     public Vector2[][] convertEdgePathsToVector2Arrays(List<List<Edge>> edgePaths)
     {
         Vector2[][] collisionPoints = new Vector2[edgePaths.Count][];
 
         for (int i = 0; i < edgePaths.Count; i++)
         {
             collisionPoints[i] = new Vector2[edgePaths[i].Count];
             for (int j = 0; j < edgePaths[i].Count; j++)
             {
                 collisionPoints[i][j] = VerticiesCollide[edgePaths[i][j].v1];
             }
         }
         return collisionPoints;
     }

Below all this code is about 350 lines commented out. Anyway if it doesn't work for you just message me


fix.png (163.6 kB)
Comment
Add comment · Show 5 · 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 Starleg2 · Jun 01, 2018 at 04:19 PM 0
Share

the answer involved using edges ins$$anonymous$$d of whole tiles and finding the boundary, then ordering the bounding edges, and finally turning the edges into points!

avatar image nmill99 · Jun 11, 2020 at 07:47 AM 0
Share

I know this is long shot since this is so old but what values are stored in VerticiesCollide?

avatar image Starleg2 nmill99 · Jun 11, 2020 at 06:31 PM 0
Share

edgePaths is a list of a list of Edge structs. Edge.v1 is the index of a vertex located in the VerticiesCollide array. Since collisionPoints is an 2D array of vector2s VerticesCollide must be an array of vector2s

avatar image CaseyHofland Starleg2 · Jul 26, 2020 at 07:38 PM 0
Share

That doesn't really make sense with this line: List<Vector3> verts = VerticiesCollide; If you are still there, could you post the full source code?

avatar image P10tr3k · Mar 28 at 02:40 PM 0
Share

Works great! VerticiesCollide are just all vertices from mesh.

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

96 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 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 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 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 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 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 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

How do I use a 2D polygon collider to generate a correct collider for my randomly generated mesh? 0 Answers

Efficient generation of 2d polygon collider 0 Answers

Find edge vertices 0 Answers

Getting mesh length (x,z) 1 Answer

Mesh dissapearing from Resources 0 Answers


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