Search Unity

Positions of collisions and when collision events are called.

Discussion in 'Physics' started by Lukephos, Jul 13, 2019.

  1. Lukephos

    Lukephos

    Joined:
    May 7, 2017
    Posts:
    8
    I'm new to using unity physics and I've ended up in a deep rabbit hole with the mechanic I'm trying to make.

    I have a 2D square ship that follows the users finger on mobile, and square weapons that float down the screen. On contact, they attach to the side they touched, kind of like a gun katamari. This has ended up meaning a lot of small colliders moving together very quickly into each other and needing to know positions at the point of contact. This has proven pretty difficult for several reasons. The biggest one however is how at least to my knowledge I'm dealing with information between frames. This has meant I'm doing things like casting backwards from the current frame to the last frame using a bit of info gained from ContactPoint2D which is somewhere in between. I've managed to get really close to a perfect system but not quiet there, and it shows. Regardless it's a really convoluted system to get this info that doesn't seem right.

    All of this has been going from the assumption the physics cycle runs, stores some info about the collision, then calls the collision event when the physics cycle is over. I don't actually know this is true though, and I can't find much about how unity's physics cycle actually works. Can someone please explain a little more about the order of events to do with collision and event calls? Importantly though if you know how to get something like the local position of contacts or something like that I'd really appreciate it. To my knowledge there's only the info you get from ContactPoint2D to work with and I'm struggling to get much from that to help with the attach position.

    I've been at this for weeks and at this point I'm tempted to make a dummy object follow the movement and ignore collision. I've tried everything I can think of and there's always a tiny catch to make it fail.
     
  2. radialxawn

    radialxawn

    Joined:
    May 13, 2019
    Posts:
    29
  3. Lukephos

    Lukephos

    Joined:
    May 7, 2017
    Posts:
    8
    Uh, ye. Didn't know if you knew this but uh, sending me something that just says 'physics happens here' doesn't at all answer my question. I wanna give you the benefit of the doubt and say that's good intended but I really don't believe it is.
     
  4. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    1,964
    The physics simulation step does the following in order:
    1. Finds New Contacts
    2. Update Existing Contacts
    3. Integrate Positions/Rotations
    4. Solve
    5. Write-Back Body Positions/Rotations to Unity Transforms
    6. Perform Contact Callbacks (OnCollision/OnTrigger)
    All this happens in Physics2D.Simulate which is either called implicitly during fixed-update or manually by you.
     
    Last edited: Jul 14, 2019
  5. Lukephos

    Lukephos

    Joined:
    May 7, 2017
    Posts:
    8
    Thanks, that's good to know. Does that mean the only information about a collision I can get is from ContactPoint2D? Since by that description everything has already moved on from said collision by the time oncollision is called.
     
  6. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    1,964
    What other information do you require?
     
  7. Lukephos

    Lukephos

    Joined:
    May 7, 2017
    Posts:
    8
    Probably the most helpful for me right now would be the positions of objects in the collision, or the local position of the collision point. I'm trying to work backwards to get that right now but it's been a huge pain to try figure out since the colliders have pushed each other.
     
  8. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    1,964
    I see you mentioning "frames" but know that the simulation does not run per-frame but per fixed-update unless you're manually running the simulation. Also, Transform is the same as the Rigidbody2D pose if you're not using interpolation/extrapolation.

    The position/rotation of the Rigidbody2D prior to the simulation should be exactly what you need. Then the simulation runs and calculate contacts at the current poses, integrates positions then solves the contacts and writes back the new poses. So the contacts show contacts at the previous step with the new poses (as you indicate) being what will be solved next time and are not necessarily at the contact points but might be.

    FYI: Assuming you don't already know, we use Box2D and more detail of what it does can be found here in "b2World::Step()". You can see here that at the existing body poses, new contacts are found and existing contacts are updated/deleted. These are then solved as I described above.
     
  9. Lukephos

    Lukephos

    Joined:
    May 7, 2017
    Posts:
    8
    I'm a little confused what you mean by that. I'm pretty bad with terminology, sorry. But from what I've tried if I do a line from the previous frame (also I mean update when I say that, sorry) to the current one the collision point happens between them. I should also mention I'm using continuous collision if that helps.
     
  10. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    1,964
    First things first, as I've already said, physics runs in FixedUpdate not Update (per-frame) so if your physics is set to run 60Hz and your frame-rate is 180Hz then you get a fixed update then three updates. You should be aware of that if you're doing stuff per-frame. In most cases this means that you're doing the same work several times without the physics updating. Rigidbody2D Interpolation is used per-frame to update the transform.

    There's a bunch of things here so without narrowing down what you're doing we could end up going around in circles.
     
  11. Lukephos

    Lukephos

    Joined:
    May 7, 2017
    Posts:
    8
    Sorry, meant fixed update. Been talking to people with this where it being fixed update has been established for so long I forgot to clarify. I know what you mean by that though. Isn't there still the problem of the collision happening in the middle of the physics movement? I'm being really bad with terms here but what I said earlier about position stuff.