Search Unity

RigidBody undefined state [Bug]

Discussion in 'Scripting' started by sebas77, Nov 25, 2015.

  1. sebas77

    sebas77

    Joined:
    Nov 4, 2011
    Posts:
    1,643
    Hi,

    After days of research, I am finally able to reproduce an interesting issues both with Unity 4 and Unity 5. Apparently the RigidBody code doesn't have any safeguard check to protect itself from the application of very huge forces.
    When very huge forces are applied (usually due to singularities due to bugs), the Rigid Body game object is shot out of the Unity world where the Unity physic laws known are meaningless. Strange things start to happen. For example:

    from the Unity documentation, I can see that the following code is valid:

    Code (CSharp):
    1. transform.position = new Vector3(0, 0, 0);
    2. print(transform.position.x);
    my code just does this:

    Code (CSharp):
    1.  go.transform.position = Vector3.zero;
    2.   Debug.Log("***********************RESET TRANSFORM********" + go.transform.position.x + "*************" + go.transform.position.y + "**********" + go.transform.position.z);
    3.  
    4.   go.transform.position = b.center;
    5.   Debug.Log("------------------SET TRANSFORM----------" + go.transform.position.x + "-----------" + go.transform.position.y + "-----------" + go.transform.position.z);
    which usually works. However under the given reproduction circumstances, the console output is actually the following:

    Code (CSharp):
    1. ***********************RESET TRANSFORM********1.28849E+10*************-8.053088E+09**********-1.073742E+09
    2. ------------------SET TRANSFORM----------1.28849E+10------------8.053088E+09------------1.073742E+09
    During the Reset step, the position is actually not set to 0 (wuuut?)

    Now, this happens because the parent transform, where the RigidBody is, has also huge position value and in this case, probably, somehow, Unity code gets confused.

    If we reset manually the position of the RigidBody gameobject to 0,0,0, then everything works.

    So I wonder, wouldn't be wise to add some code to protect the Rigid Body from being set to this undefined state?
     
  2. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    8,775
    "Doctor, it hurts when I do this." "Well then, stop doing that!"

    I don't see how it's Unity's job to protect you from this. Sure, the application of huge forces is probably by mistake, but hiding the result won't make the mistake go away.
     
  3. sebas77

    sebas77

    Joined:
    Nov 4, 2011
    Posts:
    1,643
    yes I am all for early failures instead of defensive ifs, but then the Rigid Body code should throw a good exception, so I would have spent less time trying to investigate what is wrong.

    The problem is if it's considered an exception, but since Unity cannot handle it, I think it is.
     
  4. BenZed

    BenZed

    Joined:
    May 29, 2014
    Posts:
    524
    I don't see what the problem is here.

    '1.28849E+10' is an example of scientific notation.
    It's the same as writing 1.28849 * 10^10,
    or the same as writing 12'884'900'000.

    It isn't a bug, error or exception, that's just how they've chosen to format the string.
    Writing large numbers in this fashion is pretty standard in software.

    EDIT:

    I didn't read your original post correctly. I see what you're saying now. setting transform.position to Vector3.zero doesn't actually set it to zero. When-abouts are you doing this step? In FixedUpdate() or Update()?
     
    Last edited: Nov 25, 2015
  5. Kiwasi

    Kiwasi

    Joined:
    Dec 5, 2013
    Posts:
    16,860
    How huge are we talking. Anything physics related breaks down dramatically as you get further away from the origin. Floating point numbers just are not precise enough to do useful things.
     
  6. sebas77

    sebas77

    Joined:
    Nov 4, 2011
    Posts:
    1,643
    As BenZed realised, the problem is not really the fact that Unity understandably cannot manage properly such big numbers, but the fact that operations like setting the position of GameObjects, parented with the RigidBody GameObject, stop to work.
     
  7. Kiwasi

    Kiwasi

    Joined:
    Dec 5, 2013
    Posts:
    16,860
    Can you throw up a demo scene that does this? I'm still somewhat confused by your description.