0

I have searched all across the forums including stack overflow. I found a solution that seems to work for XYZ but its only using 9 of the possible 12 parts of the matrix which I believe is the scaling part and would appreciate a little help as to how I could add that to the following code, which is a hybrid of c and basic known as AGK.

dim Matrix [3,3] as float
Matrix=EulerAnglesToMatrix(Eular,ORDER_XYZ)

function EulerAnglesToMatrix(inEulerAngle as EularAngle,EulerOrder as integer)

    // Convert Euler Angles passed in a vector of Radians
    // into a rotation matrix.  The individual Euler Angles are
    // processed in the order requested.
    dim Mx [3,3] as float
//Mx as Matrix3

    Sx as float
    Sy as float
    Sz as float
    Cx as float
    Cy as Float
    Cz as float
    


    Sx    = sin(inEulerAngle.X)
    Sy    = sin(inEulerAngle.Y)
    Sz    = sin(inEulerAngle.Z)
    Cx    = cos(inEulerAngle.X)
    Cy    = cos(inEulerAngle.Y)
    Cz    = cos(inEulerAngle.Z)

    select (EulerOrder)
    
    case ORDER_XYZ:
        Mx[0,0]=Cy*Cz
        Mx[0,1]=-Cy*Sz
        Mx[0,2]=Sy
        Mx[1,0]=Cz*Sx*Sy+Cx*Sz
        Mx[1,1]=Cx*Cz-Sx*Sy*Sz
        Mx[1,2]=-Cy*Sx
        Mx[2,0]=-Cx*Cz*Sy+Sx*Sz
        Mx[2,1]=Cz*Sx+Cx*Sy*Sz
        Mx[2,2]=Cx*Cy
        endcase

    case ORDER_YZX:
        Mx[0,0]=Cy*Cz
        Mx[0,1]=Sx*Sy-Cx*Cy*Sz
        Mx[0,2]=Cx*Sy+Cy*Sx*Sz
        Mx[1,0]=Sz
        Mx[1,1]=Cx*Cz
        Mx[1,2]=-Cz*Sx
        Mx[2,0]=-Cz*Sy
        Mx[2,1]=Cy*Sx+Cx*Sy*Sz
        Mx[2,2]=Cx*Cy-Sx*Sy*Sz
      endcase

    case ORDER_ZXY:
        Mx[0,0]=Cy*Cz-Sx*Sy*Sz
        Mx[0,1]=-Cx*Sz
        Mx[0,2]=Cz*Sy+Cy*Sx*Sz
        Mx[1,0]=Cz*Sx*Sy+Cy*Sz
        Mx[1,1]=Cx*Cz
        Mx[1,2]=-Cy*Cz*Sx+Sy*Sz
        Mx[2,0]=-Cx*Sy
        Mx[2,1]=Sx
        Mx[2,2]=Cx*Cy
      endcase

    case ORDER_ZYX:
        Mx[0,0]=Cy*Cz
        Mx[0,1]=Cz*Sx*Sy-Cx*Sz
        Mx[0,2]=Cx*Cz*Sy+Sx*Sz
        Mx[1,0]=Cy*Sz
        Mx[1,1]=Cx*Cz+Sx*Sy*Sz
        Mx[1,2]=-Cz*Sx+Cx*Sy*Sz
        Mx[2,0]=-Sy
        Mx[2,1]=Cy*Sx
        Mx[2,2]=Cx*Cy
      endcase

    case ORDER_YXZ:
        Mx[0,0]=Cy*Cz+Sx*Sy*Sz
        Mx[0,1]=Cz*Sx*Sy-Cy*Sz
        Mx[0,2]=Cx*Sy
        Mx[1,0]=Cx*Sz
        Mx[1,1]=Cx*Cz
        Mx[1,2]=-Sx
        Mx[2,0]=-Cz*Sy+Cy*Sx*Sz
        Mx[2,1]=Cy*Cz*Sx+Sy*Sz
        Mx[2,2]=Cx*Cy
      endcase

    case ORDER_XZY:
        Mx[0,0]=Cy*Cz
        Mx[0,1]=-Sz
        Mx[0,2]=Cz*Sy
        Mx[1,0]=Sx*Sy+Cx*Cy*Sz
        Mx[1,1]=Cx*Cz
        Mx[1,2]=-Cy*Sx+Cx*Sy*Sz
        Mx[2,0]=-Cx*Sy+Cy*Sx*Sz
        Mx[2,1]=Cz*Sx
        Mx[2,2]=Cx*Cy+Sx*Sy*Sz
      endcase 
    endselect

endfunction (Mx)

function MatrixToEular(mt as matrix4)
    f as EularAngle
    
    //We dont have any epsilon so just use 0.999
    if mt.x.z < -0.999
        //gimbal lock.
        f.x = 0
        f.y = MPI/2
        f.z = ATan2(mt.y.x,mt.z.x)
    elseif mt.x.z > 0.999
        f.x = 0
        f.y = -MPI/2
        f.z = ATan2(mt.y.x,mt.z.x)
    else
        f.x = -asin(mt.x.z)
        f.y = atan2( mt.y.z / cos(f.x), mt.z.z / cos(f.x))
        f.z = atan2( mt.x.y / cos(f.x), mt.x.x / cos(f.x))
    endif
    
    f.scaleX = sqrt(mt.x.x*mt.x.x + mt.x.y*mt.x.y + mt.x.z*mt.x.z)
    f.scaleY = sqrt(mt.y.x*mt.y.x + mt.y.y*mt.y.y + mt.y.z*mt.y.z)
    f.scaleZ = sqrt(mt.z.x*mt.z.x + mt.z.y*mt.z.y + mt.z.z*mt.z.z)
    
endfunction f

Thanks for any help and direction/ assistance anyone can give me

output what I fail to understand without messing up the order

I should be able to convert to Eular and then back to a matrix and the bottom group of values should be equal to the original

please see image and revised post

  • A (3D) rotation matrix is only 3x3. If you want a 3x4 matrix, you probably also want to encode the translation, but you can't get this information from Euler angles. – chtz Feb 28 '23 at 15:27
  • Thanks can you point me in the right direction to how ide go about encoding the translation. I have been thinking it was w like in quats but im not sure and would appreciate a little guidance. – Peter Kennedy Mar 01 '23 at 19:05

0 Answers0