[solved] Not sure why I'm getting Circular Collisions drawn
Posted: Sun Aug 06, 2017 10:27 am
Hi this is a simple love program. Spawn a random circle every 5 secs that don't overlap with previous circles in the array until it cant fill any more. I'm not sure why it starts drawing overlapping circles when there is still plenty of space available onscreen for circles to be drawn in. I cant see the error in my collision function am i not checking distance between circled and radius's correctly?
cheers in advance for you help.
cheers in advance for you help.
Code: Select all
rings = {}
rings[1] = {x=100,y=250,storedRadius=25}
ringColour = {}
ringColour['green'] = {r=70,g=140,b=38,a=255}
ringColour['pink'] = {r=163,g=73,b=115,a=255}
ringColour['blue'] = {r=93,g=132,b=166,a=255}
ringColour['yellow'] = {r=242,g=222,b=160,a=255}
ringColour['tan'] = {r=242,g=220,b=201,a=255}
count =0
function spawntimer(time)
spawntime = time or 5
return spawntime
end
function randRings()
ringRadius = love.math.random(15 , 100)
xRand = love.math.random(0 + ringRadius, love.graphics.getWidth() - ringRadius )
yRand = love.math.random(0 + ringRadius, love.graphics.getHeight() - ringRadius )
randColourNumber = love.math.random(1, #ringColour)
return ringRadius, xRand, yRand, randColourNumber
end
function checkCircularCollision(ax, ay, bx, by, ar, br)
randRings()
local bx = xRand
local by = yRand
local br = ringRadius
for i,v in ipairs(rings) do
ax = rings[i].x
ay = rings[i].y
ar = rings[i].storedRadius
end
local dx = bx - ax
local dy = by - ay
-- local dist = math.sqrt(dx * dx + dy * dy)
if dx^2 + dy^2 > (ar + br)^2 then
return true
else
return false
-- return dist > 2*(ar + br)
end
end
function love.load(arg)
-- body...
spawntimer()
end
function love.draw(dt)
-- body...
love.graphics.setBackgroundColor(ringColour.pink.r, ringColour.pink.g, ringColour.pink.b, ringColour.pink.a)
love.graphics.print(spawntime, 100 , 100)
love.graphics.print(count, 100 ,200)
for i,v in ipairs(rings) do
love.graphics.circle("line", v.x, v.y, v.storedRadius, 32)
end
end
function love.update(dt)
-- body...
if spawntime > 0 then
spawntime = spawntime - dt
end
if spawntime <= 0 then
checkCircularCollision()
if checkCircularCollision() == true then
table.insert(rings, {x= xRand,
y = yRand,
storedRadius = ringRadius,
})
spawntimer()
else
checkCircularCollision()
end
end
end