Collision help

Hi everyone. I need some help. Somehow I forgot how to use the “collide” function. Could I please get some help? Btw I wouldn’t be posting if I could find an e book on it or something. That’s why I ask, Becuase I can’t get any other help. Thanks guys!

Just use the Codea reference, (the eye in-project).

See the discussion What's the logic of contact bodyA and bodyB a few discussions down.

@MattthewLXXIII I have tried, but it’s not enough

And what I mean is ~physics.contact~ where to put it, and how it works. :confused:

@LL_Phoenix456, what are you trying to do? And which part is confusing of the contact reference? Have you tried looking at the physics lab demo? I found it quite helpful.

I’m trying to make an asteroid dodging game and I forgot how to check for certain collisions after scanning through a table (the table is asteroids)

@LL_Pheonix456 you could do something like:

function collide(c)
    for i,v in pairs(asteroids) do
        if (c.bodyA == v.body and c.bodyB == player.body) or (c.bodyB == v.body and c.bodyA == player.body) then
            print("DIE")
        end
    end
end

@MattthewLXXIII And what would I change the v.bodyA, v.body and c.body to?

@LL_Phoenix456 Will anything here help explain what you need to do.

displayMode(FULLSCREEN)

function setup()
    hit=6
    e=physics.body(CHAIN,true,vec2(0,0),vec2(0,HEIGHT),
            vec2(WIDTH,HEIGHT),vec2(WIDTH,0))
    e.info="edge"
    s=physics.body(CIRCLE,40)
    s.x=WIDTH/2
    s.y=HEIGHT/2
    s.gravityScale=0
    s.restitution=1
    s.info="ship"
    ast={}
    for z=1,10 do
        a=physics.body(CIRCLE,30)
        a.x=math.random(WIDTH)
        a.y=math.random(HEIGHT)
        a.linearVelocity=vec2(math.random(-300,300),math.random(-300,300))
        a.gravityScale=0
        a.restitution=1
        a.info="ast"
        a.friction=.1
        table.insert(ast,a)        
    end
end

function draw()
    background(0)
    fill(255)
    pushMatrix()
    translate(s.x,s.y)
    rotate(s.angle)
    if hit<5 then 
        hit=hit+1
        sprite("Tyrian Remastered:Explosion Huge",0,0,hit*20)
    else
        sprite("Space Art:Red Ship",0,0)
    end
    translate()
    popMatrix()
    for a,b in pairs(ast) do
        pushMatrix()
        translate(b.x,b.y)
        rotate(b.angle)
        sprite("Space Art:Asteroid Large",0,0,80)
        translate()
        popMatrix()
    end
end

function collide(c)
    if c.bodyA.info=="ast" and c.bodyB.info=="ship" or 
            c.bodyA.info=="ship" and c.bodyB.info=="ast" then
        hit=0
    end
end

@dave1707 What’s the deal with bodyA and bodyB? I tried implementing it and it didn’t work

I don’t get it. I’m trying to make it so if the player collides with an asteroid, it does something. Not if anything collides with anything.

@LL_Phoenix456 Are you saying you just want the asteroids to collide with the ship (player) and nothing else. In other words, the asteroids can come from any direction off screen, pass thru the screen area in a straight line, then go off the screen. They don’t collide with each other, only the ship. If that’s the case, you don’t need to use the collide function, which is for physics objects. You can just use a simple function that calculates the distance between each asteroid and the ship and if they’re below a certain distance, there’s a collision. Is that what you’re trying to do. A lot of times, the hard part is trying to understand what needs to be done, not the coding to do it.

@dave1707 Yep. That’s exactly right.

@LL_Phoenix456 Are you looking for something like this. Move your finger around the screen to move the ship.

displayMode(FULLSCREEN)
supportedOrientations(LANDSCAPE_ANY)

function setup()
    hits=0
    et=os.clock()
    ast={}
    for z=1,30 do
        x=WIDTH+math.random(400)    -- x position
        y=math.random(HEIGHT)       -- y position
        z=math.random(1,10)     -- speed
        w=math.random(1,2)      -- type
        table.insert(ast,vec4(x,y,z,w))        
    end
    shx=WIDTH/2
    shy=HEIGHT/2
    dx,dy=0,0
end

function draw()
    background(0)

    for a,b in pairs(ast) do
        if b.w==1 then
            sprite("Space Art:Asteroid Large",b.x,b.y)
        else
            sprite("Space Art:Asteroid Small",b.x,b.y)
        end
        b.x=b.x-b.z
        if b.x<-50 then
            b.x=WIDTH+math.random(200)
            b.y=math.random(HEIGHT)
        end
        checkCollision()
    end
    pushMatrix()
    translate(shx+dx,shy+dy)
    rotate(270)
    if hit then
        sprite("Tyrian Remastered:Explosion Huge",0,0)
    else   
        sprite("Space Art:Red Ship Icon",0,0)
    end
    popMatrix() 
    fill(255)
    text(string.format("Time %.1f   Hits %d",os.clock()-et,hits),WIDTH/2,HEIGHT-20)
end

function checkCollision()
    hit=false
    for a,b in pairs(ast) do        
        v1=vec2(b.x,b.y)
        d=v1:dist(vec2(shx+dx,shy+dy))
        if b.w==1 then
            if d<60 then
                hit=true 
                if clear then
                    hits=hits+1  
                end
                clear=false          
            end
        elseif d<20 then
            hit=true
            if clear then
                hits=hits+1
            end
            clear=false
        end 
    end 
    if not hit then
        clear=true
    end
end

function touched(t)
    if t.state==MOVING then
        dx=dx+t.deltaX
        dy=dy+t.deltaY
    end
end