How does Roblox Convert the orientation of a part from (180,0,0) to (0,180,180)

This is a question on how Roblox handles Orientation:

From this script, I change the orientation of the part from: (0,0,0) to: (180,0,0)

local Orientation = Vector3.new(180,0,0)

script.Parent.Orientation = Orientation

print(script.Parent.Orientation)

print(Orientation)

But after setting the Part’s orientation, I end up getting a different Vector3 value than what I originally set.

Print2180

This clearly shows that I have set (180,0,0) as the orientation, but Roblox automatically converts this into (0,180,180) instead.

Both Vector3 values at the end do the same thing, but I want to know how Roblox formulates, or what their equation is, to convert a part’s orientation from (180,0,0) to (0,180,180),

—pls help

1 Like

Internally, ROBLOX doesn’t store those three values (the XYZ orientation). It stores a part’s position and rotation in a single 4x4 matrix – a coordinate frame (CFrame).

Both (180, 0, 0) and (0, 180, 180) represent the exact same orientation because they represent the exact same rotation matrix.

When you print the orientation, roblox calculates the roll, pitch, and yaw from the CFrame (either on-the-fly or it’s cached).

That calculation, according to this page, probably looks something like this (but they write it in c :slight_smile: ):

-- (untested, might have gotten the order of things mixed up)
-- Calculates pitch, yaw, and roll from a CFrame
-- and returns them in that order.
-- EDIT: this returns radians, not degrees. So pass the results through math.deg
-- if you wanted to compare directly.
function GetOrientationXYZ(cframe)
    -- get the matrix (see https://developer.roblox.com/en-us/articles/CFrame-Math-Operations)
    local x, y, z, r11, r12, r13, r21, r22, r23, r31, r32, r33 = a:components();

    -- get pitch (x-axis)
    local alpha = math.atan2(r21 / r11);

    -- get yaw (y-axis)
    local beta = math.atan2(-r31 / math.sqrt(r32*r32 + r33*r33));

    -- get roll (z-axis)
    local gamma = math.atan2(r32 / r33);

    return alpha, beta, gamma
end

It just so happens that the way that formula spits out angles isn’t the same ones you gave it.

edit: How do they convert the roll, pitch, and yaw that you give into a CFrame in the first place? Probably something like the “Tait-Bryan angles” column in this table.

6 Likes

Thank you! this is epic
you were the only one who responded lol

No problem - just curious, do you need this for something or are you just curious?

Is it possible to get back the original orientation? Like instead of the (0, 180, 180), it returns the simple (180, 0, 0)? Because I’ve always been annoyed with the way this works and I keep finding like wacky methods to fix it, and then I forget how I did them.

No, you can’t get the input orientation back from the part properties, that information is lost in the conversion. You could store the yaw, pitch, roll angles yourself, and manually make sure to keep them in sync with the part.