Search Unity

  1. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Profiler Physics2D.FindNewContacts

Discussion in '2D' started by Kristen182, Jan 12, 2019.

  1. Kristen182

    Kristen182

    Joined:
    Aug 10, 2018
    Posts:
    42
    In my game, one the biggest performance costs in the profiler is Physics2D.FindNewContacts as shown in the screenshot. I don't know what this means and I can't find anything online. Can anyone explain to me what this means?

    My game has obstacles that are moved into the scene, and also collectibles. Since I read it was inefficient to create/destroy them, I have a pool of each that are disabled once they are outside the camera, and moved back into the scene when they are needed. I'm not sure whether this is related.

    upload_2019-1-12_12-4-17.png
     
  2. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    11,075
    It's doing what the title says, it's finding new contacts. This is Box2D (the 2D physics engine) processing colliders that have moved/been-created determining if they overlap. 26.13ms is a huge amount of time spent there.

    Disabling a physics component destroys the underlying physics engine object. Deleting the component would only take fractionally longer. It's more efficient to turn simulation off for a Rigidbody2D than destroy colliders etc.

    That said, manipulating things that are outside of the camera view like that is going to cost. How much depends on how many objects and what you're doing to them. If these items outside the camera view are doing nothing then why manipulate them at all? They are not performing any actions so don't cost you anything. If they're static then they never take any time. If they are dynamic then let them sleep or put them to sleep explicitly.
     
    mitchellkinard likes this.
  3. Kristen182

    Kristen182

    Joined:
    Aug 10, 2018
    Posts:
    42
    To explain further, I don't manipulate objects out of the camera view until I need them. Since it's an endless game with obstacles, I recycle my obstacles instead of deleting them and recreating them every time I need them. The only manipulation I do is move them back into the scene when I need them

    So it's inefficient to disable them once they're out of the camera? I'm not sure what you mean by turn the simulation off for the rigidbody. Should I do this instead? What type of c# call would doing this? I tried looking it up but I'm not finding much. Or should I put them to sleep? These objects are all Kinematic, I never considered switching them to static. Since I need to reposition them to recycle them, I'm not sure if static would work. I'm sorry I don't fully understand what the best route to take here is.

    Thanks for your advice.
     
  4. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    11,075
    I wasn't suggesting you do that. I meant that you can happily leave static stuff alone as it costs nothing however I didn't know you had an endless runner so you'd never see those objects again by the sounds of it so they need recycling as you say.

    It's Rigidbody2D.simulated however given what you've said, it sounds like you're still going to reposition them when you want them again which causes the same update. Given the time spent, you must be doing this for a lot of colliders or colliders with a lot of shapes perhaps.

    To be better help I'd need to understand more detail on how many colliders/shapes are being updated for that 26ms+ time. Also look at how many contacts you have being generated in the profiler might help.
     
  5. Kristen182

    Kristen182

    Joined:
    Aug 10, 2018
    Posts:
    42
    Since posting this question, I learned about the collision matrix which helped my issue a lot since I was not aware I could control which objects need to look for collisions with each other. This made a big difference but I was still having issues, but for some reason with one obstacle specifically. I think the issue is coming from my choice of colliders. I've been using only polygon colliders, not aware that they cost more for performance. Once of my obstacles must have a complicated shape because the performance improved when I switched it to a circle collider. I'm going to play with it more, but do you know if it's better for performance to have multiple simple colliders (ex: 2 box colliders) instead of a polygon collider? I don't want the collider to deviate too far from the shape of the object.

    There are 4 obstacles, each in a pool of size 3, so a total of 12 objects are being worked with. I also have a pool of 10 much smaller and simpler collectibles, but they aren't affecting performance nearly as much.

    Do you think it's inefficient that I disable the obstacles when the camera moves past? You mentioned this destroys the underlying physics engine object so I was wondering if I should be leaving them enabled? I just assumed it was more efficient to disable objects until I need them. I understand if you don't have all of the answers - looking at performance is still very new to me!
     
  6. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    11,075
    A circle collider produces a single circle shape and circles are very fast to calculate contacts for so they'll be quicker. A box collider will produce a single polygon shape (box). A polygon collider can be slower but primarily because it can produce multiple polygon shapes. For instance, a polygon collider in the shape of a box (or any 4 vertex convex shape) has the same cost as a box collider because they both produce a single 4 vertex polygon. If you go to the collider and open the info tab it shows you the shape count (amongst other info).

    Polygon colliders can get out of control though and produce a hell of a lot of shapes, especially on sprites that have curves.

    Disabling objects are efficient but it depends on what is being disabled and what data it's dealing with. A polygon collider with hundreds of shapes or hundreds of polygon colliders with dozens of shapes is going to get expensive.
     
    Last edited: Jan 16, 2019
  7. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    11,075
    If you turn off the simulation for the Rigidbody2D then it'll remove the contacts and not be part of the simulation. You can simply reposition it then enable the simulation later. This is more efficient than disabling the object as it does not involve deleting the shapes. This is especially true for polygon collider as when you enable it, it has to calculate the shapes then create them.

    Whatever you do, FindNewContacts will still be called but ensuring that you use the collision matrix so only obvious things can collider are checked will reduce time spent there.
     
  8. Kristen182

    Kristen182

    Joined:
    Aug 10, 2018
    Posts:
    42
    Thanks, I didn't know about the shape count. The obstacle that was performing badly has a shape count of 522 with a polygon collider attached so that explains why changing the collider helped so much.

    Thank you for all the advice! Find new contacts is hardly affecting performance now.
     
    MelvMay likes this.
  9. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    11,075
    That's great to hear. Sounds like you had a "bad apple". :)
     
    roshan090 likes this.
  10. vverma9

    vverma9

    Joined:
    Jan 14, 2017
    Posts:
    6
  11. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    11,075
    Only thing I can say is that it's obviously spending a lot of time finding new contacts. There are a lot of reasons this could be:
    - Recreating static colliders because you're modifying the Transform
    - Too many colliders that shouldn't be contacting each other being overlapped
    - Using PolygonCollider2D (or TileMapCollider2D) that produces too many collision shapes.
    - etc etc etc

    Have a look at the profiler "Physics 2D" area, look at the number of contacts produced when this happens. It'll probably be very high.

    Make sure your ladders don't end up producing 10s of thousands of shapes or something bad. I'm not sure but it seems your ladder collider shapes might be modelling the holes too. Not sure that's necessary when a simple box would do. Each one of those ladder segments would be broken down into multiple shapes.