Search Unity

Physics2D.DestroyShapes is taking 22ms

Discussion in 'Physics' started by StuwuStudio, Jun 1, 2022.

  1. StuwuStudio

    StuwuStudio

    Joined:
    Feb 4, 2015
    Posts:
    165
    I'm having some issues optimizing the physics for my 2D game and I'm realizing a lot of my spikes are caused by Physics.DestroyShapes.
    Is this a bug? How come destroying shapes could be more costly than creating new ones? Should I just never deactivate the collider and move it away when I don't need it?

    Another thing to note is that I use
    Code (CSharp):
    1. collider.enabled = false
    before setting a bunch of paths on my polygon collider to avoid re-creating the shape every time I add new path but instead only once I'm done adding all paths. Knowing that, I can't really keep all colliders always on.

    upload_2022-6-1_15-22-42.png
    upload_2022-6-1_15-20-4.png
     

    Attached Files:

  2. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    11,459
    It's hard to say really because there's no information on how many shapes are being destroyed. By this I don't mean collider count, I mean shape count for each collider as shown in the inspector for each Collider2D in "Info".

    It could be related and subsequently fixed around 4 months ago by this: https://issuetracker.unity3d.com/is...ances-even-when-very-few-physics-shapes-exist

    Note the above wasn't "very few physics shapes", it was a lot but it was still taking too long. It's not just the shapes, it's all the related information such as contacts that need destroying too.

    Here's the fix stream history:
    Merged to 2022.2/release in 2022.2.0a9
    Merged to 2022.1/release in 2022.1.0b5
    Merged to 2021.2/release in 2021.2.9f1 & 2021.3.0f1
    Merged to 2020.3/release in 2020.3.27f1
     
  3. StuwuStudio

    StuwuStudio

    Joined:
    Feb 4, 2015
    Posts:
    165
    I'm using 2021.3.1f1 (And I'm assuming this version would have the fix right?). It like I might still be having that issue that was supposed to be fixed. Or maybe I have the fixed version and the impact would've been much bigger before it was fixed.

    I was able to reduce the impact of the collider destruction in my game by reducing the chunk size of my 2d terrain, thus cutting down the number of shapes being created. There was probably some mistake in my system that was disabling too many colliders too because 117 calls seems excessive. (Although doing the math, this would still mean a destruction time of ~0.1ms/shapes which still seems a bit high)
     
  4. SuperPaw

    SuperPaw

    Joined:
    Jan 23, 2018
    Posts:
    1
    Problem also still exists for me in version 2021.3.1f1.
     
  5. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    11,459
    Please submit a bug report because this means you're encountering a different problem and you posting here won't help you or anyone else. As much as it's easy to assume it's the same problem and say it wasn't fixed, it was, so this simply highlights it's a different issue with a similar symptom.

    Lots of things happen when shapes are destroyed. The above reported issue had a specific cause and it was resolved and was verified in both the submitted project and separately by QA and by performance tests.

    Performance can always be improved but there's a clear separation between a gross problem and a performance improvement. WIthou you submitting a bug report, it's impossible to action.
     
  6. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    11,459
    A single call to that could be destroying any number of shapes. It's not called once per-shape but once per Collider2D. For instance, a PolygonCollider2D could have hundreds of shapes, a TilemapCollider2D could have tens of thousands. A Box, Circle or Capsule would only have one.
     
  7. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    11,459