Help with collisions. [Resolved]

Say they are two object types, and they’re both in different tables. Is there a way to detect what position an item of the second object type is in the table? Because right now I’m using:

    if contact.state == BEGAN  then
        
        Contactx=contact.bodyA.x   
        Contacty=contact.bodyA.y    
        
    end  

The only problem with that is I have to run a for loop to check all the items on the screen and see if one fits the boundaries of the contact variables. However, I get an error when I try to remove an instance of Object 2 once contact has been made between two items. Is there another way to find the exact objects that have made contact, and automatically delete that from the table?

Please tell me to elaborate if needed.

@Ignatz. I was looking around, and I found this post by @dave1707 that really summarizes my problem.

Here is @dave1707 's code with the addition of one line that is commented out and would cause an error. It is the table.remove line.


-- Random PinBall by Dave1707

function setup()
    displayMode(FULLSCREEN)
    
    -- define variables and tables
    cx=0
    cy=0
    limit=150   
    tab1={}
    tab2={}
    
    create()    -- set table values
    
    -- set pinball values
    circ1 = physics.body(CIRCLE,5)
    circ1.x=300
    circ1.y=HEIGHT-50
    circ1.gravityScale=.6
    circ1.restitution=1

    -- set line values   
    line1 = physics.body(EDGE,vec2(0,0),vec2(0,HEIGHT))
    line2 = physics.body(EDGE,vec2(WIDTH,0),vec2(WIDTH,HEIGHT))
    line3 = physics.body(EDGE,vec2(0,0),vec2(WIDTH,0))
    line4 = physics.body(EDGE,vec2(0,HEIGHT),vec2(WIDTH,HEIGHT))     
end

function create()
    -- set initial values for pins in tables 
    for x=1,limit do
        tab1[x] = physics.body(CIRCLE,10)  
        tab1[x].x = math.random(WIDTH)
        tab1[x].y = math.random(HEIGHT)
        tab1[x].type = STATIC
        tab2[x]=0    -- 0=pin not hit, 1=pin was hit
    end       
end

function collide(contact)
    -- check for collision 
    if contact.state == BEGAN then
        cx=contact.bodyA.x  -- x position of hit pin
        cy=contact.bodyA.y   -- y position of hit pin
        -- restart pinball when at bottom of screen
        if circ1.y < 20 then
            circ1.y = HEIGHT-50
            circ1.x = math.random(WIDTH)
        end
    end   
end

function draw()
    background(0, 0, 0) 

    -- draw pinball
    fill(255,0,0,255)
    ellipse(circ1.x,circ1.y,10,10)

    -- draw boundary lines    
    line(0,0,0,HEIGHT)  
    line(WIDTH,0,WIDTH,HEIGHT) 
    line(0,0,0,WIDTH)
    line(0,HEIGHT,WIDTH,HEIGHT)
    
    -- draw flashing pins or white pins each draw cycle
    for x=1,limit do
        -- check which pin was hit
        if cx == tab1[x].x and cy == tab1[x].y then
            tab2[x] = 1 
           -- table.remove(tab1, tab1[x])
   -- set table for hit pin
            cx=0           -- clear values         
            cy=0
        end
        
        fill(255)                -- set color to white
        if tab2[x] > 0 then      -- pin was hit, set random color
            fill(255, 0, 0, 255)
        end
        
        ellipse(tab1[x].x,tab1[x].y,20)    -- draw pins       
    end 

    -- draw credits    
    fill(0, 252, 0, 255) 
    text("Random PinBall by Dave1707",WIDTH/2,HEIGHT/2)   
end

@ Ignatz. Thanks, but I’m still lost on how to actually do that. The problem I’m having with collisions is that I’m trying to delete the objects on screen from a for loop using something like

table.remove(t, t[index of the for loop])

But that doesn’t seem to work.

@YoloSwag - physics bodies have a property called info. You can put a number or text in here that tells you which body it is, eg the index of the array, then when you have a collision, look at the info properties of bodyA and bodyB.

@YoloSwag Here’s an example that removes any white ball that collides with the red ball.


displayMode(FULLSCREEN)

function setup()
    tab={}
    for z=1,50 do
        a=physics.body(CIRCLE,20)
        a.x=math.random(WIDTH)
        a.y=math.random(HEIGHT)
        a.gravityScale=0
        a.restitution=1.1
        a.info=z
        a.linearVelocity=vec2(math.random(-200,200),math.random(-200,200))
        table.insert(tab,a)
    end
    e1=physics.body(EDGE,vec2(0,0),vec2(0,HEIGHT))
    e1.info=0
    e2=physics.body(EDGE,vec2(0,HEIGHT),vec2(WIDTH,HEIGHT))
    e2.info=0
    e3=physics.body(EDGE,vec2(WIDTH,HEIGHT),vec2(WIDTH,0))
    e3.info=0
    e4=physics.body(EDGE,vec2(WIDTH,0),vec2(0,0))
    e4.info=0
end

function draw()
    background(40, 40, 50)
    for a,b in pairs(tab) do
        fill(255)
        if a==1 then
            fill(255,0,0)
        end
        ellipse(b.x,b.y,40)
    end
    if rem~=0 then
        for a,b in pairs(tab) do
            if rem==b.info then
                tab[a]:destroy()
                table.remove(tab,a)
            end
        end
    end
end

function collide(c)
    if c.bodyA.info==1 then
        rem=c.bodyB.info
    elseif c.bodyB.info==1 then
        rem=c.bodyA.info
    end
end

read about zombie physics objects in the second half of this post (and what to do with them)

http://coolcodea.wordpress.com/2013/03/21/applying-gravity/

@Ignatz, I’ll be sure to read that. @dave1707, that example really helped me, thanks!