Search Unity

  1. Unity Asset Manager is now available in public beta. Try it out now and join the conversation here in the forums.
    Dismiss Notice

OnCollisionEnter2D is called later than OnTriggerEnter2D (Case 808525)

Discussion in '2D Experimental Preview' started by Jay-Pavlina, Jun 24, 2016.

  1. Jay-Pavlina

    Jay-Pavlina

    Joined:
    Feb 19, 2012
    Posts:
    195
    I submitted a bug report with a simple repro project. Case 808525.

    It's kind of funny, but this is the opposite of a bug I reported a few years ago, when OnTriggerEnter2D was being called later than OnCollisionEnter2D. (Case 572272) I guess now it's OnCollisionEnter's turn to be late...
     
  2. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    11,456
    Is this related to the experimental beta only then? I believe this relates to the standard release.
     
    Last edited: Jun 24, 2016
  3. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    11,456
    Unfortunately, this is Box2D and you go check that out yourself!

    Box2D finds/reports contacts prior to the solver running. It then runs the discrete-solver then the continuous-solver. If those produce a new contact then those are reported next update. It does this so it has all the existing contacts before the solver runs.

    For triggers (sensors in Box2D speak), after the solvers have run, we explicitly cycle through all contacts checking for sensors and perform a contact update which can be expensive as it performs narrow-phase collision-detection. This is a performance penalty but no other impact apart from the fact that we are performing narrow-phase col-det twice each update. If we were to do this for non-trigger contacts then it would become much more expensive as they are prevelant.

    Also, doing so for these means that the detailed collision responses are lost such as the relative-velocity on collision as one is reported at the start of the update and another is reported at the end. We can't report two so the last one is reported. In a lot of cases this is zero as the first collision response contained the impact velocity, the solver ran and stopped the objects and the second callback reports zero impact velocity.

    This is only a single example and I found this problem at my peril as I modified Box2D earlier in the release cycle and lots of people started reporting issues with things such as relative velocity.

    TBH: We shouldn't be reporting the trigger as we do but there were a lot of complaints about it, the penalty for implementing it is performance.
     
  4. Jay-Pavlina

    Jay-Pavlina

    Joined:
    Feb 19, 2012
    Posts:
    195
    I think I was the main person complaining about it. I'm sorry it caused issues.

    I don't understand how people are ok with this though. How are you supposed to make a responsive action game if all of the callbacks you get are late? If you didn't fix the triggers, all attacks would be late, unless you would call Physics2D.OverlapArea every fixed time step.

    Since the kinematic contacts are late, I can't use them. I need to know right away when my character lands on the ground. If it's one step late, they fall through the ground and are then moved back on top of it. I guess I'll just have to use raycasts for everything. It adds so much more complication. :(

    The only other solution I can think of is to half my fixed time step so that there are two before any render frame. Is that really something I should consider? What would you do to account for this?
     
    Last edited: Jun 24, 2016
  5. Jay-Pavlina

    Jay-Pavlina

    Joined:
    Feb 19, 2012
    Posts:
    195
    It works fine if I decrease the time step. I am interpolating my character's movement anyway, so it's not a huge deal. Back when I reported the problem 2.5 years ago, I didn't know a whole lot about fixed time steps / interpolation and was baffled by it. So I had some pent up anger related to the issue, but much of it was a result of ignorance, as it seems most physics engines work this way.

    How many other people complained about the trigger being late? Because I remember when I reported it you seemed really surprised and you said you spent an entire day investigating it or something. I feel sort of bad about it now. Can you remove it or will that mess everyone up?
     
    Last edited: Jun 24, 2016