How would I calculate the orientation vector between two CFrames?

I want to find the orientation vector (the radian difference for X, Y, and Z) between two CFrames, and I couldn’t really find anything besides the degree between two vectors using the dot product. Can anyone point me towards the right direction? I think there are ways such as using ToOrientation() and subtracting the difference, but I feel like there should be a mathematical way to cut out the overhead. Thank you.

So I think what you’re kind of getting as is something like:

RotationCFrame * initialCFrame = finalCFrame

In which case you can find the rotation by calculating finalCFrame * initialCFrame:inverse()

It’s then just a case of transforming that rotation CFrame into the format you want, in this case it seems a vector and angle type thing.

1 Like

There isn’t a single solution between two 3D rotation matrices. Unless you would like to clarify more of what you’re doing, it sounds like you want interpolation.

local startCFrame = ?
local endCFrame = ?

local cFrameAtHalfwayPoint = startCFrame:lerp(endCFrame, 0.5)
1 Like

So you want to find the rotational difference between two CFrames? I think you can just use ToObjectSpace for that or rotation matrix inversing which is the math term for it I believe.

Yep like @McThor2 said but here is a script example.

--random/any/arbritrary cframes
local cframe1 = CFrame.fromOrientation(1,2,1)
local cframe2 = CFrame.fromOrientation(5,5,5)

local cframeRotationalDifference = cframe1:ToObjectSpace(cframe2)

local newCFrame = cframe1*cframeRotationalDifference

print(newCFrame)
print(cframe2)
print(cframeRotationalDifference:ToOrientation())

You will notice that the newCFrame calculated from the cframe rotational difference has numbers that are close to cframe 2 but are not exactly due to well floating point errors. Hence you can get the rotational difference between the two CFrames in terms of orientation via the ToOrientation() function:

print(cframeRotationalDifference:ToOrientation())

I believe it’s the shortest possible rotational difference as @MrNicNac said there should be multiple solutions to go from one rotation to another rotation but tbh I don’t understand the math and I just learnt from mixing and matching CFrames.

5 Likes