Search Unity

Matrix4x4.TRS broken with non-identity Scales?

Discussion in 'Scripting' started by RibsNGibs, Apr 13, 2017.

  1. RibsNGibs

    RibsNGibs

    Joined:
    Jun 17, 2016
    Posts:
    15
    I can't imagine that something fundamental like this is bugged, so it must be my fault, but I don't know what it is.

    Code (csharp):
    1.  
    2.         Matrix4x4 test = Matrix4x4.TRS(Vector3.zero, Quaternion.identity, Vector3.one);
    3.         Debug.Log("trans: " + test.GetPosition());
    4.         Debug.Log("rot: " + test.GetRotation().eulerAngles);
    5.         Debug.Log("scale: " + test.GetScale());
    6.  
    results (correctly) in:
    trans: (0.0, 0.0, 0.0)
    rot: (0.0, 0.0, 0.0)
    scale: (1.0, 1.0, 1.0)

    but
    Code (csharp):
    1.  
    2.         Matrix4x4 test = Matrix4x4.TRS(Vector3.zero, Quaternion.identity, new Vector3(.5f,.5f,.5f));
    3.         Debug.Log("trans: " + test.GetPosition());
    4.         Debug.Log("rot: " + test.GetRotation().eulerAngles);
    5.         Debug.Log("scale: " + test.GetScale());
    6.  
    results in:
    trans: (0.0, 0.0, 0.0)
    rot: (18.0, 58.3, 58.3) <----- (!!!!)
    scale: (0.5, 0.5, 0.5)

    Am I being dumb here or is this busted?

    I'm in Unity 5.5.1f1.

    Thanks
     
  2. ThermalFusion

    ThermalFusion

    Joined:
    May 1, 2011
    Posts:
    906
    Those methods you use on the matrix are not built in are they? At least they are not in the online documentation.
    As far as I understand it is mathematically impossible to extract the rotation without knowing the scale from a matrix and vice versa, so I'm guessing the rotation extraction method assumes a uniform scale of 1.
     
  3. RibsNGibs

    RibsNGibs

    Joined:
    Jun 17, 2016
    Posts:
    15
    Huh, that's interesting. They (GetPosition/Rotation/Scale) seem built in to me - I did not add them. I don't know what method they are using but if there are no non-uniform scales/skews I think it should be mathematically possible to extract the rotation.

    In any case I will have to doublecheck my code to see if my original failure case (before I created the test case I posted) relied on GetRotation not.