Search code examples
luacalculusangle

Yaw, pitch, and roll rotations to six float variables


I need help creating a function to convert three angles (in degrees, yaw pitch and roll) to six float variables.

How would I go about making a function output these floats?

  • {0, 0, 0} = {1, 0, 0, -0, -0, 1}
  • {45, 0, 0} = {0.70710676908493, 0.70710676908493, 0, -0, -0, 1}
  • {0, 90, 0} = {-4.3711388286738e-08, 0, 1, -1, 0, -4.3711388286738e-08}
  • {0, 0, 135} = {1, -0, 0, -0, -0.70710676908493, -0.70710676908493}
  • {180, 180, 0} = {1, -8.7422776573476e-08, 8.7422776573476e-08, 8.7422776573476e-08, 0, -1}
  • {225, 0, 225} = {-0.70710682868958, 0.5, 0.5, -0, 0.70710670948029, -0.70710682868958}
  • {270, 270, 270} = {1.4220277639103e-16, -2.3849761277006e-08, 1, 1, 1.1924880638503e-08, 1.42202776319103e-16}
  • {315, 315, 315} = {0.5, -0.85355341434479, 0.14644680917263, 0.70710688829422, 0.5, 0.5}

MORE EXAMPLES REQUESTED BY: Egor Skriptunoff

  • {10, 20, 30} = {0.92541658878326, -0.018028322607279, 0.37852230668068, -0.34202012419701, -0.46984630823135, 0.81379765272141}
  • {10, 30, 20} = {0.85286849737167, -0.0052361427806318, 0.52209949493408, -0.5, -0.29619812965393, 0.81379765272141}
  • {20, 10, 30} = {0.92541658878326, 0.21461015939713, 0.3123245537281, -0.17364817857742, -0.49240386486053, 0.85286849737167}
  • {20, 30, 10} = {0.81379765272141, 0.25523611903191, 0.52209949493408, -0.5, -0.15038372576237, 0.85286849737167}
  • {30, 10, 20} = {0.85286849737167, 0.41841205954552, 0.3123245537281, -0.17364817857742, -0.33682405948639, 0.92541658878326}
  • {30, 20, 10} = {0.81379765272141, 0.4409696161747, 0.37852230668068, -0.34202012419701, -0.16317591071129, 0.92541658878326}

The code I currently have can calculate all of the floats except the 2nd and 3rd.

function convert_rotations(Yaw, Pitch, Roll)
    return {
        math.cos(math.rad(Yaw))*math.cos(math.rad(Pitch)),
        0,
        0,
        math.sin(math.rad(Pitch))*-1,
        math.sin(math.rad(Roll))*math.cos(math.rad(Pitch))*-1,
        math.cos(math.rad(Roll))*math.cos(math.rad(Pitch))
    }
end

I cannot seem to find the correct math for when all angles are nonzero for the 2nd float and 3rd float, but I did come up with this:

-- The second float when the Yaw is 0 degrees
math.sin(math.rad(Pitch))*math.sin(math.rad(Roll))*-1

-- The second float when the Pitch is 0 degrees
math.sin(math.rad(Yaw))*math.cos(math.rad(Roll))

-- The second float when the Roll is 0 degrees
math.sin(math.rad(Yaw))*math.sin(math.rad(Pitch))

And for the 3rd float I came up with this:

-- The third float when Yaw is 0 degrees
math.sin(math.rad(Pitch))*math.cos(math.rad(Roll))

-- The third float when Pitch is 0 degrees
math.sin(math.rad(Yaw))*math.sin(math.rad(Roll))

-- The third float when Roll is 0 degrees
math.cos(math.rad(Yaw))*math.sin(math.rad(Pitch))

Solution

  • local function Rotate(X, Y, alpha)
        local c, s = math.cos(math.rad(alpha)), math.sin(math.rad(alpha))
        local t1, t2, t3 = X[1]*s, X[2]*s, X[3]*s
        X[1], X[2], X[3] = X[1]*c+Y[1]*s, X[2]*c+Y[2]*s, X[3]*c+Y[3]*s
        Y[1], Y[2], Y[3] = Y[1]*c-t1, Y[2]*c-t2, Y[3]*c-t3
    end
    
    local function convert_rotations(Yaw, Pitch, Roll)
        local F, L, T = {1,0,0}, {0,1,0}, {0,0,1}
        Rotate(F, L, Yaw)
        Rotate(F, T, Pitch)
        Rotate(T, L, Roll)
        return {F[1], -L[1], -T[1], -F[3], L[3], T[3]}
    end