- Home /
Physics2D Mesh Collider?
Hey all,
I've been experimenting with procedural generation and creating meshes from code. I've had a lot of success with this in 3D by creating chunks of blocks (think Minecraft) and creating these meshes by defining vertices and triangles in code. For the collision I simply used a mesh collider with the same mesh I created and it works great.
My problem is bringing this to a 2D world.
So far I have created a mesh using points on the x and y axis. My problem is creating a 2d collider for this mesh. How would I go about getting this mesh to have collision? Would I need to create an edge collider and define the points to my vertices?
Here is an image of my generated mesh:
Thank you
EDIT: I've come up with the solution of adding a 2D Box Collider to every block position. A box collider is not added if the block is completely surrounded. This solution works and as this is only a small game with small chunk sizes it should be suitable. Is there a more efficient way? I imagine the most efficient way would be creating polygon outline of this mesh like Tiled2Unity does but I wouldn't even know where to start creating an algorithm like that.
Answer by tanoshimi · Jul 14, 2016 at 06:25 AM
What you're looking for is to create a Polygon Collider for each contiguous set of blocks, passing the vertices of the corners of that shape to the points
property. This will be more efficient than creating many box colliders, which creates more collision objects to test, and also contains redundant edges that run through the middle of blocks.
We can't help you work out the vertices at the corners since we don't know the method you're using to place the blocks, but it shouldn't be too hard to iterate through a 2d array and determine each of the separate contiguous shapes it contains.
Your answer
Follow this Question
Related Questions
How to remove gaps in a 2D map. 0 Answers
Unity2D collision problem 3 Answers
Box Collider 2d Not Colliding 0 Answers
How to calculate angle between 2D objects and use the result? 0 Answers