I cant really figure out whats CFrame:Inverse(), :toObjectSpace() and :toWorldSpace() and really need an explanation.
Can someone pls explain?? Thanks, I also need a little explanation on the difference between Vector3.new() and CFrame.new().
All helps will be greatly appreciated. Will accept the most suitable answer.
Let's start with coordinate systems. A coordinate system is just a way to describe the position of an object in some space.
ROBLOX is, obviously, three dimensional, so to describe the position you need three numbers. These numbers can be interpreted as the distance along each axes, so (3, 5, 6) would mean 3 studs right (X axis), 5 studs up (Y axis) , and 6 studs forward (Z axis). This requires a point of reference, i.e where to start before applying these movements. This point of reference is called the origin of the coordinate system, and is represented by the (0,0,0) coordinate.
Those three numbers can be efficiently represented and computed upon using vectors. Which is what Vector3 class provides, a way to manipulate positions in 3D space along with common vector operations such as cross and dot product.
However vectors can only represent position, not orientation. You would think you need 3 more numbers to define orientation (the degree to rotate along each axis applied in sequence), and it would be correct, those are called Euler Angles, however to represent them efficiently and be able to make quick computations with them you use what we call rotation matrices.
These are provided by roblox with the CFrame class, name standing for Coordiante Frame, and they can encode orientation and apply rotations efficiently.
Now let's say you have a ball in position (3,0,2), as I mentioned above, this vector represents the position with respect with to the origin of a coordinate system. The default coordinate system in ROBLOX is called world space, where every object shares the same origin. However what if you need to know how far a character standing at (3,0,3) is from the ball?, you can compute it easily enough with simple math and get (0,0,1) studs from the ball. What you did there was actually convert the character position to object space, particularly to the ball's object space, and as the name implies is just a coordinate system where the origin is now the position of the ball, so everything is represented as being away from the ball instead of the world origin.
This was done easily enough since we didn't care about orientation, when you take into account orientation the math gets complicated (for those interested is just some linear algebra and trigonometry), so ROBLOX handles that for you with the methods toObjectSpace
and toWorldSpace
, where given a CFrame we convert it to the object space of another CFrame, or viceversa with toWorldSpace.
Inverse
is used under the hood when performing those computations, as I mentioned before these computations involve linear algebra, but in layman's terms if you move an object with a CFrame, Inverse
gives you the CFrame to move the object back to starting place.
CFrame:toObjectSpace(arg) takes the CFrame (in world coordinates) and translates it to the coordinates of arg (as if arg was the origin of the game).
CFrame:toWorldSpace(arg) takes the CFrame relative to arg and translates it back to coordinates relative to the origin of the game.
CFrame:Inverse() as far as I know inverts the matrix of a CFrame. Which you'd have to know more about matrix math to deal with.
Thank You @Destrings & @Vathriel for answering and trying to help! Also, thank you to all of you guys who commented. Appreciated :) Have a good day!