Questions about :ToWorldSpace/ToObjectSpace?

I just had some quick questions about to world space/to object space, if I put a cframe as the parameter what does that do?

example:
local CF = workspace.Part.CFrame

print(CFrame:ToWorldSpace(CF))

question 2, if I call ToWorldSpace once, do I need to call it again to move a new part?

example:
local CF = workspace.Part.CFrame

Part2.CFrame = CFrame:ToWorldSpace(CF)

if I wanted to use ToWorldSpace on part3, would I have to call ToWorldSpace on CF again or could I just say part3.CFrame = CF?

final question, all the tutorials I have seen say that ToWorldSpace is relative to the origin, and ToObjectSpace is relative to the objects position, but when I use it, it seems to be the other way around, when I call ToObjectSpace it places stuff at the origin, when I use ToWorldSpace it places at the object. anyone know why?

thanks in advance, also if any of the code snippets I used are wrong please tell me as I am still a beginner and did not test any of it before hand.

1 Like

Both ToWorldSpace and ToObjectSpace are methods on individual CFrame objects, not the CFrame type as a whole. This means you have to have 2 constructed CFrames to use the methods.

CFrame:ToWorldSpace is the exact same as multiplying 2 CFrames together. CFrame:ToObjectSpace allows you to get the offset (rotational and translational) the second CFrame is from the first one.

What exactly is your goal with using these methods?

3 Likes

ToWorldSpace takes in a cframe and offsets it relative to the first cframe. For example, Part2.CFrame:ToWorldSpace(CFrame.new(0, 1, 0)) would return a cframe that is one stud relatively higher than Part2

ToObjectSpace basically does the opposite of that. It returns a cframe that represents the offset of the cframe in the parameter to the first one. Part2:ToObjectSpace(Part3.CFrame) returns a cframe that represents Part3’s cframe relative to Part2’s

1 Like

I am making a placement tool, I made it so that when you click a part 6 arrows appear, (one on each side), but it did not work when you rotated the part, so someone told me to use ToWorldSpace, when I did the first part was placed correctly, the second was a little off, the third even more off, every arrow I placed got progressively more wonky till the 5th one, I could not even find the fifth one it was so far away. I will provide the code, give me a sec to open roblox studio.

code:

local Part = script.Parent

local Pos = Part.CFrame

local Size = Part.Size

local Arrow = Instance.new("Part")

Arrow.Parent = script.Parent

local SizeX = Size.X * 1/2

Arrow.Anchored = true

local FinalPos = SizeX + 1

Arrow.CFrame = script.Parent.CFrame:ToWorldSpace(CFrame.new(FinalPos, 0, 0))

Arrow.Size = Vector3.new(0.5, 0.5, 0.5)

local Part = script.Parent

local Pos = Part.CFrame

local Size = Part.Size

local Arrow = Instance.new("Part")

Arrow.Parent = script.Parent

local SizeX = Size.X * 1/2

Arrow.Anchored = true

local FinalPos = SizeX - 1

Arrow.CFrame = script.Parent.CFrame:ToWorldSpace(CFrame.new(FinalPos, 0, 0))

Arrow.Size = Vector3.new(0.5, 0.5, 0.5)

If you want your arrows to appear like this


you can just use basic CFrame transformation.

Assuming your arrow part is pointing straight upwards when it has an orientation of 0,0,0:

-- Up arrow --
arrow.CFrame = part.CFrame * CFrame.new(0, 3, 0) -- 3 is the distance from the part the center of your arrow part will be

-- Down arrow
arrow.CFrame = part.CFrame * CFrame.new(0, -3, 0) * CFrame.Angles(math.pi, 0, 0) -- math.pi is the radian equivalent of 180 degrees
1 Like

Ok, just to be clear, multiplying cframes is the same as :toworldspace right?

heres your answer on the What is tobjectspace question

Thanks, we have the same questions lol.

Yes. The reason why it’s called “ToWorldSpace” is because you’re giving it an origin CFrame (the first one) and an offset CFrame (the second one) and you’re trying to get the result of that in the space of the world.

“ToObjectSpace” has you give it an object’s CFrame (the first one) and a world-space CFrame (the second one) and gives you the offset between the first and second one. In other words, it gives you the offset of that second CFrame relative to your “object” (first CFrame).

CFrames are actually really cool if you think of :Inverse()ing a CFrame as putting a negative sign in front of a number. You can also think of the * (translation) symbol as addition, although you have to be careful with it because when translating CFrames it’s not commutative. What that means is a * b ~= b * a.

The idea about thinking of CFrames as numbers with a few odd quirks exists, and sometimes really complicated problems can be broken down into some basic addition and subtraction problems!

Here’s the reason why the wiki says “ToObjectSpace” is the same as a:Inverse() * b. If you’re trying to get from the number 3 (a) to the number 5 (b), you offset 3 by +2. In other words, -3 + 5 = 2. Using addition/subtraction you can get that answer multiple different ways (5 - 3 = 5 + (-3) = 2), however since CFrames aren’t commutative it has to go in a specific order.

This probably isn’t the way they were designed to be worked with, but it usually helps me in some weird cases haha

10 Likes

Thanks, I clearly need more practice with CFrames, but you explained it very well. That reply was very helpful.

2 Likes