Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

Question EdgeCollider2Ds and center of mass

Discussion in '2D' started by jdruzdzel, Jun 22, 2023.

  1. jdruzdzel

    jdruzdzel

    Joined:
    Jan 9, 2018
    Posts:
    8
    Hello,

    I have a Rigidbody2D with a rig set up with a bunch of EdgeCollider2Ds with non-zero radiuses, however modifying any of them does nothing to the auto-calculating center of mass. Adding other colliders such as a CircleCollider2D or BoxCollider2D does prompt CoM refresh, but while still ignoring the edge colliders in the calculation.

    Is this just a property of the EdgeCollider2D where the area is always seen as zero? Does anyone have a suggestion for a workaround? I would like to continue using edge colliders because they work great as a 2.5D collider (2D physics, 3D rotation).

    Cheers
     
  2. jdruzdzel

    jdruzdzel

    Joined:
    Jan 9, 2018
    Posts:
    8
    Currently, my workaround is adding a bunch of extra CircleCollider2Ds with all override layers set to Nothing. It feels a bit overdone but it'll do for now.
     
  3. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    10,623
    Yes, EdgeCollider2D with zero radius have no area so cannot change the center of mass. Adding even a tiny radius means they do and the COM will change. You'll also get this warning explicitly if you select outline mode of a CompositeCollider2D (attached to a Dynamic Rigidbody2D) as that produces edges too.

    Just use a tiny radius or just add up all the points of the edgecollider(s) and divide by the number of points to give a center-of-mass and set that. Easy to do in the start of an attached script.
     
  4. jdruzdzel

    jdruzdzel

    Joined:
    Jan 9, 2018
    Posts:
    8
    Thank you for your reply, MelvMay, but I think you misunderstand--all of my edge colliders already have *non*-zero radiuses, but the center of mass is not appropriately calculated. I am using version 2022.2.7f1
     
  5. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    10,623
    No, you misunderstand.

    As I said pretty clearly:
    If you read the remaining of what I wrote, it would help.
     
  6. jdruzdzel

    jdruzdzel

    Joined:
    Jan 9, 2018
    Posts:
    8
    I do not want to appear rude or unappreciative, but you are implying that an EdgeCollider2D with a radius that is not zero has area and thus affects the center of mass, when it really does not--EdgeCollider2Ds seemingly never have area. Is there something else I am misunderstanding?

    I did read your scripting suggestions, and those are great, thank you, however I was hoping I could rely on the underlying physics engine to deal with all CoM calculations implicitly.

    Here is an ugly gif that shows an unchanging center of mass when the edge radius is changing.
    EdgeCollider2D unchanging CoM.gif