What's the logic of contact.bodyA and contact.bodyB?

Is there a logic which object will be defined as contact.bodyA?
So far I always checked for both possibilities but now realised that some of my objects are ALWAYS contact.bodyB.

@Surfer I think one object defined before another object is bodyA, and the other is bodyB. So if you have a lot of objects created it would be hard to keep track of the order they were created to know which is bodyA and bodyB. I always check both possibilities.

@Surfer Here’s a little demo showing the order. I created an edge around the screen and 20 objects with their order number in info. When they collide, I print the value of bodyA.info and bodyB.info. You can see that bodyA.info is less than bodyB.info or edge prints before any number. Now the odd things is if you move the creation of the edges (CHAIN) to after the creation of the objects, edge still prints first. So it’s possible that some objects always come before others.

function setup()
    e1=physics.body(CHAIN,true,vec2(0,0),vec2(0,HEIGHT),
                vec2(WIDTH,HEIGHT),vec2(WIDTH,0))
    e1.info="edge"
    tab={}
    for z=1,20 do
        p=physics.body(CIRCLE,10)
        p.x=math.random(WIDTH)
        p.y=math.random(HEIGHT)
        p.restitution=1
        p.linearVelocity=vec2(200,200)
        p.info=tostring(z)
        p.gravityScale=0
        p.friction=0
        table.insert(tab,p)
    end
end

function draw()
    background(40, 40, 50)
    fill(255)
    for a,b in pairs(tab) do
        ellipse(b.x,b.y,20)
    end   
end

function collide(c)
    if c.state==BEGAN then
        print(c.bodyA.info,c.bodyB.info)
    end
end

Very good example! Thanks a lot for the effort you put into it!

I was curious how objects were defined as bodyA or bodyB in collisions, so I wrote this to see what happens. What I found out was a CHAIN object was always bodyA. An EDGE object was also always bodyA. CHAIN and EDGE object are fixed, so they don’t collide with each other. A CIRCLE is always bodyB and a POLYGON is bodyA when it collides with a CIRCLE and bodyB when it collides with a CHAIN or an EDGE. For multiple CIRCLES, the earlier one defined is bodyA, and the later is bodyB. The same goes for multiple POLYGONS. I’m not sure if this means anything, because I always check both bodyA and bodyB for a condition.

displayMode(FULLSCREEN)
supportedOrientations(LANDSCAPE_ANY)

function setup()
    physics.continuous=true
    tab={}
    p1Tab={}
    p2Tab={}
    for z=1,3 do
        p1=physics.body(CIRCLE,20)
        p1.x=math.random(WIDTH)
        p1.y=HEIGHT-50
        p1.info="circle "..z
        p1.restitution=1
        p1.friction=0
        table.insert(p1Tab,p1)
        p2=physics.body(POLYGON,vec2(0,0),vec2(100,0),vec2(100,100),vec2(0,100))
        p2.x=math.random(100,WIDTH-100)
        p2.y=600
        p2.info="polygon "..z 
        p2.restitution=1
        table.insert(p2Tab,p2)
    end
    p3=physics.body(CHAIN,true,vec2(0,0),vec2(100,0),vec2(100,100))
    p3.x=500
    p3.y=150
    p3.info="chain"    
    p4=physics.body(EDGE,vec2(0,0),vec2(WIDTH,0))
    p4.info="edge"
    p4.restitution=1
    p5=physics.body(EDGE,vec2(0,0),vec2(0,HEIGHT))
    p5.info="edge"
    p5.restitution=1
    p6=physics.body(EDGE,vec2(WIDTH,0),vec2(WIDTH,HEIGHT))
    p6.info="edge"
    p6.restitution=1
    p7=physics.body(EDGE,vec2(0,HEIGHT),vec2(WIDTH,HEIGHT))
    p7.info="edge"
    p7.restitution=1
    fill(255)
end

function draw()
    background(40, 40, 50)
    stroke(255)
    text("chain .. polygon\
chain .. circle\
edge .. polygon\
edge .. circle\
polygon .. circle",100,HEIGHT-50)
    strokeWidth(3)  
    for a,b in pairs(p2Tab) do 
        pushMatrix()
        translate(b.x,b.y)
        rotate(b.angle)
        j=#p2.points
        for z=1,#b.points do
            line(b.points[z].x,b.points[z].y,b.points[j].x,b.points[j].y)
            j=z
        end 
        translate()
        popMatrix()
    end
    pushMatrix()
    translate(p3.x,p3.y)
    rotate(p3.angle)
    j=#p3.points
    for z=1,#p3.points do
        line(p3.points[z].x,p3.points[z].y,p3.points[j].x,p3.points[j].y)
        j=z
    end 
    translate()
    popMatrix()   
    for a,b in pairs(p1Tab) do 
        ellipse(b.x,b.y,40)
    end
    text("bodyA .. bodyB",300,HEIGHT-25)
    cnt=0
    for a,b in pairs(tab) do
        cnt=cnt+1
        text(b,300,HEIGHT-20*cnt-50)
    end    
end

function collide(c)
    if c.state==BEGAN then
        str=c.bodyA.info.." .. "..c.bodyB.info
        tab[str]=str        
    end
end