2017-08-08 15 views
0

円を描くためにランダムなx、y、r(半径)を追加して作成したテーブルがあります。最初に、新しいサークルが既存サークルと重複しないようにテストされます。これらの円は時間の経過と共にゆっくりと成長します。私はリングテーブルをどのようにテストして、彼らが交差するほど大きく成長するかを調べようとしています。円のテーブルをチェックする方法円のテーブルが大きくなるにつれて衝突しない

テーブル内の他のすべてに対して最初のテストを行い、次に残りのテストすべてに対して2番目のテストを行う方法はありません。オーバーラップを削除します。

これで始まりましたが、うまく動作しませんでした。それは、次のサークルと比較するだけですが、テーブルの最後にはクラッシュします。

local function newRing() 
    while true do -- infinite loop to create new rings 
     for i, v in ipairs(rings) do 
      --[[ collision calculations on all rings in table until a collision 
       is detected using Pythagoras to calculate distance]] 
      if not collides then 
       rX= v.x 
       rY = v.y 
       rR = v.r 
      local dx = rX - rings[i+1].x 
      local dy = rY - rings[i+1].y 
      local distCalc = dx * dx + dy * dy 
      if distCalc <= ((rings[i+1].r + ringWidth) + (rR + ringWidth))^2 then 
       collides = true 
       break -- restarts while loop once one collision is found 
      end -- end if distCalc block 
     end -- i,v block 
      break 
     end -- end if not collides block 
    end -- end while loop 
    end 

答えて

1
-- remove all collided rings 
for k = #rings, 1, -1 do 
    local rX = rings[k].x 
    local rY = rings[k].y 
    local rR = rings[k].r 
    local collides 
    for j = k + 1, #rings do 
     local dx = rX - rings[j].x 
     local dy = rY - rings[j].y 
     local distCalc = dx * dx + dy * dy 
     if distCalc <= ((rings[j].r + ringWidth) + (rR + ringWidth))^2 then 
     collides = true 
     break 
     end 
    end 
    if collides then 
     -- do something here (erase ring[k] from the screen, etc.) 
     table.remove(rings, k) 
    end 
end 
+0

乾杯魔法のように動作します。私は[[k = #rings、1、-1 do]の前にこの構造を見たことがない –

関連する問題