Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Some problems with multiple objects

edited August 2014 in Questions Posts: 175

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
   else ellipse(-100,-100,100,100)

But this don't work.

P.s All suggestions will make sense for me



  • Posts: 718

    Try this

    --collision detection example based on circles
    function setup()
        --set up a table to hold the x y coordinates of the rocks
        for i=1,10 do
    function draw()    
        background(40, 40, 50)
        --use a vector to store the x y coordinates of the ship
        --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
        --print the number of rocks in the rock table
  • Posts: 175

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

  • Posts: 425

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

  • Posts: 175

    @coder ooo , that's great. Thank you

  • dave1707dave1707 Mod
    edited August 2014 Posts: 7,470

    @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
  • Posts: 175

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

  • dave1707dave1707 Mod
    Posts: 7,470

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

  • Posts: 175

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

Sign In or Register to comment.