# Calling relative offset (CFrame) between parts?

I want to check what the offset of b (green) is relative to a (red). I thought this worked like this:

``````local Offset = workspace.a.CFrame / workspace.b.CFrame
``````

But this of course errors. How exactly can you get the position and rotation relative to an anchor point?

4 Likes

This method is the one you want;
http://wiki.roblox.com/index.php/CFrame#toObjectSpace

It returns the offset of b relative to a using
`a:toObjectSpace(b)`

5 Likes

You can try a.CFrame * b.CFrame:inverse()
That should give you the desired result.

2 Likes

Is there a way to use the stored offset and applying rotations to it?
For example:

``````local offset = a.CFrame * b.CFrame:inverse()
b.CFrame = a.CFrame * offset
``````

I tried it in this case and it resulted like so:

``````local target = workspace.Target
local offset = target.a.CFrame * target.b.CFrame:inverse()
print(offset)
-- -34.6450272, -93.4899979, -17.1021347, 0.707106829, -0.707106829, 0, 0, 0, -1, 0.707106829, 0.707106829, 0
target.b.CFrame = target.a.CFrame * offset
``````

Have a look at the link I sent. It will work correctly if you use this;

``````local offset = a.CFrame:toObjectSpace(b.CFrame)
b.CFrame = a.CFrame * offset
``````

T̶h̶e̶ ̶m̶a̶t̶h̶ ̶t̶h̶a̶t̶ ̶J̶a̶r̶o̶d̶ ̶w̶a̶s̶ ̶t̶a̶l̶k̶i̶n̶g̶ ̶a̶b̶o̶u̶t̶ ̶s̶h̶o̶u̶l̶d̶ ̶b̶e̶ ̶t̶h̶e̶ ̶o̶t̶h̶e̶r̶ ̶w̶a̶y̶ ̶a̶r̶o̶u̶n̶d̶ ̶t̶o̶ ̶w̶o̶r̶k̶ ̶p̶r̶o̶p̶e̶r̶l̶y̶,̶ ̶i̶.̶e̶.̶;̶
I was totally wrong, his math was right

You can plug that back into your script and it should get you what you’re after, though I’d recommend the first method I suggested (toObjectSpace) as it’s an explicit operation as opposed to math (which isn’t as easily recognizable)

What do you mean about applying rotations? You can transform your part by some rotation as you could normally with any other CFrame, i.e.
b.CFrame = a.CFrame * offset * CFrame.Angles(0,0,math.pi/4)

14 Likes

When I first tried to figure it out, I got it to snap back in place but what would happen is the part would get put into a 0,0,0 rotation. So in case I needed parts to stay rotated the way they are at all time (for example with doors that recalculate all offsets of its parts relative to an anchor point) I had to find a way to keep their rotations intact.

CFrame:toObjectSpace() is working flawlessly. Thank you very much! Are there any other tricks with CFrames that are worth noting down?

1 Like

Glad you got it working! Sorry I edited that post like 5 times in the last 15 minutes lol

There’s so much involved with CFrames that it’s hard to pick any few things to remember in particular - I’d just recommend keeping the wiki page handy and having a read over the quick reference table if there’s something in particular you’re after. The more you use them the more familiar you’ll get.

If you do want to read up a bit more there’s a lot of good mathy articles to go through on the wiki, though fair warning they do get fairly involved. This one is a good place to start if you’re interested

3 Likes

Thank you very much

1 Like