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. Dismiss Notice

How to use Vector3.OrthoNormalize properly?

Discussion in 'Scripting' started by Fuby, Feb 25, 2016.

  1. Fuby

    Fuby

    Joined:
    Mar 26, 2014
    Posts:
    35
    Greetings,

    I`m currently trying to write a function to "orthogonalize" a Matrix. To accomplish that I thought I could extract the vectors of my matrix, use Vector3.OrthoNormalize(ref v0, ref v1, ref v2) and stuff them back into the matrix.

    Note: I got that example from the Unity API: http://docs.unity3d.com/ScriptReference/Vector3.OrthoNormalize.html

    But all it does is either nothing or set everything but [2,2] and [3,3] to 0. [2,2] and [3,3] will be set to 1 in that case(its pretty obvious for [3,3], since I´m personally setting it to that value). Sometimes even get some pretty random values like -0.345345.

    Am I missing something? Is there even any sense in what I´m trying to accomplish?

    Code (CSharp):
    1. public Matrix4x4 matrixOrthogonal(Matrix4x4 matrix)
    2.     {
    3.         //Extract and orthogonalize vectors
    4.         Vector3 v0 = matrix.GetColumn(0);
    5.         Vector3 v1 = matrix.GetColumn(1);
    6.         Vector3 v2 = matrix.GetColumn(2);
    7.         Vector3.OrthoNormalize(ref v0, ref v1, ref v2);
    8.  
    9.         Matrix4x4 matrixOrtho = new Matrix4x4();
    10.         matrixOrtho.SetColumn(0, v0);
    11.         matrixOrtho.SetColumn(0, v1);
    12.         matrixOrtho.SetColumn(0, v2);
    13.         matrixOrtho[3, 3] = 1.0F;
    14.  
    15.         /*
    16.         float stretchFactor = 1.0f;
    17.         Matrix4x4 scale = new Matrix4x4();
    18.         scale[0, 0] = stretchFactor;
    19.         scale[1, 1] = 1.0F / stretchFactor;
    20.         scale[2, 2] = 1.0F / stretchFactor;
    21.         scale[3, 3] = 1.0F;
    22.         Matrix4x4 fromNewSpace = toNewSpace.transpose;
    23.         Matrix4x4 matrixOrtho = toNewSpace * scale * fromNewSpace;
    24.         */
    25.  
    26.         Debug.Log("Matrix: " + "\n" + matrix);
    27.         Debug.Log("Matrix orthogonal: " + "\n" + matrixOrtho);
    28.  
    29.         return matrixOrtho;
    30.     }
     
  2. bigmisterb

    bigmisterb

    Joined:
    Nov 6, 2010
    Posts:
    4,221
    Code (csharp):
    1.  
    2.         Matrix4x4 matrixOrtho = new Matrix4x4();
    3.         matrixOrtho.SetColumn(0, v0);
    4.         matrixOrtho.SetColumn(0, v1);
    5.         matrixOrtho.SetColumn(0, v2);
    6.         matrixOrtho[3, 3] = 1.0F;
    7.  
    shouldn't that be...

    Code (csharp):
    1.  
    2.         Matrix4x4 matrixOrtho = new Matrix4x4();
    3.         matrixOrtho.SetColumn(0, v0);
    4.         matrixOrtho.SetColumn(1, v1);
    5.         matrixOrtho.SetColumn(2, v2);
    6.         matrixOrtho[3, 3] = 1.0F;
    7.  
     
    StarManta likes this.
  3. Fuby

    Fuby

    Joined:
    Mar 26, 2014
    Posts:
    35
    Yep thanks :p I just figured that out after hours of frustration, thanks though. Case closed.