Lua 具有奇怪结果的力定向作图算法

Lua 具有奇怪结果的力定向作图算法,lua,organization,graphing,roblox,Lua,Organization,Graphing,Roblox,我一直在尝试用RBX::Lua创建一个力导向的绘图算法。到目前为止,一切似乎都在数学上运行得非常好,但有一些事情完全没有意义 我的问题是,节点之间存在着一种意想不到的吸引力,无论是否连接。程序所做的是,所有未连接的节点像磁铁一样相互排斥,所有连接的节点像弹簧一样相互吸引 我在代码中没有看到任何可能导致这种情况的东西 -- Creating an icon to represent each node local n = Instance.new("Part", Instance.new("Hum

我一直在尝试用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

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

也许运算符优先级弄乱了您的公式?你可以试着在事物上加上明确的括号。