Collision

Hi, sorry for a question that seems to me like it should be pretty simple but I cannot understand collisions. I am new to this an do would really appreciate it if someone were to help me out by telling me how to use them. Also can you provide examples? Thanks in advance :slight_smile:

Hi @Deretsuk

Here is a simple program showing a collision and how to get some info when a collision happens.


function setup() 
    c1 = physics.body(CIRCLE,100)    -- create stationary circle
    c1.type=STATIC
    c1.x=WIDTH/2
    c1.y=300
    
    c2 = physics.body(CIRCLE,50)    -- create falling circle
    c2.x=WIDTH/2
    c2.y=800
    c2.restitution=1
end

function draw()
    background(40,40,50)
    stroke(255)
    strokeWidth(1)
    noFill()
    ellipse(c1.x,c1.y,200,200)    -- draw the stationary circle
    ellipse(c2.x,c2.y,100,100)    -- draw the falling circle   
end

function collide(c)  -- this gets called when a collision occurs
    if c.state==BEGAN then
        print("\
collision")
        print("radius of body A ",c.bodyA.radius)
        print("radius of body B ",c.bodyB.radius)
        -- see physics.contact and physics.body in the reference manual
        -- for other info that's available during a collision
    end  
end

Thank you so much. This was so helpful :slight_smile:

whats wrong with my code i want it to be able to make the sprites not overlapp

– gravity with walls

– Use this function to perform your initial setup
function setup()
print(“rugby rules”)
x = WIDTH/2
y = HEIGHT/2
xx = WIDTH/2
yy = HEIGHT/2

sound("Game Sounds One:1-2-3 Go")

end
function draw()
– This sets a dark background color
sprite(“Documents:feild”,WIDTH/2, HEIGHT/2, WIDTH, HEIGHT)

-- This sets the line thickness
strokeWidth(5)

-- Do your drawing here
sprite("Documents:ball",x, y, 50)
x = (x + Gravity.x * 23)
y = (y + Gravity.y * 23)

sprite("Documents:loose head prop",xx, yy, 40)
xx = (xx + Gravity.xx * 20)
yy = (yy + Gravity.yy * 20)

if x < 25 then
    x = 25
    elseif x > WIDTH - 25 then
    x = WIDTH - 25
end


if y < 25 then
    y = 25
    elseif y > HEIGHT - 25 then
    y = HEIGHT - 25
end


if xx < 20 then
    xx = 20
    elseif xx > WIDTH - 20 then
    xx = WIDTH - 20
end

if yy < 20 then
    yy = 20
    elseif yy > HEIGHT - 20 then
    yy = HEIGHT - 20
end

end

function isNotlapping()
local re1=x+20
local le1=x-20
local te1=y+20
local be1=y-20
local re2=xx+20
local le2=xx-20
local te2=yy+20
local be2=yy-20
if s2.re > s1.le or
s2.le > s1.re or
s2.be > s1.te or
s2.te < s1.be then
return false
end
end
function isOverlapping()
local re1=x+20
local le1=x-20
local te1=y+20
local be1 =y-20
local re2=xx+20
local le2=xx-20
local te2=yy+20
local be2=yy-20
if s2.re < s1.le or
s2.le < s1.re or
s2.be < s1.te or
s2.te > s1.be then
return true
end
end

-- gravity with walls

-- Use this function to perform your initial setup
function setup()
    print("rugby rules")
    x = WIDTH/2
    y = HEIGHT/2
    xx = WIDTH/2
    yy = HEIGHT/2
    
    sound("Game Sounds One:1-2-3 Go")
end
function draw()
    -- This sets a dark background color
    sprite("Documents:feild",WIDTH/2, HEIGHT/2, WIDTH, HEIGHT)
    
    -- This sets the line thickness
    strokeWidth(5)
    
    -- Do your drawing here
    sprite("Documents:ball",x, y, 50)
    x = (x + Gravity.x * 23)
    y = (y + Gravity.y * 23)
    
    sprite("Documents:loose head prop",xx, yy, 40)
    xx = (xx + Gravity.xx * 20)
    yy = (yy + Gravity.yy * 20)
    
    if x < 25 then
        x = 25
        elseif x > WIDTH - 25 then
        x = WIDTH - 25
    end
    
    
    if y < 25 then
        y = 25
        elseif y > HEIGHT - 25 then
        y = HEIGHT - 25
    end
    
    
    if xx < 20 then
        xx = 20
        elseif xx > WIDTH - 20 then
        xx = WIDTH - 20
    end
    
    if yy < 20 then
        yy = 20
        elseif yy > HEIGHT - 20 then
        yy = HEIGHT - 20
    end
end


function isNotlapping()
    local re1=x+20
    local le1=x-20
    local te1=y+20
    local be1=y-20
    local re2=xx+20
    local le2=xx-20
    local te2=yy+20
    local be2=yy-20
    if s2.re > s1.le or
    s2.le > s1.re or
    s2.be > s1.te or
    s2.te < s1.be then
        return false
    end
end
function isOverlapping()
    local re1=x+20
    local le1=x-20
    local te1=y+20
    local be1 =y-20
    local re2=xx+20
    local le2=xx-20
    local te2=yy+20
    local be2=yy-20
    if s2.re < s1.le or
    s2.le < s1.re or
    s2.be < s1.te or
    s2.te > s1.be then
        return true
    end
end

my code got messted up

@minersack27 - please don’t post comments in threads that are two years old, which have nothing to do with your problem. Start a new thread.

Also - put three ~ on the line before your code, and another three ~ on the line after your code, to format it nicely (you can edit your posts above to do this), otherwise nobody s going to be able to read your code at all

added the 3 ~'s before and after the code

@minersack27 Here’s your code changed a little. I don’t have the sprites you used so I picked some that’s in Codea. The girl sprite can overlap the boy sprite, but the boy sprite can’t overlap the girl sprite. A better way to do this is to use physics objects and let the physics code handle collisions.


-- gravity with walls

function setup()
    print("rugby rules")
    x = WIDTH/2
    y = HEIGHT/2
    xx = WIDTH/2-50
    yy = HEIGHT/2-50
    sound("Game Sounds One:1-2-3 Go")
end
function draw()
    background(0)
    sprite("Planet Cute:Grass Block",WIDTH/2, HEIGHT/2, WIDTH, HEIGHT)
    sprite("Planet Cute:Character Boy",x, y, 50)
    if not Overlapping() then
        x = (x + Gravity.x * 23)
        y = (y + Gravity.y * 23)
    end    
    sprite("Planet Cute:Character Pink Girl",xx, yy, 50)
    xx = (xx + Gravity.xx * 20)
    yy = (yy + Gravity.yy * 20)
    if x < 25 then
        x = 25
    elseif x > WIDTH - 25 then
        x = WIDTH - 25
    end
    if y < 25 then
        y = 25
    elseif y > HEIGHT - 25 then
        y = HEIGHT - 25
    end
    if xx < 20 then
        xx = 20
    elseif xx > WIDTH - 20 then
        xx = WIDTH - 20
    end
    if yy < 20 then
        yy = 20
    elseif yy > HEIGHT - 20 then
        yy = HEIGHT - 20
    end
end

function Overlapping()
    if math.abs(x-xx)<50 and math.abs(y-yy)<50 then
        return true
    else 
        return false  
    end
end

@minersack27 Here’s code using physics objects. It’s a lot easier to let the physics engine do the collision detection. To move the characters, slide your finger around the screen starting on their side of the field. Each character can be moved on their own.


supportedOrientations(PORTRAIT_ANY)
displayMode(FULLSCREEN)

function setup()
    sound("Game Sounds One:1-2-3 Go")
    rectMode(CENTER)
    physics.continuous=true
    c=physics.body(CHAIN,false,vec2(0,0),vec2(0,HEIGHT),
            vec2(WIDTH,HEIGHT),vec2(WIDTH,0),vec2(0,0))
    p1=physics.body(CIRCLE,20)
    p1.x=WIDTH/2
    p1.y=HEIGHT-100
    p1.gravityScale=0
    
    p2=physics.body(CIRCLE,20)
    p2.x=WIDTH/2-50
    p2.y=100
    p2.gravityScale=0
    
    b1=physics.body(CIRCLE,20)
    b1.x=WIDTH/2
    b1.y=HEIGHT/2
    b1.gravityScale=0  
    b1.restitution=1.1
    
    g1x,g1y=0,0
    g2x,g2y=0,0
end
function draw()
    background(0, 255, 24, 255)
    strokeWidth(3)
    line(0,HEIGHT/2,WIDTH,HEIGHT/2)
    noFill()
    ellipse(WIDTH/2,HEIGHT/2,200)
    rect(WIDTH/2,HEIGHT,200,100)
    rect(WIDTH/2,0,200,100)
    fill(255)
    ellipse(b1.x,b1.y,40)
    sprite("Planet Cute:Character Boy",p1.x, p1.y, 50)
    sprite("Planet Cute:Character Pink Girl",p2.x,p2.y, 50)
    p1.linearVelocity=vec2(g1x,g1y)
    p2.linearVelocity=vec2(g2x,g2y)
end

function touched(t)
    if t.y>HEIGHT/2 then
        g1x=g1x+t.deltaX*5
        g1y=g1y+t.deltaY*5
    else
        g2x=g2x+t.deltaX*5
        g2y=g2y+t.deltaY*5
    end
end