Search Unity

PolygonCollider2D.SetPath is slowing down my collider generation at runtime

Discussion in 'Physics' started by Yorsh, Mar 29, 2020.

  1. Yorsh

    Yorsh

    Joined:
    Feb 17, 2015
    Posts:
    6
    Hello,

    My game has a destructable 2D world like worms.



    I'm trying to generate a new PolygonCollider2D at runtime after the map is modified. Everything works fine until I call SetPath(), which can take 200ms to execute. This is what I have done :
    1. Paint on the map texture.
    2. Generate a grid of node for marching squares from the map texture (alpha > 0.5 is true, otherwise it's false).
    3. Run marching squares, but instead of generating triangles I customized it to generate the paths going around my solid objects (nodes that are true).
    4. Call PolygonCollider2D.SetPath for each one of the paths.

    Code (CSharp):
    1. List<List<Vector2>> collisionPaths= marchingSquares.GeneratePaths();
    2. collider.pathCount = collisionPaths.Count;
    3. for (int i = 0; i < collisionPaths.Count; i++)
    4. {
    5.       collider.SetPath(i, collisionPaths[i]);
    6. }
    SetPath gets slow for a lot of points, like 60 paths for a total of 3000 points. From my understanding it's generating shapes based on the paths on the main thread, so I can see why it's freezing the game, but I'm looking for a workaround. I'm not sure what to do...
    • Is there a way to make it async instead ?
    • Could I feed it the collision shapes it's trying to generate already made instead of paths ? I can propably do this easely, but I don't know how to give them to the collider.
    • Am I trying to make the polygon collider works with way too many points ? Should I inherite from collider2D and make my own collider type or something ?
     
    dunk7 and unity_O944Xfg5fdDMJQ like this.
  2. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    11,492
    The way you could speed it up is to disable the collider, change all the paths then enable it. With it enabled, it performs work after each path set which is redundant as you have a lot of paths to change.

    Doing this is perfectly acceptable because disabling the collider has the effect of destroying all contacts and collision shapes but so does calling SetPath which in the case of it being enabled, contacts are destroyed, shapes are destroyed and new shapes are recreated.

    With it disabled, it does very little apart from validating your path points and storing them away. When you enable it, it then creates collision shapes from your path(s).
     
    Last edited: Mar 30, 2020
  3. Yorsh

    Yorsh

    Joined:
    Feb 17, 2015
    Posts:
    6
    Wow that did sped it up by like 40 times on my exemple scene. Thanks a lot.
     
    dunk7 and MelvMay like this.
  4. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    11,492
    btw, what you're doing here looks very cool.
     
    dunk7 likes this.
  5. ryanspr1vates95

    ryanspr1vates95

    Joined:
    Jul 30, 2018
    Posts:
    5
    Sorry to post on a 3 year old post, but I'm interested in this as I am trying to do something similar. Would love to message you more about it!
     
    MelvMay likes this.
  6. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    11,492
    Feel free to create your own thread and tag me in with a question.
     
  7. ryanspr1vates95

    ryanspr1vates95

    Joined:
    Jul 30, 2018
    Posts:
    5
    I actually was addressing Yorsh haha. But I'll for sure use you as a resource!
     
    MelvMay likes this.