PolygonCollider2D generates hole

This specific polygon collider 2d is generating a hole in its geometry and I don’t know why. The hole is that triangle in the very center there.

Here is a video demonstrating the hole. You can see a message indicating the square has entered the collider, and then a message that it left when it moves into the triangle in the center.

Here you can see I moved one point and that triangle in the center goes away.

I’m using unity 2019.4.28f1. I’m generating the polygoncollider2d paths procedurally based on meshes exported by 3ds max using this code:

The square is just a sprite with a box collider2d and rigidbody2d set to kinematic, while the polygon collider has it’s isTrigger flag set. It doesn’t matter if the object is moved into the polygon collider or the polygon collider is moved over the object. I’ve included a zip of the polygon collider along with my script to generate the debug messages. I’ll also include the actual points and paths of the collider pulled from the prefab at the end of the post. An important note is that it seems to generate different holes if the PolygonCollider2D’s GameObject’s transform is rotated differently.

If you have alternative solutions to generating a polygoncollider2d from a mesh filter or have any ideas about how or why this is happening, let me know! Thanks :slight_smile:
EDIT: Sorry, the videos were set to private, fixed.

  m_Points:
    m_Paths:
    - - {x: 144, y: -400.00003}
      - {x: 144, y: 143.99994}
      - {x: -336, y: 143.99994}
      - {x: -336, y: -400.00003}
    - - {x: -384, y: 175.99994}
      - {x: -384, y: 47.99997}
      - {x: -336, y: 47.99997}
      - {x: -336, y: 175.99994}
    - - {x: -336, y: 207.99994}
      - {x: -336, y: 143.99994}
      - {x: -144, y: 143.99994}
      - {x: -144, y: 207.99994}
    - - {x: -432, y: 15.999969}
      - {x: -432, y: -80.00003}
      - {x: -336, y: -80.00003}
      - {x: -336, y: 15.999969}
    - - {x: -144, y: 303.99994}
      - {x: -144, y: 143.99994}
      - {x: 384, y: 143.99994}
      - {x: 384, y: 303.99994}
    - - {x: -96, y: 399.99994}
      - {x: -96, y: 303.99994}
      - {x: 288, y: 303.99994}
      - {x: 288, y: 399.99994}
    - - {x: 144, y: 143.99994}
      - {x: 144, y: -48.00003}
      - {x: 384, y: -48.00003}
      - {x: 384, y: 143.99994}
    - - {x: 384, y: 303.99994}
      - {x: 384, y: 143.99994}
      - {x: 432, y: 143.99994}
      - {x: 432, y: 271.99994}
      - {x: 408, y: 271.99994}
      - {x: 408, y: 303.99994}
    - - {x: 384, y: 143.99994}
      - {x: 384, y: 111.99997}
      - {x: 408, y: 111.99997}
      - {x: 408, y: 143.99994}

7955080–1019095–problem collider.zip (1.38 KB)

In the end, a hole is created only for two reasons:

  • The area isn’t covered by the paths
  • Opposite winding means a hole

So if you do CW path for a square outline then a CCW path for a smaller square inside it, the smaller square will be a hole.

Do you think the warning it produces has some relevance? :slight_smile:

In your data you’ve likely got vertex way too close and/or redundant, colinear verts. All the PolygonCollider2D can do is stop your data from causing a crash because of invalid data. In the end, it has to produce convex polygons which the 2D physics engine can consume. This isn’t for rendering so instead of rendering artifacts you get physics artifacts (missing polygons).

If it finds invalid things in the data, it has to remove it. It provides a warning about this and this results in shapes being removed etc. Vertex too close or collinear, stuff like that.

Also, these things can be on the border of being valid so when you perform some transformation on the data such as you’re doing here (rotating on the Y axis by 180-deg) then due to floating-point precision and changing of windings, it can give you bad results.

Using your data, with no rotation it’s okay but with 180-deg rotation on the Y, some points are invalid.

Using a scale of 10 on the X axis to force the vertices to be further apart then rotating on the Y axis removes the issue so it’s likely because you have vertices that are way too close. Those should be merged. Even a scale of 5 clears it up. Less than that though and it doesn’t.

It seems to be related to elements (paths) 4 and possibly 6 because you can modify these and watch the warning go away.

It’s best to clean up the mesh but you could also try adding a CompositeCollider2D in Polygon geometry mode and setting the PolygonCollider2D to use it. That provides things like Vertex Distance and Offset Distance which can help merge/clean-up bad vertices. I did this on your data and left Vertex Distance at default and set Offset Distance to 0.

3 Likes