collide

I have two cars and if they run into each other the game restarts how can i do this.

Here’s an example. Think of the 2 circles as your cars.


function setup() 
    c1 = physics.body(CIRCLE,50)
    c1.x=300
    c1.y=900
    
    c2 = physics.body(CIRCLE,50)
    c2.x=300
    c2.y=600
    c2.type=STATIC
end

function collide(contact)
    if contact.state == BEGAN then
        print("\
restart program")
        restart()
    end
end

function draw()
    background(30, 30, 30, 25)    
    stroke(255)
    strokeWidth(1)
    noFill()   
    ellipse(c1.x,c1.y,100,100)    
    ellipse(c2.x,c2.y,100,100)
end

thank you. when I put the code into my game it didnt work. i think its becaus i have more than two car going in the game. My main set up is petty much the same as the code you just gave me, it just has more objects in the game

@Andrewsimpson4 If you want to check collision between selected objects, then give them a unique info value, then when a collision occurs, you check the info of the 2 objects to see if they’re the ones colliding.

EDIT: If your objects are defined in a particular order, you can simplify the if statement with bodyA and bodyB. bodyA is the object defined before bodyB. So instead of doing a double compare, you can do a single compare of bodyA.info and bodyB.info.


function setup() 
    c1 = physics.body(CIRCLE,50)
    c1.x=300
    c1.y=900
    c1.info="c1"     -- new code
    
    c2 = physics.body(CIRCLE,50)
    c2.x=300
    c2.y=600
    c2.type=STATIC
    c2.info="c2"     -- new code
end

function collide(contact)
    if contact.state == BEGAN then
        -- new code
        if contact.bodyA.info=="c1" and contact.bodyB.info=="c2" or
            contact.bodyA.info=="c2" and contact.bodyB.info=="c1" then
                print("\
restart program")
                restart()
        end
    end
end

function draw()
    background(30, 30, 30, 25)    
    stroke(255)
    strokeWidth(1)
    noFill()   
    ellipse(c1.x,c1.y,100,100)    
    ellipse(c2.x,c2.y,100,100)
end

that kind of work. My game is I have a car that you control with your finger and you try to dodge the cars coming towards you. I noticed that when i move my car around and hit a car the game just freezes. but when i don’t move my car and just let the cars coming towards me hit me then it works and restarts.

@dave1707

@Andrewsimpson4 Since I don’t know what your code is doing, here’s something I threw together. I have one sprite (boy) controlled by my finger movement (joystick) and 2 sprites(boy and girl) moving down the screen. If the boy and girl collide, the program restarts. If the 2 boys collide, the falling boy just gets pushed around. I know that’s not exactly what you’re doing, but somewhat similar. Since c1 is defined before c2 or c3, I know that bodyA is going to be c1 during a collision. bodyB could be either c2 or c3. Since I don’t care about the c1 and c3 collision, I don’t do anything for that.


displayMode(FULLSCREEN)

function setup()
    c1=physics.body(CIRCLE,40)
    c1.x=400
    c1.y=200
    c1.gravityScale=0
    c1.info="c1"
    
    c2=physics.body(CIRCLE,10)
    c2.x=500
    c2.y=1000
    c2.gravityScale=.2
    c2.info="c2"
    
    c3=physics.body(CIRCLE,10)
    c3.x=300
    c3.y=1000
    c3.gravityScale=.2
    c3.info="c3"
        
    x,y=0,0
    sx=WIDTH/2
    sy=HEIGHT/2
end

function draw()
    background(40, 40, 50)
    fill(255)
    sprite("Planet Cute:Character Boy",c1.x,c1.y)
    if x+y>0 then
        c1.x=c1.x+(tx-x)/10
        c1.y=c1.y+(ty-y)/10
        fill(255)
        ellipse(x,y,8)    -- draw circle center
        noFill()
        stroke(255)
        strokeWidth(4)
        ellipse(x,y,200)    -- draw outer circle
    end
    sprite("Planet Cute:Character Cat Girl",c2.x,c2.y)
    if c2.y<0 then
        c2.y=1000
        c2.linearVelocity=vec2(0,0)
    end
    sprite("Planet Cute:Character Boy",c3.x,c3.y)
    if c3.y<0 then
        c3.x=300
        c3.y=1000
        c3.linearVelocity=vec2(0,0)
    end
end

function collide(c)
    if c.state==BEGAN then
        if c.bodyA.info=="c1" and c.bodyB.info=="c2" then
            restart()
        end
    end   
end

function touched(t)
    if t.state==BEGAN then    -- starting center point
        x=t.x
        y=t.y
        tx=x
        ty=y
    elseif t.state==MOVING then
        tx=t.x
        ty=t.y
    elseif  t.state==ENDED then    -- done moving
        x,y=0,0
    end
end

here is the code i am using to move my guy around. i think this is the problem because when i don’t move him at all and a guy runs into him it will restart but when i move him and then he runs into someone it wont restart

function touched(t)
if math.abs(t.x-c1.x)<50 and math.abs(t.y-c1.y)<50 then
c1.x=t.x
c1.y=t.y

@dave1707

I wrote another version that was closer to what you said your program did, and I was getting an error when the program was to restart after I collided with another object and I was still trying to move the object. Maybe that’s similar to what you were getting. The change I had to make was to add a flag when the collision happened so I wasn’t able to move the piece anymore until the program restarted. Try this. In setup, set the variable hit=false. In the collide routine before restart(), set hit=true. If the touch function, try this


function touched(t)
     if not hit then
        if math.abs(t.x-c1.x)<50 and math.abs(t.y-c1.y<50 then
           c1.x=t.x
           c1.y=t.y
        end
     end
end

If you want me to post my latest test so you can see what I did, let me know.

ya could you post @dave1707

@Andrewsimpson4 Here is the other version. Is this close to what you’re trying to do. Slide your finger left or right on the screen to move the ship. Try to avoid the stars.


displayMode(FULLSCREEN)

function setup()
    tab={}
    yVel=-300
    c1=physics.body(CIRCLE,20)
    c1.x=400
    c1.y=400
    c1.gravityScale=0
    c1.info="c1"   
    running=false
    menu=true
end

function setup1()    
    for z=1,15 do    
        c2=physics.body(CIRCLE,20)
        c2.x=math.random(WIDTH)
        c2.y=math.random(HEIGHT-200,HEIGHT)
        c2.linearVelocity=vec2(0,yVel)
        c2.gravityScale=0
        c2.info="c2"
        table.insert(tab,c2)
    end    
end

function draw()
    background(40, 40, 50)
    if menu then
        start()
    else
        if not running then
            setup1()
            running=true
        end
        drawCircles()
    end    
end

function drawCircles()
    for z=1,#tab do
        sprite("Space Art:Green Explosion",tab[z].x,tab[z].y,40)
        if tab[z].y<0 then
            tab[z].x=math.random(WIDTH)
            tab[z].y=math.random(HEIGHT,HEIGHT+300)
            tab[z].linearVelocity=vec2(0,yVel)
        end
    end  
    sprite("Space Art:Red Ship",c1.x,c1.y,40)
    yVel = yVel - .2
end

function start()
    fill(255)
    text("tap screen to start",WIDTH/2,HEIGHT/2)
end

function collide(c)
    if c.state==BEGAN then
        if c.bodyA.info=="c1" and c.bodyB.info=="c2" then
            running=false
            restart()
        end
    end   
end

function touched(t)
    if t.state==BEGAN then
        menu=false
    end
    if t.state==MOVING and running then
        c1.x=c1.x+t.deltaX*2
        if c1.x>WIDTH or c1.x<0 then
            running=false
            restart()
        end
    end
end

thats perfect thank you for your help

@dave 1707

One other thing in collide() you might want to change…

function collide(contact)
    if contact.state == BEGAN then
        if (contact.bodyA == "c1" or contact.bodyB == "c1") and (contact.bodyA == "c2" or contact.bodyB == "c2") then
            running = false
            restart()
        end
    end
end

@SkyTheCoder The long “if” statement isn’t necessary here because c1 is defined before c2, so c1 will always be bodyA and c2 will be bodyB. The only time you need a long “if” statement is when you’re not sure what order the objects will be defined. That would be when you create and destroy objects on the fly. In this case, the objects are created only once and not destroyed.

@dave1707 Oh, I wasn’t sure how it decided bodyA and bodyB. Thanks for explaining.

@SkyTheCoder I didn’t know either until I created a program with multiple objects that I could move. I would collide the different objects and see which one printed as bodyA and which one as bodyB. It always turned out that the bodyA object was defined before the bodyB object.

with the code where it has the boy coliding into the girl to restart can you make it so you can colide with bolth of them to restart @dave1707

@Andrewsimpson4 Just comment out the “if” and “end” statements like below. Anytime there’s a collision, the program restarts.


function collide(c)
    if c.state==BEGAN then
        --if c.bodyA.info=="c1" and c.bodyB.info=="c2" then
            restart()
        --end
    end   
end