力导向图形算法具有奇怪的结果

时间:2013-01-09 03:16:08

标签: lua organization graphing roblox

我一直在努力用RBX :: Lua创建一个力导向图形算法。到目前为止,一切似乎在数学上都非常好,但有一些事情完全没有任何意义。

我的问题是,无论是否连接,节点都会遇到意外的吸引力。程序的作用是,所有未连接的节点像磁铁一样互相排斥,所有连接的节点都像弹簧一样相互吸引。

我的代码中没有任何可能导致此问题的内容。

-- Creating an icon to represent each node
local n = Instance.new("Part", Instance.new("Humanoid", Instance.new("Model")).Parent)
n.Name = "Head"
local tor = Instance.new("Part", n.Parent)
tor.Name = "Torso"
tor.Anchored = true
tor.FormFactor = "Custom"
tor.Transparency = 1
local h = n.Parent.Humanoid
h.Health = 0
h.MaxHealth = 0
h.Parent.Name = "Node"
n.FormFactor = "Symmetric"
n.Shape = "Ball"
n.TopSurface, n.BottomSurface = "Smooth", "Smooth"
n.Size = Vector3.new(2,2,2)
n.BrickColor = BrickColor.new("Institutional white")
n.Anchored = true
Instance.new("Vector3Value", n).Name = "velocity"

-- List of connections and nodes
local t = {
    ["Metals"]={"Gold", "Silver", "Steel", "Brass", "Mercury"},
    ["Alloys"]={"Steel", "Brass"},
    ["Noble Gasses"]={"Helium", "Argon", "Krypton", "Xenon"},
    ["Water"]={"Hydrogen", "Oxygen"},
    ["Liquids"]={"Water", "Mercury"},
    ["Alone"]={}
}
--[[ -- Separate list for testing, commented out
local t = {
    ["A"]={"B"},
    ["B"]={"C"},
    ["C"]={"D"},
    ["D"]={"E"},
    ["E"]={"F"}
}]]

-- Add all of the nodes to the workspace, position them randomly
for _, v in pairs(t) do
    local p1 = workspace:findFirstChild(_) or n.Parent:clone()
    p1.Name = _
    p1.Parent = workspace
    p1.Head.CFrame = CFrame.new(Vector3.new(math.random(-100000,100000)/1000,0,math.random(-100000,100000)/1000))
    for a, b in ipairs(v) do
        if v ~= b then
            local p2 = workspace:findFirstChild(b) or n.Parent:clone()
            p2.Name = b
            p2.Parent = workspace
            p2.Head.CFrame = CFrame.new(Vector3.new(math.random(-100000,100000)/1000,0,math.random(-100000,100000)/1000))
            local at = p1:findFirstChild(b) or Instance.new("ObjectValue", p1)
            at.Name = b
            local at2 = at:clone()
            at2.Parent = p2
            at2.Name = _
            local lasso = Instance.new("SelectionPartLasso", p2.Head)
            lasso.Name = "Link"
            lasso.Humanoid = p2.Humanoid
            lasso.Part = p1.Head
            lasso.Color = BrickColor.new("Institutional white")
        end
    end
end

local parts = {} -- List of all of the nodes themselves
-- Add all of the nodes to the list
for _, v in ipairs(workspace:GetChildren()) do
    if v.ClassName == "Model" then
        table.insert(parts, v.Head)
    end
end

while wait() do -- Repeat forever waiting one frame between loops
    for _, v in ipairs(parts) do
        for a, b in ipairs(parts) do
            if v ~= b then
                local dif = b.Position-v.Position
                local force = 0
                if b.Parent:findFirstChild(v.Name) then -- if b is conneted to v
                    force = (dif.magnitude-30)/100
                else
                    force = force - 1/dif.magnitude^2
                end
                local add = dif/dif.magnitude*force
                add = add - v.Position/v.Position.magnitude/100
                v.velocity.Value = v.velocity.Value + add
            end
        end
        v.CFrame = v.CFrame + v.velocity.Value -- Postion the node
        v.velocity.Value = v.velocity.Value*.2 -- Damping
        v.CFrame = v.CFrame-v.CFrame.p*Vector3.new(0,1,0) -- Force 2D (optional)
        v.Parent.Torso.CFrame = v.CFrame -- To display links connecting nodes
    end
end

1 个答案:

答案 0 :(得分:0)

我发现了问题。 “b.Parent:findFirstChild(v.Name)”应该是“b.Parent:findFirstChild(v.Parent.Name)”,否则每次都会返回true。这只是因为我如何设置节点的显示。