Search Unity

Bug Rigidbody2D.GetRelativePoint() doesn't work if body.gameObject is inactive.

Discussion in 'Physics' started by aegis123321, Jun 18, 2021.

  1. aegis123321

    aegis123321

    Joined:
    Jul 5, 2015
    Posts:
    71
    Rigidbody2D.GetRelativePoint() always return (0,0) if its gameObject is inactive.

    There is no description on UnityAPI about this.
    https://docs.unity3d.com/ScriptReference/Rigidbody2D.GetRelativePoint.html
    And not only GetRelativePoint() but GetPoint(), GetVector(), and GetRelativeVector() are broken.

    I think because the Rigidbody2D.position and Rigidbody.rotation works fine in inactive state, those methods should also work. And those methods work well if simulated=false.
    So this seems to be a bug.

    Code:
    Code (CSharp):
    1. [RequireComponent(typeof(Rigidbody2D))]
    2. public class BugReproducer : MonoBehaviour {
    3.     public Rigidbody2D body;
    4.     public Vector2 anyPoint;
    5.     public void Start() {
    6.         body = this.GetComponent<Rigidbody2D>();
    7.         body.gameObject.SetActive(false);
    8.         Debug.Log($"position: {body.position}");
    9.         Debug.Log($"rotation: {body.rotation}");
    10.         Debug.Log($"GetRelativePoint: {body.GetRelativePoint(anyPoint)}");
    11.         Debug.Log($"GetPoint: {body.GetPoint(anyPoint)}");
    12.         Debug.Log($"GetVector: {body.GetVector(anyPoint)}");
    13.         Debug.Log($"GetRelativeVector: {body.GetRelativeVector(anyPoint)}");
    14.     }
    15. }


    Unity2019.4.17f1

    Case 1344115
     
    Last edited: Jun 18, 2021
  2. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    11,491
    Actually position and rotation don't read the body if it's inactive as it's not around then at all. They just return the Transform pose instead which is one of many fallback behaviours we've had to implement. Stuff like velocity, Sleep, WakeUp etc all have to either do nothing or give you something when read. The thing is, the body isn't around so to support what you're doing, just becomes the same but doing it through the Transform. This kind of fallback behaviour is messy and prone to producing differences. For instance, rotation. Rotation from the Transform Z is likely to return slightly different values than the Rigidbody2D Z rotation so a new fallback then gets reported as a bug etc etc,

    Simulated being false doesn't destroy the physics body which is why it works and there are no fallbacks. This is what you should use instead of deactivating a body because it's super quick.

    You deactive it but you want everything available as if it were active. So this isn't a bug, it's intentional behaviour. We can accept it and change it but then we've got to do that for all other related methods then eventually we hit something we cannot easily create a fallback for and that becomes a discontinuity.

    In my mind here, if you want stuff relative to the Transform then you're better off doing that.
     
  3. aegis123321

    aegis123321

    Joined:
    Jul 5, 2015
    Posts:
    71
    Thanks for reply.
    I understand it's kind of fallback behaviours,

    but you already returned from Transform Z?

    I don't see any continuity in current approach since it returned position and rotation through Transform but didn't do with those method related position and rotation.
    If it's just the thing that you're not able to change for any reason, I hope the Documentation notes it for anyone like me
     
  4. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    11,491
    Rotation Z? Actually the Rigidbody2D rotation can have multiple windings (Transform cannot) and it can also not be the same as the Transform when interpolation is on. Also, it's not as simple as taking the Z rotation part (Euler) of the Quaternion. We need to calculate an "effective" Z rotation cancelling out other XY rotations and do that with the same precision and match it to the (Box2D) body. Nothing is as simple as we present to you unfortunately and whilst you might not care about this stuff but do understand it, lots of other devs will report it as a bug and won't.

    You can always link one thing with something else so in theory, continuity means supporting everything.

    Yes, I've noted some things that will be added to the next round of docs.

    To be clear though, I'm not necessarily saying we cannot do it and maybe we'll just take a step back and say yes, let's add that fallback but limit it to these few methods and then add docs for other stuff. I just felt it necessary to explain why you get what you get and that it is intended behaviour. A bug report returned as "By Design" just leaves unanswered questions.
     
  5. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    11,491
    I've asked QA to reactivate this bug report and I'll look at how best to add this functionality back in. I will add that it's unlikely to get validated for too many backports, especially into LTS. This part isn't my decision but we'll see.
     
    aegis123321 likes this.
  6. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    11,491
    I've added a fallback for GetPoint, GetRelativePoint, GetVector & GetRelativeVector that uses the Transform is the body is not around and tries to use as close a code-path as possible which, in theory, should give identical results for a majority of use-cases. All tests are done and I've pushed it for QA test. I have only pushed it for 2022.1, 2021.2 & 2021.1 so anything before that will be out of my hands.

    Here's the Public Tracker but it might take a few hours for it to be updated to reflect the state of fixes and versions etc.

    Hope this helps.
     
    aegis123321 likes this.