Does inversing return the original position of an object? or what, thank you for answering if you do!
In math, the inverse of a value with respect to some operation is the value that "undoes" the operation.
For example, the additive inverse of 5
is -5
since 5 + -5 = 0
. If you have x + 5 + -5
, you get back x
.
The multiplicative inverse of 5
is 1/5
, since 5 * 1/5 = 1
. If you have x * 5 * 1/5
, you get back x
.
In Roblox, the :inverse()
of a CFrame can be used to "undo" a multiplication of a CFrame. Normally, the inverse itself is useless. An easier to grasp pattern uses the :toObjectSpace
method.
The .CFrame
of a part is absolute, that is, it's "relative" to the origin, some fixed point in the middle of the map.
Often, you want to know how two parts are related in space. For example, how far above part A is part B?
You can use a.CFrame:toObjectSpace(b.CFrame)
to get the position and orientation in space of b
relative to a
. Internally, this is just a.CFrame:inverse() * b.CFrame
.
One application of this is simulating joints:
-- Capture how `part` was originally placed relative to `anchor` local offset = anchor.CFrame:toObjectSpace(part.CFrame) while wait() do -- Spin the anchor somehow anchor.CFrame = anchor.CFrame * CFrame.Angles(0.1, 0.2, 0.3) -- Keep `part` in the same place relative to the anchor as it was before part.CFrame = anchor.CFrame:toWorldSpace(offset) end