Some problems with multiple objects

Hi guys , I want send big thanks to all of you who send me you solutions , suggestions in previous post. Now I have some troubles with making new project . I have a look to butterflies project, but without comments I don’t understand much . So my problems : 1. Multiple floating flys (for example) 2. How make codea understand overlapping (I search here , but I want make collision without physics.body) 3. How delete sprite/rectangle/anything from the screen . My solution for this like

function touched(touch)
    if touch.state == BEGAN then
   ellipse(touch.x,touch.y,100,100)
   else ellipse(-100,-100,100,100)
end
end

But this don’t work.

P.s All suggestions will make sense for me

Try this

--collision detection example based on circles
function setup()
    --set up a table to hold the x y coordinates of the rocks
    rocktable={}
    for i=1,10 do
        table.insert(rocktable,vec2(math.random(WIDTH),math.random(HEIGHT)))
    end
    
end

function draw()    
    background(40, 40, 50)
    --use a vector to store the x y coordinates of the ship
    ship=vec2(CurrentTouch.x,CurrentTouch.y)
    --drawthe ship
    sprite("Space Art:Part Red Hull 3",ship.x,ship.y)
    --loop through the rock table
    for rocknumber,rock in pairs(rocktable) do
        --draw the current rock
        sprite("Space Art:Asteroid Small",rock.x,rock.y)
        --if the distance between the centre ofthe current rock and the shipis less than 25 pixels then delete the current rock from the table
        if ship:dist(rock)<25 then
            table.remove(rocktable,rocknumber)
        end
        
    end   
    --print the number of rocks in the rock table
    text(#rocktable,WIDTH/2,HEIGHT-100)
end


@west thank you for this code , very useful for me

if you search “aabb” axis-aligned bounding box there is a library i wrote for rectangular collision detection.

@coder ooo , that’s great. Thank you

@lupino8211 Maybe this will help. Any fly that overlaps are removed. I don’t have time to clean this up, but if you have questions I can answer them later.


displayMode(FULLSCREEN)

function setup()
    count=0 -- set count
    flys={} -- table for flys
    -- create 30 flys
    -- set random x,y position and set x,y speed to 0
    for z=1,30 do
        table.insert(flys,vec4(math.random(WIDTH),math.random(HEIGHT),0,0))
    end
end

function draw()
    background(40, 40, 50)
    count=count-1   -- decrement count
    if count<=0 then    -- if 0, set to 30
        count=30    -- this is equivalent to 1/2 second
    end
    -- a=fly table position
    -- b=fly information
    -- b.x=fly x position  b.y=fly y position
    -- b.z=fly x direction to move and distance
    -- b.w=fly y direction to move and distance
    for a,b in pairs(flys) do
        b.x=b.x+b.z -- add speed direction to x position
        b.y=b.y+b.w -- add speed direction to y position
        if b.x>WIDTH then   -- check if off screen
            b.x=0
        end
        if b.x<0 then
            b.x=WIDTH
        end
        if b.y>HEIGHT then
            b.y=0
        end
        if b.y<0 then
            b.y=HEIGHT
        end
        -- draw fly at x and y position
        sprite("Platformer Art:Battor Flap 1",b.x,b.y)
        -- call function to check for overlap
        checkOverlap(b.x,b.y,a) -- pass position of fly being drawn
        if count==30 then   -- change direction every 1/2 second
            b.z=math.random(-1,1)*3
            b.w=math.random(-1,1)*3
        end
    end
    -- remove flys that a flagged for removal
    for z=#flys,1,-1 do
        if flys[z].x==-5 then
            table.remove(flys,z)
        end
    end
end

function checkOverlap(x,y,z)
    v1=vec2(x,y)    -- x,y position of current fly
    for w=1,#flys do    -- loop thru fly table
        -- check for distance between flys
        d=v1:dist(vec2(flys[w].x,flys[w].y))
        if d<40 and z~=w then
            flys[w].x=-5    -- set flys for removal
            flys[z].x=-5
        end
    end    
end

@dave1707 oh, big thanks to you too. But what means that reductions? (b.x , d.x and so on)

@lupino8211 I changed the above code and added comments to explain what’s happening.

@dave1707 thank you , kind sir. I start programming recently and comments very useful for me.