How To Find Pitch/Yaw from a CFrame?

say I constructed a CFrame like this:

local cframe=CFrame.Angles(pitch,0,0)*CFrame.Angles(0,yaw,0)

If I didn’t know what the pitch and yaw were, but I had the complete cframe, how could I figure the pitch and yaw out so I could form a cframe like above?
Doing this because im tryna make a camera system that can be enabled/disabled and can transition from the regular camera. Pitch and yaw are determined directly through the InputChanged event, but I need to find the camera’s original cframe as if it were constructed the same way as my camera system, and I’m not exactly sure how to do that. :confused: Any help would be appreciated!

2 Likes

local cframe=CFrame.Angles(1,0,0)*CFrame.Angles(0,2,0) local x,y,z=cframe:ToOrientation(cframe) print(x,y,z)

^tried something like this, doesnt work at all. should return 1 and 2 for x and y but it returns very different numbers

local pitch, yaw, roll = CFrame:ToEulerAnglesYXZ()

EDIT: Should be XYZ, not YXZ.

doesnt seem to work,

I figured out I can get pitch like this tho:

local pitch=math.asin(camera.CFrame.lookVector.y)

question is how 2 get yaw?

edit
wait nvm, im so confused

yea ik but this is what the results were:

I had a cframe constructed like this:
local cframe=CFrame.Angles(1,0,0)*CFrame.Angles(0,2,0)
so the pitch should be 1 radian, the yaw 2 radians right?

print(cframe:ToEulerAnglesYXZ())
^this prints in output “-0.35775843262672 1.8132071495056 0.95596045255661”

so something isnt right :confused:

By default the numbers are in radians, and not in degrees.
To convert them to degrees you can simply multiply by 180 and then divide by pi.

local x,y,z = CFrame:ToOrientation()
local pitch,yaw,roll = x*180/math.pi,y*180/math.pi,z*180/math.pi

Try

local pitch, yaw = CFrame:ToEulerAnglesXYZ()

doesnt seem to work either,

should return 1 for pitch and 2 for yaw, but gives this: -2.1415927410126 1.1415926218033

You could alternatively create a CFrame from yaw and pitch by doing,
local cf = CFrame.Angles(0, yaw, 0)*CFrame.Angles(pitch, 0, 0)
and make sure yaw is between -math.pi and math.pi
and make sure pitch is between -math.pi/2 and math.pi/2

(This should be easy to visualize too.)

Then from there, cf:toEulerAnglesYXZ() will return back pitch and roll as put in.

2 Likes

so is there no other way 2 do it how i described?

ok yea, your way seems to work i think, thanks

Yes there are other ways to do it using some trig. math.atan2(Z, X) should give you the yaw.