Search Unity

PolygonCollider2D incorrectly calculates shape

Discussion in 'Physics' started by dotsquid, Sep 2, 2020.

  1. dotsquid

    dotsquid

    Joined:
    Aug 11, 2016
    Posts:
    224
    Hi @MelvMay
    I ran into a bug (as I believe) with PolygonCollider2D. In some cases it incorrectly (as I believe) calculates its shape by given points. However, if the same collider has a non-zero offset, the problem is gone.
    I had created a bug report https://fogbugz.unity3d.com/default.asp?1274172_2bqv4chjkj77h4ie but it didn't get through your QA. I strongly believe it's a bug, because it requires ridiculous workaround with applying an offset to collider and a negative offset to each point. That's why I had to contact you via this forum.
    Thanks.
     
  2. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    11,491
    I'm assuming it's the same thing but I was asked by a QA the general question of the warning that's produced which indicates some primitive shapes couldn't be created because they didn't pass validation. In the end, the libtess2 library we use sometimes produces *very* thin triangles when the outline its being asked to produce causes large fine fan-shapes. This makes it very susceptible to minor floating-point shifts (any transform or offset change). There's absolutely nothing we can do about that. If we pass it to the physics system then it causes very bad collision responses so we filter them out. In nearly all cases this means removing a fractional triangle or super thin polygon wedged inbetwen others but these are fairly rare.

    If you are seeing significant visible polygons missing then that is indeed a bug but if the problem is that you don't like the warning because it's unexpected for you then that isn't a bug.

    We intend to expose the ability to allow Delaunay Triangulation which deals with these horrible fan-shaped things much better and also produces less primitive shapes but in the meantime, there's no bug that can be fixed unfortunately. If you are seeing fine fan-shaped primitives then this is likely the root cause of that sensitivity to changes.

    If this is for a Static collider then you can add a Rigidbody2D set to Static (this is the same as not adding a Rigidbody2D component btw). This means that when you move the body via the Transform in the editor or the body itself, it doesn't cause the collider to be recalculated so is a much more efficient way to offsetting a collider. It will act exactly the same.
     
  3. dotsquid

    dotsquid

    Joined:
    Aug 11, 2016
    Posts:
    224
    Hi, thanks for a quick reply.
    The warning does not bother me as opposed to the objects incorrectly penetrating each other.
    What I don't understand is that if this problem is caused by thin / degenerate triangles why collider automagically becomes correct if I set a non-zero offset while the set of points stays untouched?
     
  4. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    11,491
    Because at zero it changes nothing but non-zero slightly affects the points. Floating point changes mean the library sees a slightly different set of points. A small change of points can produce a larger change of decomposed convex polygons. The same happens with Transform changes although they're more evident if you were to rotate around the Z axis.

    If this were rendering it'd be fine but for physics it can be a disaster. Indeed, there's asserts all over the place checking these polygons.

    This is the library we use: https://github.com/memononen/libtess2

    Anyway, add a Rigidbody2D set to Static should work perfectly fine without any hacks.
     
  5. dotsquid

    dotsquid

    Joined:
    Aug 11, 2016
    Posts:
    224
    No, it does not. I added a Rigidbody2D to the problem collider in the project I attached to the original bug report, and another collider still incorrectly penetrates this collider.
     
  6. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    11,491
    Sounds like you misunderstood. Don't add offset, set the Transform local position with a Static Rigidbody2D. You already said your shape is fine with no offset.
     
  7. dotsquid

    dotsquid

    Joined:
    Aug 11, 2016
    Posts:
    224
    Yes, sounds like we are talking about different things. Did you see the repro project from the bug report?
     
  8. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    11,491
    If I set the offset to be (0.54,0) on my machine then some shapes are removed.

    Nevertheless, I'll reactivate the bug and assign to myself so I can take a deeper look at what's going on during the creation of that shape. Thanks for raising it as my only involvement so far was to answer QA regarding the question about offsets affecting primitive shapes being removed due to validation. That said QA were more than correct to close it upon my response so really pleased you raised it again as the shape size being removed is huge. Something is off there but it would seem that the slight floating-point difference is exposing the real bug.

    Please bear with me, kind of a crazy busy time right now. I'll post this forum link in the case as a reminder to get back to you here. If there's a bug I'll find it.
     
    Last edited: Sep 4, 2020
  9. dotsquid

    dotsquid

    Joined:
    Aug 11, 2016
    Posts:
    224
    Great! Thank you for taking the time on this problem.
     
    MelvMay likes this.
  10. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    11,491
    No problem, sorry for the issues it's causing you. Will get back to you ASAP.
     
  11. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    11,491
    Last edited: Sep 7, 2020
  12. dotsquid

    dotsquid

    Joined:
    Aug 11, 2016
    Posts:
    224
    \o/ Yay! Thanks.