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!
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 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