I want to ask about ToWorldSpace and ToObjectSpace

Is ToWorldSpace same as adding two CFrame and ToObjectSpace is same as subtracting two CFrame?

Looking into it, I can’t see it doing anything relevant even in testing.

If you want to do something that involves world space, you can just add a Vector3 to it.

To answer your question, adding two CFrames is not the same as ToWorldSpace and substracting two CFrames is not the same as ToObjectSpace. ToWorldSpace and ToObjectSpace are more specific operations that deal with transforming coordinates in relation to a specific CFrame. Adding and subtracing CFrames will generally produce a new CFrame that represents the combination of the two original CFrames.

Example

local cf1 = CFrame.new(10, 0, 0)
local cf2 = CFrame.new(0, 5, 0)

-- Get the position of cf2 in cf1's coordinate space
local cf2InCf1Space = cf1:ToObjectSpace(cf2).Position
print(cf2InCf1Space) -- Output: Vector3(-10, -5, 0)

-- Get the position of cf2 in world space
local cf2InWorldSpace = cf1:ToWorldSpace(cf2).Position
print(cf2InWorldSpace) -- Output: Vector3(10, 5, 0)

In this example, we have two CFrame values: cf1 and cf2. We use ToObjectSpace to get the position of c2 in the coordinate space of cf1 and we use the ToWorldSpace to get the position of cf2 in world space.

In ToObjectSpace, we subtract cf1 from cf2 to get a new CFrame value that represents cf2 in cf1's coordinate space. Then we acces the Position property of the resulting CFrame to get the position of cf2 relative to cf1.

In ToWorldSpace, we add cf1 and cf2 together to get a new CFrame value that represents cf2 in world space. Then we access the Position property of the resulting CFrame to get the absolute position of cf2 in the game world.

I believe ToWorldSpace just offsets a CFrame by another CFrame. ToObjectSpace is a little different. e.g. cf1:ToObjectSpace(cf2) will give you a CFrame back such that, if you were to offset cf1 by the result, you would get cf2. In other words, cf1:ToWorldSpace(cf1:ToObjectSpace(cf2)) == cf2.

when I can use these two methods?

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.