Search Unity

2D Physics General Physics Feedback

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

  1. Jay-Pavlina

    Jay-Pavlina

    Joined:
    Feb 19, 2012
    Posts:
    195
    I’ve spent a bit of time with some of the new Physics features and I’m loving them. Here's some feedback.

    Kinematic Contacts
    Best feature by far for me is kinematic contacts. I’ve been waiting for something like that since the 2D physics was announced. I can finally get collision data without the physics system reacting to the collision. This makes it way easier to program 2D platformers that don’t have realistic physics.

    I noticed that the normal impulse property of the contacts of a kinematic body collision are 0. If I wanted to simulate a regular collision reaction, is there a way for me to do it? Then I would basically be able to determine whether objects react to physics after a collision occurred. I tried using the normal with the edge separation, and it seemed to work alright, but was not perfect. I am mainly asking because I thought it might be an easy way to position my character on top of ground and stuff like that. I usually just get the actual position of the top of the collider and put my character on top of it.

    Edge Radius
    The edge radius seems to add some possibilities, but I am wondering if there is a more intuitive way to size things in the editor? If I wanted to have a BoxCollider of size 1, but I want the corners to be rounded, it seems hard to get that sized correctly. What if the property was just called roundness, and it would keep the box the same size, but would just round the corners? This seems like it would also work on a polygon collider, but on edge collider it would still need to be called radius since they have no size by default. Just a thought.

    LateFixedUpdate
    Please try to convince Unity to add a LateFixedUpdate callback. This will be useful for some of the new API you've included, like Physics2D.GetContacts. If we want to always make sure the info from those is accurate, we'd need a callback for after all physics have been simulated. I want the callback so I can check on things and make sure nothing is wrong with my bodies and colliders. There is currently no way to get a callback after all OnTrigger and OnCollision methods have fired. This has caused me much strife.

    Other Notes:
    • New casting stuff looks good and useful
    • Isn't it redundant to have some of the same methods in two places, like how GetContacts is on both Physics2D and Rigidbody2D/Collider2D? Seems to make more sense just being on Rigidbody2D/Collider2D
    • Is there any way to get both colliders in a trigger collision, like in OnTriggerEnter2D? I've always found it annoying only one collider is included, and I've had to do weird things with game objects to be sure I knew which colliders were hitting. Can you add this if it's not included already?
    • Radius seems like a good fit for PolygonCollider2D as well
    • I like the new ContactFilter2D struct. Reduces clutter.
    • New Physics2D gizmos in editor are awesome. Saved me lots of time already.
    • Isn't edge collider with a radius the same thing as a capsule collider?
    I mainly just use the physics2D stuff for collision detection and control stuff myself, so I've never used effectors or joints and can't give feedback on them.
     
    MelvMay and der_r like this.
  2. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    2,053
    I'll have to dig into the code and check this for you.

    One of the features I wanted to work on was the ability to ask for a body or list of bodies to be 'solved'. This would mean passing in an array of bodies, letting the solver run on them and returning the position/rotation for each allowing you to update them yourself. This is one way, the other way is to modify the bodies and not return the position/rotation.

    Yes, that's another way of approaching this however it does make things more difficult when considering PolygonCollider2D and it doesn't fit with how the EdgeCollider2D works. Thicknening of the edges i.e. a radius was an approach the worked in all cases.

    I too want this and hopefully we'll see it eventually.

    Not really. The primary one is the static Physics2D ones with the instance Rigidbody2D/Collider2D just being convenient as you don't need to specify which Rigidbody2D/Collider2D.

    The new GetContactsAPI had a new callback which provided this however, due to some pending internal changes outside of the 2D physics system, this wasn't in the preview. We need to stop allocations in the OnCollision callbacks (both 2D & 3D) and part of that is changing Collision/Collision2D to structs. We can't do that and maintain backwards compat. The future then means we will keep the OnCollision/OnTrigger callbacks for backwards compat but introduce a unifying callback that has zero allocation and tells you if it's a collision/trigger and provides all the details that you'd expect including both colliders/bodies etc. Again though, this is being held back by the other work on the callback/event system.

    It will be coming to PolygonCollider2D which itself will be having a huge makeover making it efficient (it's terrible now) and provide access to the paths/verts individually for updating.

    That's good to hear. There are other uses of ContactFilter2D coming as well. One of which is its use in IsTouching/IsTouchingLayers. This will be useful for cases such as checking for 'Ground' as defined by a collision from 'below' the character (collision normal range) etc.

    Deep down internally it's exactly that however EdgeCollider2D won't collide with EdgeCollider2D whereas CapsuleCollider2D will. Just different internal intersection tests but essentially you are correct.

    Thanks for the feedback, much appreciated!
     
  3. Jay-Pavlina

    Jay-Pavlina

    Joined:
    Feb 19, 2012
    Posts:
    195
    I would prefer updating the position/rotation manually if I had to choose between them. This gives more control, and allows you to do things like simulating the trajectory of a grenade without actually modifying the position of anything.
    Sounds good. I personally always thought it would be more useful to have callbacks that are actual C# events instead of Unity's message things. So you would use it like this:
    Code (CSharp):
    1. GetComponent<BoxCollider2D>().OnCollisionEntered += collision => {
    2.     Debug.Log(collision);
    3. };
    I just meant in the editor it would make things easier, even if it's only available in the edit collider mode. (Although I personally also like the option of entering it in a field.) If I had the algorithm I'd probably just write a custom inspector to add the field. On Box collider, I added fields for adjusting size and position from the bottom left because I do that quite often.
    Sounds good. My guess is you improved it to make it easier on yourself to build the tile map collider. ;)
    People may get confused by edge colliders not hitting each other in the future when they forget the time that they existed without a radius.
    Either way you need the instance so why not just have it on the instance? Not a big deal. Just sayin'.
     
  4. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    2,053
    There's been some push-back when it's been suggested that we add those. There's some backend work aparently going to happen with events so maybe it'll happen. You can do this already: https://twitter.com/melvmay/status/674202826717978624

    Not really. The existing PolygonCollider2D hack for TileMap wasn't written by me and will be removed, replaced by a new TileMapCollider2D.

    Yeah, it's not a big deal. It's preference and syntax choice.