CFrame math help

I’m trying to get the rotation of a part relative to another part. I’m mainly trying to get the rotation around the Y axis. Here is how I’m doing it:

local rel = workspace.A.CFrame:Inverse() * workspace.B.CFrame
print(rel:ToEulerAnglesXYZ())

This works perfectly fine if rotated like this:

0.52359879016876

But if I rotate the block around it’s X axis, it prints this:

Notice how the rotation along the Y axis is now 0.41644218564034 instead of 0.52359879016876. Is there any way I can still get a rotation of 0.52359879016876 on the Y axis no matter how much it is rotated around the X, or Z axis?

Test.rbxl (23.2 KB)

I suggest adding CFrame.Angles(0, math.rad(deg), 0) (deg is the degrees you want to rotate it with) to the part’s CFrame.
If you want to rotate it to rotate a model perspectively to a part you could use Model:SetPrimaryPartCFrame().

Now, to rotate the part along the world Y axis we would need to move the CFrame.Angles to a world space:

part.CFrame = CFrame.Angles(0,math.rad(20),0):ToObjectSpace(part.CFrame)

I think managed to fix it by using this:

Text.rbxl (23.2 KB)

workspace.A.Weld.C0 = CFrame.Angles(0, math.rad(45), 0) * CFrame.Angles(math.rad(45), 0, 0)

print((workspace.A.Weld.C0 * CFrame.Angles(math.rad(45), 0, 0):Inverse()):ToEulerAnglesXYZ())

workspace.A.Weld.C0 = CFrame.Angles(0, math.rad(45), 0)-- * CFrame.Angles(math.rad(45), 0, 0)

print(workspace.A.Weld.C0:ToEulerAnglesXYZ())

These two print almost the same exact angle. But I’m assuming that floating point errors make these not print the exact same number.

If anyone has a better way of doing this, please let me know! :grin:

Ignore the fact that I’m using welds.