Search Unity

Official 2D Physics in Unity 2022.2

Discussion in '2D' started by MelvMay, Nov 20, 2022.

  1. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    11,497
    2D Physics in Unity 2022.2

    2DPhysicsProfiler.png

    Hi 2D folks! We have new updates for 2D Physics in Unity 2022.2!

    What’s new?
    • New Physics Preferences Window
      • Previously, the physics gizmo configuration was located in the project settings but have now been moved into their own Editor wide preferences window.

    • New Physics Settings Window
      • A reorganised physics settings window has been created that separates the "General Settings" from the more commonly used "Layer Collision Matrix" into their own tabs. The window will also stay on that tab when you revisit for convenience. A few minor refactors here are a renamed "Multithreading" property and a single drop-down option for selecting how physics gizmos work.
      • The new Gizmo options here now also include the ability to control whether the Collider outlines are drawn as well as a new interior Collider fill option. Each of these has their own colors you can select from too, selected in the new Physics Preferences Window.

    • New Physics Profiler Module
      • The physics profiler module has been completely redesigned to provide more in-depth information related to what is happening under-the-hood as well as being organised into logical areas of activity to make them easier to view. Also, nearly all of these values shown are available as custom profiler counters which are compatible with customizing your own profiler module as can be seen here.

    • New Joint2D Break Actions
      • This new property allows you to control what action (if any) will be taken when a joint breaks its specified break force or break torque. Previously, the only implicit action was to destroy the joint. Now you can ignore it, only have a callback, disable the joint or destroy the joint as before.

    • New "Include Layers" and "Exclude Layers" on Collider2D and Rigidbody2D
      • Each Rigidbody2D or Collider2D can now override the "Layer Collision Matrix" and include and/or exclude specific layers it should interact with. This allows very fine control per component regarding what interactions should take place without being forced to do this at the project-wide level.
    • New "Force Send Layers" and "Force Receive Layers" on Collider2D
      • All Collider2D can now select how a contact between itself and an another Collider2D is handled by the physics system. Specifically, it can control if it will cause a force to be sent to another Collider2D as well as if it can receive a force from another Collider2D. This allows a uni-directional force upon contact as opposed to the default bi-directional force. This can be controlled on a per-collider and per-layer basis too! Here's the CircleCollider2D as an example.
    • New "Contact Capture Layers" and "Callback Layers" on Collider2D
      • All Collider2D can now select which Layers it wishes to be captured from the internal physics system. Capturing contacts has an overhead so turning this off for any layer(s) can give a substantial performance boost if you're not interested in knowing about them. You can also control which specific layers will receive callbacks too! This allows the physics engine to behave as normal but allows you to only receive callbacks for the specific layers you're interested in. This could remove the need to check a specific layer in a script callback. This is available on all Collider2D; here's the CircleCollider2D as an example.
    • New "TotalForce" and "TotalTorque" on Rigidbody2D
      • Previously when using methods such as AddForce or AddTorque on a Rigidbody2D, the force or torque was stored away internally and applied during the simulation step but no access was made available to these values. Now these values are directly exposed so that you can not only read them but also write to them too!
    Samples
    Check out the 2D Physics Examples GitHub repo (branch "2022.2"). We've added a bunch of scenes to demonstrate the above new features:

    About 2D Physics
    Read more about the current 2D Physics features here.

    What can you do?
    Try it out and let us know what you think of the additions and improvements. We want to know what works as expected, what doesn’t and what is missing. We’d love to see how you use them as well, so please show off all the cool things that you make with them!
     
  2. DragonCoder

    DragonCoder

    Joined:
    Jul 3, 2015
    Posts:
    1,700
    Awesome, finally an intuitive solution (unlike where physics and graphics layers were the same). This is definitely helpful!
    Could you tell whether there is a noticeable performance disadvance of setting this on ~200 bodies vs using the global setting?
    Aka are those batched into a virtual global layer, or does it require a per-object check like the old IgnoreCollisions method?
     
  3. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    11,497
    It doesn't affect performance at all. It also doesn't stop the use of layers. This is the ability to exclude or include layers on-top of what the global is. This allows completely overwriting what the global is, removing some of the layers from the global or including extra ones on top of the global. This is per-collider or rigidbody (all its colliders) so you are free to turn-off all Layer Collision Matrix interaction if you wish and simply use the per-collider Include Layers for instance. Or you could have your LCM set-up and have a specific collider not interact on certain layers etc. In the end though, it's a super simple bit-mask/bit-mask check.

    Physics always involves a "per-object" check. It works because when there's a potential contact from the broadphase, the layers for the two physics shapes (sub-parts of any collider) are checked to see if their masks allow it. It's already "baked" into their respective masks as a combination of the LCM and their include/exclude overrides.

    Also, because you can change this at runtime, you can do things like stopping a specific collider or a whole rigidbody interacting with certain layers or start interacting with them. Let's say, this specific player is invincible so you turn-off its enemy layer interaction for as long as you want. It'll only apply to this one.

    In the case of IgnoreCollisions, that is a fast-hash-table of specific collider/collider interactions. That happens after this mask/mask check. That is always checked as part of seeing if a contact can proceed to the more expensive narrowphase intersection tests.

    Lots of use-cases really.

    Hope that clarifies it.
     
    DragonCoder likes this.
  4. Baste

    Baste

    Joined:
    Jan 24, 2013
    Posts:
    6,338
    Looks great! Excited to get into these for future projects.

    We're shipping on 2021 LTS for safety, but I might just spin up a version of our game for 2022 so I can check the physics profiler for details.
     
    dannyalgorithmic and MelvMay like this.
  5. kayroice

    kayroice

    Joined:
    Feb 7, 2017
    Posts:
    49
    I've been in need of the ForceSendLayers option for quite a while now, and have been waiting to try it out since it was announced a while back. Just started using it (2022.2.8) and it perfectly solved my use case: item is dropped, need the item to interact with both the environment and units, but not apply a force to units when being captured. Totally worth waiting for, and saved me from having to code up a kludge to get the desired behavior, thanks @MelvMay!
     
    MelvMay likes this.