Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Improving 2D collision detection performance with PolygonColliders

Discussion in '2D' started by Saticmotion, Aug 23, 2015.

  1. Saticmotion

    Saticmotion

    Joined:
    Aug 27, 2014
    Posts:
    15
    I'm working on a first prototype for my game. The game will have a large amount of sprites, so generating colliders by hand is something I'd like to avoid. So I'm letting Unity generate the colliders for me. I'm using PolygonColliders because sprites can be highly irregular, so they add a lot of realism to collisions.

    In my testing, the framerate drops to ~30 (from 60, because VSync) when around 50 sprites are on screen. I don't expect to have this many enemies on screen, but I'd like to make mobile builds if I can get the performance high enough. So: PC builds might be fine at this point, mobile probably not.

    Some more info about the setup:

    This is my test scene with ~50 enemies (Yep, those are maplestory sprites, used as placeholders):
    Untitled.png

    This is the setup of one of the large guys:
    Untitled.png

    As you can see. it generates a collider with 76 points. It matches the shape quite nicely, but obviously adds a lot of overhead. I'm also switching between colliders based on the current frame, but as far as I can tell from the profiler, that's not the issue. I've also made sure they're not triggers, since that also adds a lot of overhead (Framerate < 15 with just 10 enemies).

    What else can I do to improve performance? is there any way to have Unity generate PolygonColliders with lower detail? Any cheap to run algorithms I could use myself to generate colliders? Is there a way to disable collisions between all monsters? Maybe that's what's tanking my framerate.

    And a screenshot of the profiler:
    Untitled.png
     
  2. Saticmotion

    Saticmotion

    Joined:
    Aug 27, 2014
    Posts:
    15
    I've managed to find how layers interact with physics, added a layer for enemies and told it not to interact with itself. This brings the framerate to ~60 with 50 enemies. A nice gain, but not enough for mobile I suspect.
     
  3. imaginaryhuman

    imaginaryhuman

    Joined:
    Mar 21, 2010
    Posts:
    5,834
    Make sure the layer also does not interact with any other layers that it isn't actively meant to collide with.

    Also if you switch to multiple box/circle colliders it probably will be faster - polygons require more complex math to detect a collision, no to mention that every triangle in the polygon can be involved in the test, which is a lot of work.
     
  4. Saticmotion

    Saticmotion

    Joined:
    Aug 27, 2014
    Posts:
    15
    That would definitely be better for performance. Unfortunately, the overhead of creating the colliders for each and every enemy would be too much for me to handle.
     
  5. mgear

    mgear

    Joined:
    Aug 3, 2010
    Posts:
    8,991