Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Physics2D Collision Garbage

Discussion in 'Physics' started by Robdon, Apr 29, 2017.

  1. Robdon

    Robdon

    Joined:
    Jan 10, 2014
    Posts:
    141
    Hi,

    I had been following this post (https://forum.unity3d.com/threads/physics-contacts-gc-activity.369625/) as I had seen some garbage generated through the collision 2D system.

    I've just upgraded from 5.5 -> 5.6, which states 'Physics2D improvements', so I tested to see if the "Physics.Contacts" garbage allocations had gone.

    Woohoo, I thought, as I could not see any, however, now I'm getting allocations on 'Physics2D.ConvertCollision2DForScript' (this wasn't there before in 5.5, and I cant see anything in Google about it)

    This seems to be a new one, or has Contacts just been renamed, and nothing has been fixed with garbage yet?

    See attached for the profiler screenshot.

    Thanks, Rob Donovan.
     

    Attached Files:

  2. herb_nice

    herb_nice

    Joined:
    May 4, 2017
    Posts:
    170
    I see the same thing in 2017.1.0b3. I think you may be correct.
     
  3. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    11,314
    GC for 2D & 3D callbacks are not new although the profiler entries have changed a lot but there are two problems.

    The first is that the Collision & Collision2D types are classes and are legacy issues. They cannot simply be changed to structs but there are plans to provide better callbacks. There is a small overhead of creating the Collision or Collision2D classes.

    The second is more of a problem in that the "contacts" property returns an array of contact points. This is done for you even if you don't use them. For 2D I have fixed this and it will be in a future release: https://twitter.com/melvmay/status/861222263009443840

    Essentially "Collision2D.contacts" for 2D physics will now be evaluated lazily (only if you use them). To avoid GC altogether, you can call "Collision2D.GetContacts()" and there's a new "Physics2D.GetContacts(Collider2D, Collider2D)" as well.
     
  4. herb_nice

    herb_nice

    Joined:
    May 4, 2017
    Posts:
    170
  5. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    11,314
    How can I answer such a question?

    I didn't say that it was fixed and it's about to land either. You forgot to mention the detail of what I said which was that this work cannot continue while we're held up with other implementations that are outside of our control. Unity is a large organization and priorities shift and the physics team and others are at the mercy of various systems dev.

    I recerntly made a big push on this because I was frustrated by nothing happening. The good news is that we've got a meeting with the appropriate parties involved with a view to getting this finally resolved, even if it's just for 2D/3D physics initially.
     
  6. herb_nice

    herb_nice

    Joined:
    May 4, 2017
    Posts:
    170
    Don't want to argue but I do want to prod you to get this done.

    Over a year ago you said:

    ...



    So after a year you've booked the meeting. That's great, any forward motion is appreciated. Please keep us posted and best of luck.
     
    Last edited: May 9, 2017
  7. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    11,314
    No, I said this:
    Completely transparent and the problem and intent was made clear. No promises either. We've been held up by major refactorings by other teams that are now coming to fruition.

    Inferring that I made the post then waited for a year to book a meeting.

    Sure.

    Yeah, sure.
     
  8. Robdon

    Robdon

    Joined:
    Jan 10, 2014
    Posts:
    141
    Thanks for the reply Melvyn.

    Ah, so when we have this 'lazy' fix, what we could do is still use the OnCollisionEnter2D() event, with no params, and then manually process GetContatcs() with a predefined array, and then we should get zero GC I guess?


    Edit: Ah sorry, I see over on another thread (https://forum.unity3d.com/threads/physics-contacts-gc-activity.369625/) you talk about it in more details... ty
     
    Last edited: May 11, 2017