Search Unity

Tilemap Collider 2D Bug (Mesh not generating correctly, falling through floors/walls)

Discussion in '2D' started by Igotlazy, Feb 11, 2020.

  1. Igotlazy

    Igotlazy

    Joined:
    Jan 15, 2018
    Posts:
    65
    https://i.gyazo.com/544755c5ba2e988d89d7f67896a5d53b.mp4

    So I'm using Tilemap Collider 2Ds to define the levels of my 2D Platformer. For some reason however, in these very specific locations, the mesh doesn't generate properly and basically acts as though a wall/floor doesn't exist. This only happens if I transition into the scene that has the Tilemap Collider, not if I start the game in the scene (happens in build as well).

    In the clip I showed, every other wall in the level functions perfectly. It's only this wall. I have had this occur to me in a different scene with a floor segment. If I change the level geometry, the bug usually fixes itself.

    I am using Tilemap 2Ds with Composite Collider 2Ds and Static Rigidbodies. I have also set the gameobject itself to Static, and the geometry type on the Composite Collider to Polygons. The Tilemap colliders are quite large and complex, and are often disjointed as well.
     
  2. Tom-Atom

    Tom-Atom

    Joined:
    Jun 29, 2014
    Posts:
    153
  3. Igotlazy

    Igotlazy

    Joined:
    Jan 15, 2018
    Posts:
    65
  4. Tom-Atom

    Tom-Atom

    Joined:
    Jun 29, 2014
    Posts:
    153
    Hmmm... that's pitty.... In my level it works well now. But I remember, that previously small rearranging of tiles produced different collider with bugs in different places. So, I might be just lucky because of level layout.

    Best would be, if you could report a bug with minimal project attached (I used Unity Repo Project Wizard - https://github.com/Unity-Technologies/ReproProjectWizard to strip unnecessary things) and I also recorded detailed video for them. Better you describe it, higher the chance they will find it quickly...
     
  5. Igotlazy

    Igotlazy

    Joined:
    Jan 15, 2018
    Posts:
    65
    Perhaps the fix only applies to fresh Tilemaps/Composite colliders. The ones I'm using have existed in the project since 2019.2. I'll play around with it.
     
  6. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    11,499
    The original problem was that the (great) libtess2 library that the CompositeCollider2D uses was producing very small polygons with almost collinear sides and depending on the polygon itself, Box2Ds collision detection algorithms were not producing contacts. We knew this already and detected absolute collinear verts and even allow a small amount of wiggle room so "nearly" collinear but the original bug report shows verts that were outside of this but still Box2D thought they were colinear so we increased it slightly and used an addition metric of polygon area and this fixed all the reports we had where under a special configuration you would pass through the shape.

    Maybe you're creating another one. To be honest, it's a tricky balancing act.

    Whilst these cases are rare, the CompositeCollider2D can produce all sorts of weird configurations so it's why it's appearing there and not in the PolygonCollider2D which also uses libtess2.

    The way forword I think is to perform a better edge-merging for these cases but it can become non-trivial to do.

    We do have another report of this which we've accepted but if you do have a project you could share with the bug reported then please go ahead and created it and send me the case number and I'll see if it's the same.

    As a workaround, maybe you'll get better results if you were to use Outline mode (assuming you can) and/or change the Vertex Distance to smooth out the resultant shapes. As I say though, this is a workaround to keep you moving forward.
     
  7. Igotlazy

    Igotlazy

    Joined:
    Jan 15, 2018
    Posts:
    65
    Do you happen to have a link to the bug you've accepted, just so I can see if it's similar to mine (I'm assuming it's something on the Issue Tracker)?

    I've considered using Outlines, and they do seem to always generate proper geometry, but honestly I'm partially worried about using them. The player should never, ever be inside of these walls, and Outlines makes no attempt to depenetrate them once an object crosses the border. Is there anything to keep in mind/techniques to use with this geometry type in order to avoid cases where entities could find themselves inside the collider? Most Unity tutorials that use Tilemaps Colliders do set the geometry type to Outlines, so maybe I'm stressing for no reason.
     
  8. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    11,499
    I don't have a link for you as it's not public. Let's not assume it's the same thing as that happens all the time. Please submit a reproduction case and send me the case number.
     
  9. Tom-Atom

    Tom-Atom

    Joined:
    Jun 29, 2014
    Posts:
    153
    @MelvMay I originally reported this issue as bug under Case 1205703 with Unity 2019.2. It got rejected as duplicate to https://issuetracker.unity3d.com/is...-when-composite-polygon-type-collider-is-used

    Now, I continue in building levels (with 2019.3.5f1) and issue is still there. So, I created another bug report - it is Case 1227909. It has sample project attached to examine. You can read its description - so far most of the discussion was about incorrect normals. But as I wrote into bug description, I have problem with box/circle cast and when I tried RayCast I could get right collision. So, I am not sure whether it is problem of normals...?
     
  10. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    11,499
    The original case wasn't about performing queries but was found to be Box2D producing poor collision detection when some polygons were almost collinear. We increased the threshold slightly to remove the ones we encountered in the original case but increasing it too much results in other problems.

    It's not as simple as saying the original bug report wasn't fixed; it was, however there are edge cases for certain types of collision detection in Box2D which are not easy to fix. I am not sure what specific issue you're encountering in your new case but I'll wait until QA have gone through the case you submitted. It might be yet another case of something being near-collinear or just as easily, something else.

    In the end, Box2D doesn't know about any of the Unity colliders, it only sees Polygon, Capsule, Circle and Edge and we rely on it for casting and its ability to produce contacts and collision response. We have found that it can be sensitive to certain polygon set-ups and the CompositeCollider2D is unfortunately good at producing them by the very nature of what it does. Our "fixes" nearly always come in the form of filtering out stuff it doesn't like.

    We've been discussing switching to a different triangulation method such as Delaunay that will reduce these long thin polygons etc.
     
    Tom-Atom likes this.
  11. Tom-Atom

    Tom-Atom

    Joined:
    Jun 29, 2014
    Posts:
    153
    @MelvMay thanks for reply. As a workaround I switched composite colliders off for now (as I am working on level and its design changes, I tried to switch composite collider back for while and got the problem at another place...).
     
  12. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    11,499
    If you can afford the cost of continuous col-det or just use collision queries then you should not see the issue with outline mode which uses edge primitives. The added advantage is that this produces continuous edges unlike polygon mode.
     
  13. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    11,499
    So today I went through your bug report and sure enough, it's related to a collinear issue where your specific case was unfortunately only just beyond the threshold.

    I've gone ahead and fixed the issue by improving the collinearity check (scale invariant & larger threshold). This should ensure that the mesh that comes out of our polygon decomposition library is something that Box2D is happy with.

    The reason why this collinearity is appearing though is coming from the Tilemap and how it makes incremental changes to the tile mesh as you paint tiles. Without going into too much detail, it has to do various operations to ensure it can quickly and cleanly update the mesh when painting in the editor or tiles being modified at runtime. This is why causing a full recreate by disabling/enabling the TilemapCollider2D or modifying the CompositeCollider2D etc will change it and often remove the issue).

    Anyway, I'll get this fix in ASAP.

    EDIT: If you're interested, it might be worth me creating a custom build based upon 2019.3 so you can adequately test it out to ensure it fixes your problem is all scenarios. I need to figure out how to get your character moving too in the demo you provided. ;)
     
    Last edited: Mar 17, 2020
    Tom-Atom likes this.
  14. Tom-Atom

    Tom-Atom

    Joined:
    Jun 29, 2014
    Posts:
    153
    Thanks for help!
    Do you need access to our full project for it? Or need any additional information (character is controlled with left/right arrow. Jumping is mapped to Z key, attack to X key and spell to S key. With down key you can fall through thin platforms)?
     
  15. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    11,499
    No, the level you supplied demonstrates the issue well.

    I've sent you a DM with more information on the custom build.
     
    Last edited: Mar 18, 2020
  16. Igotlazy

    Igotlazy

    Joined:
    Jan 15, 2018
    Posts:
    65
    Oh good to know it's been fixed. Would you happen to know when it will be officially released?
     
  17. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    11,499
    A fix has to go through a lot of processes so unfortunately not. We’re still working on verifying that it does indeed fix all output from the composite. We do know what the issue is though so it’s being resolved.
     
  18. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    11,499
    Whilst I cannot tell you when it'll land, I have today pushed a PR with a fix. This actually solves a number of related issues which manifest as unstable collision-detection (objects never quite settling), physics queries not detecting edges and in extreme cases (when using the CompositeCollider2D which produces much more varied and complex set-ups), geometry missing.