How does :ToObjectSpace work?

I was just wondering how :ToObjectSpace works. I have tried doing some research on it but I’m still a little confused. Any help is appreciated.

2 Likes

CFrame:ToObjectSpace() is literally the exact same thing as CFrame:Inverse() * cf

2 Likes

for example, we have 2 parts, one named partA and partB respectively. partA is located at (0,5,0) while partB is located at (0,3,0).

partB.CFrame:ToObjectSpace(partA.CFrame)

this would return (0,2,0) since partA is 2 studs higher than partB.

edit: if you’re quite literally asking how it works behind the scenes then @remcodesremcodes is correct

5 Likes

With this Question you are Essentially asking how Object Space works.
:ToObjectSpace() works by converting a CFrame to what we call “Object Space” based off another CFrame, which is the Location or Point Relative to another Point.

You often hear the Term “World Space” and “Object Space” come around when Position, in essense “World Space” refers to the location based off the Origin of the World, which is exactly (0, 0, 0) if we are speaking in a 3 Dimensional terms, Its is the positions you normally see when position objects at a certain location in this world, for example I could place a block at (10, 20, 30), and based off of world space, would remain the exact same due to its position being already based on world Space.
“Object Space” as stated before refers to a location based off another location, I’ll explain:

In math, its is relatively simple to calulate, it just take only one step to do, but for Simplicity sake, lets use two, 2 Dimensional Positions, you are able to use 3 Dimensional but to make this a lot easier to understand it would be better to use 2 Axis.

In this Example, our point x is located at (23, 10), and our other point y is located at (14, 2), the point we are gping to convert, here these two points are based off World Space, as they are based around the Origin of the World, for Object Spaces, we need to have both of our points, and then find the difference of them, if you dont know what that means, it means they subtract.
we then subtract x and y to find our point convertted to object space, which for this example:

  • 23 -14 = 9
  • 10 - 2 = 8

from this example, we got the answer of (9, 8), which is our point y Convertted to Object Space, which is the position between them rather than the origin, pretty simple proccess.
I guess you can do the same for a single axis, with just simple subtraction, which is also simple to understand

However for the usage in CFrames, it is not exactly the same, you cannot subtract a CFrame with another CFrame, only add them by multiplying them together (cf * cf), However this is also why you see the utilization of :Inverse() for this usage. :Inverse() is essentially the Opposite version of the CFrame, if you have the Position of (1, 1, 1), :Inverse() will convert this to (-1, -1, -1), the opposite of before, wity this new CFrame, we can now essentially subtract by adding, by multiplying.
If you like you can also think about it as adding a negative signed number to a positive signed number which equates to subtraction (ex: [1 + -1] = [1 - 1]), the resultant will then be the Difference between both the given CFrames, which is our Object Space CFrame, and how :ToObjectSpace() works.
As for :ToWorldSpace(), its as simple as reversing your Changes by adding back what you took away.

9 Likes

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