2013-01-09 4 views
1

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 
+0

아마도 연산자 우선 순위가 수식을 엉망으로 만들고 있습니까? 사물에 명백한 괄호를 넣으십시오. – hugomg

답변

0

이 문제점을 발견했습니다. "b.Parent : findFirstChild (v.Name)"은 "b.Parent : findFirstChild (v.Parent.Name)"이어야합니다. 그렇지 않으면 매번 true가 반환됩니다. 이는 노드를 표시하는 방법을 설정 한 이유입니다.

관련 문제