如何检查圆形表不会碰撞,因为他们增长
问题描述:
我有一个表是通过添加随机x,y和r(半径),我用来绘制圆形创建的。首先他们进行测试,以确保新的圈子不重叠现有的圈子。随着时间的推移,这些圈子会慢慢增长我正在努力研究如何测试我的戒指表,因为它们长得如此之多以至于它们相交。如何检查圆形表不会碰撞,因为他们增长
我无法找到一种方法来测试第一个对所有其他人在表中,然后第二个对所有其余的人等删除任何重叠。
从此开始,但意识到它不会工作最好它只会将自己与下一个圆圈进行比较,但在表格末尾时会崩溃。
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
答
-- 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
欢呼声就像一个魅力。在[[for k = #rings,1,-1]之前]我从来没有见过这种构造] –