Physics.body.info

Can I use body.info to store my own data associated with the body? If so then I think there’s a bug. The code below should never print false but if I leave it running for 20-30 seconds, it eventually does.

Edit: changed the code to trigger the issue sooner. Within a few secs it will print false

function setup()
    nextT = 0
    bodies = {}
    physics.gravity(vec2(0,-10000))
end

function draw()
    if ElapsedTime > nextT then
        for _,body in ipairs(bodies) do body:destroy() end
        bodies = nil
        bodies = makeBodies()
        nextT = ElapsedTime + .1
    end
end

function makeBodies()
    local bodies = {}
    local box = physics.body(POLYGON,vec2(0,0),vec2(10,0),vec2(10,10),vec2(0,10))
    box.x = 20
    box.y = 361
    box.info = {mytype="box"}
    table.insert(bodies,box)
    
    local ground = physics.body(POLYGON,vec2(0,0),vec2(WIDTH,0),vec2(WIDTH,20),vec2(0,20))
    ground.x = 0
    ground.y = 360
    ground.type = STATIC
    ground.info = {mytype="ground"}
    table.insert(bodies,ground)
    return bodies
end

function collide(contact)
    if contact.bodyA.info==contact.bodyB.info then 
        print(contact.bodyA==contact.bodyB,ElapsedTime)
    end
end

@ruilov I encountered the bug when writing a physics example but i haven’t been able to track it down yet. There’s a global table that maps from integers to objects internally to allow the c code to keep references to arbitrary objects. I’m going to have a go at tracking it down. This code should come in handy, thanks!

cool, I don’t really understand how it works, but why does it not just keep track of a pointer to the data?

With lua you can’t keep a reference to pointers directly because they can change during garbage collection, so there’s a reference system for that. There may be an issue with other parts of Codea mucking around with it though.

I also ran inti this bug. But fun app to code in the sofa with :slight_smile:

Also ran into that bug. Here is the whole program to recreate it:

function setup()
    circle = physics.body(CIRCLE,10)
    circle.info = 0
    print(circle.info)
end

If you store information in a table and then save the table in circle.info, it works.

function setup()

    tab={"qwerty","asdfg","zxcvb"}

    circle = physics.body(CIRCLE,10)
    circle.info=tab

    for x,y in pairs(circle.info) do
        print(x,y)
    end

end

I have this problem too.
It seems that all bodies share the same info except if you make like Dave1707 did, using a temporary table. However that’s a bit annoying…

I’m not sure what info you want to save, but this is probably the way I should have shown it. You can save whatever you want in info, it just has to be in the form of table entries.


function setup() 
    c1 = physics.body(CIRCLE,50)
    c1.info={"It can be a single table entry"}
    
    c2 = physics.body(CIRCLE,50)
    c2.info={"or it ","can be","multiple","table","entries"}
    
    print("\
info for c1")
    for a,b in pairs(c1.info) do
        print(a,b)
    end
    
    print("\
info for c2")
    for a,b in pairs(c2.info) do
        print(a,b)
    end
end

function draw()
    background(30, 30, 30, 25)
end