CFrame ToWorldSpace and ToObjectSpace function

Some people said that the CFrame:ToWorldSpace and CFrame:ToObjectSpace is important, but I don’t really know how to use it or even when I should use it. I believe they are called CFrame Matrix, but I don’t know anything about it. I remember someone said about making bread sliced into 2 parts could use the ToObjectSpace function.

I only know that World space is basically the CFrame of the world and the Object space is the CFrame of a specific object.

I’ve did some research on google, read the DevHub, watched some Youtube videos about it. And I still have hard time understanding it. But, like after I did those researches, I got this:

``````local Part = workspace:WaitForChild("Part")

local A = workspace:WaitForChild("A")

print(Part.CFrame:ToWorldSpace(A.CFrame)) -- prints how far is it (on the x, y, z axis) from the center of the world/ prints the position relative to the center of the world,
-- subtract the World CFrame from the Part CFrame
-- A.CFrame will be relative to the Part.CFrame

print(Part.CFrame:ToObjectSpace(A.CFrame)) -- treating the Part as the center of the world
-- subtract the Part CFrame from the A CFrame
``````

I also don’t know how to use the parameter for the function too and how is ToWorldSpace() the same as CFrame * CF. Also, how is ToObjectSpace() the same as CFrame:Inverse() * CF

1 Like

It’s all about being relative to other things.

In “world space”, the CFrame represents the position and rotation from the origin point (position 0,0,0 with 0 rotation). In “object space”, the CFrame represents position and rotation from the position and rotation of another CFrame.

Here’s an example. Put some object on your desk. What is its position and rotation? Well, that’s a weird question to ask. It needs to be relative to another object to answer that question. The position and rotation relative to your desk would be an example. This is so-called object space.

A CFrame by itself doesn’t necessarily represent world or object space (although, by practice, we just assume world-space by default).

Example: Given the initial CFrame between two parts (`mainPart` and `offsetPart`), I want `offsetPart` to retain that exact same offset, relative to the CFrame of `mainPart`, even if `mainPart` moves or rotates. To go with the desk example, you can imagine that `mainPart` is the desk, and `offsetPart` is some object on the desk that needs to lock itself to the same position/orientation relative to the desk, even if the desk moves.

``````-- Goal: Always set 'offsetPart' to be 1 unit above 'mainPart', relative to 'mainPart's CFrame

local mainPart = workspace.MainPart
local offsetPart = workspace.OffsetPart

-- Convert 'offsetPart' CFrame into object space, relative to 'mainPart':
local relativeCFrame = mainPart.CFrame:ToObjectSpace(offsetPart.CFrame)

while true do
-- Convert relativeCFrame back to world-space by multiplying it with mainPart's current CFrame
offsetPart.CFrame = mainPart.CFrame:ToWorldSpace(relativeCFrame)
end
``````

As you can see, converting to object-space is usually an intermediate step. We do it to store relative positional/rotational information. But we will eventually need to convert this back to world-space most likely.

Most uses of CFrame in the Roblox API are based on world-space. An exception would be Attachments, which have a `CFrame` property representing object-space, and a separate `WorldCFrame` property representing world-space.

2 Likes

Wait relative, you mean like how far is it from the origin?

That would be world-space, yes. In object-space, we’re talking about how far away it is from another CFrame (including relative rotation).

Imagine you have two parts. `PartA` has a position of `0, 10, 0`. `PartB` has a position of `0, 12, 0`. That’s their world-space position. Relative to the origin. However, `PartB`’s relative position to `PartA` is `0, 2, 0`.

and what does offset mean? is it like how far away it is?

and if PartA’s relative position to Part B would be 0, -2, 0? or it is also 0, 2, 0?

Yup, it would be `0, -2, 0`. The relative position should be able to get you back to the correct world position, assuming you apply it to the object that we’re relatively based upon (e.g. `PartB` in that case).

So `PartB`’s world position is `0, 12, 0`, so if we add our `0, -2, 0` vector, we get back to `0, 10, 0`, which is the original world position of `PartA`.

But how come in like Magnitude, it doesnt matter which one subtract to which one.

That doesn’t matter because of how Magnitude works:

``````magnitude = math.sqrt(vector.X ^ 2 + vector.Y ^ 2 + vector.Z ^ 2)
``````

When you square a number (e.g. `vector.X ^ 2`), you will always get a positive number. So `2^2` and `(-2)^2` both equal `4`.

The magnitude check to figure out the distance between vectors is fun too, because it’s just an alternative to the distance formula, which would do the same magnitude check, but would subtract the numbers inline in the square-root call:

``````distance = math.sqrt((v2.X - v1.X) ^ 2 + (v2.Y - v1.Y) ^ 2 + (v2.Z - v1.Z) ^ 2)
``````

And thus we can conclude that the distance between two vectors is equal to the magnitude of their difference!

How would you applicate CFrame ToWorldSpace() and ToObjectSpace() real use?

Like how useful are they and when should we use them?

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