Page 1 of 1

[Solved] Physics - shapes get destroyed when collected

Posted: Wed Aug 10, 2011 12:10 am
by Rad3k
Hi there :)

I'm making a pong-like game that uses love.physics, and I've been getting some weird behavior. I've found out what was the cause and fixed it, but I still don't know whether this was a bug in Löve or was like that by design. That's how I encountered it:

I surrounded the playing field with invisible walls at the borders of the screen, and discarded references to shapes, because I didn't need to do anything with them later. I put a ball inside and set it bouncing around. I did some test runs, and each time, after some 2-3 minutes, the ball kept falling out of screen (like it was passing through walls). It happened even when the ball was going really slow, so it ruled out tunneling. After some time of wondering what may be causing this, I've found out. The walls were disappearing when Lua garbage-collected the shapes. When I explicitly ran garbage collector after creating walls, they were gone.

Now, is this the intended behavior? Should I always keep somewhere references to shapes I create? I initially thought that since shapes are always attached to a body (that itself is "attached" to world), it's not necessary to keep references to them.

Re: Physics - shapes get destroyed when collected

Posted: Wed Aug 10, 2011 12:12 am
by TechnoCat
Rad3k wrote:Hi there :)

I'm making a pong-like game that uses love.physics, and I've been getting some weird behavior. I've found out what was the cause and fixed it, but I still don't know whether this was a bug in Löve or was like that by design. That's how I encountered it:

I surrounded the playing field with invisible walls at the borders of the screen, and discarded references to shapes, because I didn't need to do anything with them later. I put a ball inside and set it bouncing around. I did some test runs, and each time, after some 2-3 minutes, the ball kept falling out of screen (like it was passing through walls). It happened even when the ball was going really slow, so it ruled out tunneling. After some time of wondering what may be causing this, I've found out. The walls were disappearing when Lua garbage-collected the shapes. When I explicitly ran garbage collector after creating walls, they were gone.

Now, is this the intended behavior? Should I always keep somewhere references to shapes I create? I initially thought that since shapes are always attached to a body (that itself is "attached" to world), it's not necessary to keep references to them.
It sometimes takes the Lua garbage collector a few minutes to collect the unreferenced shapes and free them.

Re: Physics - shapes get destroyed when collected

Posted: Wed Aug 10, 2011 6:32 am
by thelinx
Rad3k wrote: Now, is this the intended behavior? Should I always keep somewhere references to shapes I create?
Yes.

That's just how Lua works. If it can't find any references to a variable, it is scrapped.

Re: Physics - shapes get destroyed when collected

Posted: Wed Aug 10, 2011 9:41 am
by Rad3k
Thanks for answers :) I know how Lua garbage collector works, but I just thought that maybe shapes, bodies and other physical objects are managed internally by Box2d or something, so that unless you explicitly destroy them, they stay there.

Re: [Solved] Physics - shapes get destroyed when collected

Posted: Sat Aug 27, 2011 4:31 am
by vpdp_pc
No, they don't. That's why the tutorial of physics in wiki (http://love2d.org/wiki/Tutorial:Physics) uses a table to store all objects. And each objects keep their own shapes. So all objects and shapes are kept in a table and they never be collected by garbage collector.