I was learning CFrame, and I came across the following:
toWorldSpace
toObjectSpace
pointToWorldSpace
pointToObjectSpace
vectorToWorldSpace
vectorToObjectSpace
As I was learning, it got really confused on what is world and object space.
If I were to give you directions to a certain location on the earth, I could give you specific longitude and latitude coordinates. Or I could say that the specified location is "5 km north of the convenience store". This relates to object and world space. Giving longitude and latitude coordinates is analogous to world space and giving directions relating to the school is analogous to object space.
Each of the CFrame
methods you outlined are explained and given with examples.
This method converts a cframe in object space to a cframe in world space. In other words it takes cf
(which is relative to CFrame
) and returns a result that is relative to the origin. For example, if I have a cframe called cf1
which is defined to be CFrame.new(0 , 10 , 0)
and I have another cframe relative to cf1
called cf2
which is CFrame.new(1 , 0 , 0)
, cf1:toWorldSpace(cf2)
will return a cframe in world space that is relative to the origin/identity cframe.
This method does the exact opposite. Given a CFrame (let's call it cf1
), and an input CFrame (call it c2
), it will return a result representing the cframe cf2
is in relative to cf1
. For example, if an input CFrame
defined CFrame.new(5 , 1 , 3)
is given, CFrame.new(1 , 1 1):toObjectSpace(cf)
will return CFrame.new(4 , 0 , 2)
because that is the CFrame
the input is in relative to CFrame.new(1 , 1 , 1)
These methods just do the same thing as CFrame:toObjectSpace(cf)
and CFrame:toWorldSpace(cf)
respectively but work with Vector3
inputs instead.
This method is a little harder to visualize because it takes rotation into account. Keep in the mind that in this case, the input Vector3
represents a direction and not a position. Given a CFrame
and a Vector3
(relative to the CFrame's orientation), this method will return a new Vector3
that represents the rotation in world space. For example, if I have a CFrame that is defined as CFrame.Angles(math.pi , 0 , 0)
(this CFrame's y axis is upside down) and I call cframe:vectorToWorldSpace(Vector3.new(0 , 1 , 0))
, it will return Vector3.new(0 , -1 , 0)
. This is because the original vector is pointing upwards relative to the CFrame
. Since the CFrame
itself is upside down, vectorToWorldSpace(v3)
returns the "actual" direction the vector is pointing in.
This method reverses what vectorToWorldSpace()
does. It will rotate a vector in world space so that it matches up with the rotation of the said CFrame
.