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.
[Solved] Physics - shapes get destroyed when collected
Forum rules
Before you make a thread asking for help, read this.
Before you make a thread asking for help, read this.
[Solved] Physics - shapes get destroyed when collected
Last edited by Rad3k on Thu Aug 18, 2011 12:10 am, edited 1 time in total.
- TechnoCat
- Inner party member
- Posts: 1611
- Joined: Thu Jul 30, 2009 12:31 am
- Location: Denver, CO
- Contact:
Re: Physics - shapes get destroyed when collected
It sometimes takes the Lua garbage collector a few minutes to collect the unreferenced shapes and free them.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.
Last edited by TechnoCat on Wed Aug 10, 2011 12:18 pm, edited 1 time in total.
Re: Physics - shapes get destroyed when collected
Yes.Rad3k wrote: Now, is this the intended behavior? Should I always keep somewhere references to shapes I create?
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
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
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.
Who is online
Users browsing this forum: Bing [Bot], Google [Bot] and 3 guests