Relative Degree Rotation

How can I get the “direction” (such as front, back, right, and left) of an object relative to another object. So lets say I want to get the rotation of Part A, relative to Part B. If Part B’s orientation changes so does the 0 degree axis for Part A.
I calculated global directions with the code shown below, but how can I get the rotation of one object relative to another?

local v = Camera.CFrame.LookVector
print(math.floor(math.deg(math.atan2(v.X, v.Z))+180))

Also, in my case I am trying to get the relative rotation of the Camera around the HumanoidRootPart in player.
Here is a quick image I drew up to help explain my situation:

1 Like

I believe EgoMoose has a great explanation on the inner workings of this method on Scripting Helpers but Roblox has a built-in function for object space calculation. Using CFrame.toObjectSpace() or CFrame.toWorldSpace(), you can retrieve a relative rotation.

Yes but, I am a little confused when it comes to that method could you provide an example?
The wiki doesn’t provide an example of ToObjectSpace.

Unfortunately I couldn’t find EgoMoose’s example, but this one should be able to help you. I’ll do my own research and see if I can get you the mathematics.

EDIT: Knowing what I do about math, I understand you can get the relative position of an object by subtracting the position of one object minus the other. I would recommend trying that with an Orientation vector (or a CFrame) and then seeing if that works.

EDIT #2: Turns out that’s exactly what toObjectSpace() does…

2 Likes
Camera.CFrame:VectorToObjectSpace(aLookVector) --convert world space lookvector to be relative to camera space

otherwise, you can use atan2 on both lookvectors and try:

(ang2 - ang1 + pi) % (2*pi)  - pi --to keep it between -pi and pi (-180° and 180°)
2 Likes