I have a table that is created by adding random x,y and r (radius), which I use to draw circles. First they are tested to ensure the new circles don't overlap existing ones. These circles then grow slowly over time. I'm trying to work out how to test my rings table for when they grow so much that they intersect.
I cant find a way to test the first one against all others in the table, then the 2nd one against all remaining ones etc. Removing any that overlap.
Started with this but realized it wont work at best it will compare itself to the next circle only but crash when at the end of the table.
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