Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

Did I do this CFrame wrong?

Asked by
drew1017 330 Moderation Voter
9 years ago
local weld2 = Instance.new("Weld", game.JointsService)
weld2.Part0 = Head
weld2.Part1 = rEye
weld2.C0 = rEye.CFrame:inverse()
weld2.C1 = rEye.CFrame:inverse()

local weld3 = Instance.new("Weld", game.JointsService)
weld3.Part0 = Head
weld3.Part1 = lEye
weld3.C0 = lEye.CFrame:inverse()
weld3.C1 = lEye.CFrame:inverse()

(I have all variables defined and this is the only part of the script that interacts with them)

The C0 = inverse + C1 = inverse operation is meant to get the part back into it's original position after it has been welded (and thus transfered position to part0). This works perfectly if only the first block of code is there, but if the second is there, the rEye and lEye are both wildly offcenter. I don't see how I did this incorrectly, so could someone help?

0
Not sure... But eyes? What are you making? unmiss 337 — 9y

1 answer

Log in to vote
1
Answered by
Unclear 1776 Moderation Voter
9 years ago

This is a problem better solved with a generic Model welding algorithm.

My favorite way to go about it is as follows:

  1. Get a reference to a target Model (make sure all of its children are Anchored)
  2. Get all of the children of the target Model
  3. Pick one of the children to be the "root". All other parts will be welded to this single child (hence a "root").
  4. Loop through all of the other children and weld them to "root".
  5. Unanchor all of the parts.

When welding two Parts together to preserve their relative CFrames, you will calculate the offset of one of the Parts from the other Part. In our case, the offset of the target child from the "root".

This is best done as follows: offset = rootCFrame:inverse()*childCFrame. This is because you want to center the child's CFrame around a new coordinate system based upon root's CFrame, hence you invert root's CFrame to get its offset from the origin (which is the current coordinate system). Try drawing this out on a piece of paper to get more of a sense for the logic behind this.

Altogether, a Model welding function may look like the following:

local create = LoadLibrary("RbxUtility").Create 
-- my favorite built-in ROBLOX object creation function, feel free to use another one

function weldModel(model)
    local children = model:GetChildren()
    local root = children[1]
    for index = 2, #children do
        local child = children[index]
        if child:IsA("BasePart") then
            local offsetFromRoot = root.CFrame:inverse()*child.CFrame
            create("Weld")({
                Parent = root,
                Part0 = root,
                Part1 = child,
                C0 = offsetFromRoot
            })
            child.Anchored = false
        end
    end 
    root.Anchored = false
end
weldModel(workspace.Model) -- Replace with a reference to your model
0
This is super clever, thank you for sharing your magix drew1017 330 — 9y
Ad

Answer this question