[Solved] Physics - shapes get destroyed when collected

Questions about the LÖVE API, installing LÖVE and other support related questions go here.
Forum rules
Before you make a thread asking for help, read this.
Post Reply
Rad3k
Citizen
Posts: 69
Joined: Mon Aug 08, 2011 12:28 pm

[Solved] Physics - shapes get destroyed when collected

Post 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.
Last edited by Rad3k on Thu Aug 18, 2011 12:10 am, edited 1 time in total.
User avatar
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

Post 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.
Last edited by TechnoCat on Wed Aug 10, 2011 12:18 pm, edited 1 time in total.
User avatar
thelinx
The Strongest
Posts: 857
Joined: Fri Sep 26, 2008 3:56 pm
Location: Sweden

Re: Physics - shapes get destroyed when collected

Post 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.
Rad3k
Citizen
Posts: 69
Joined: Mon Aug 08, 2011 12:28 pm

Re: Physics - shapes get destroyed when collected

Post 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.
vpdp_pc
Prole
Posts: 15
Joined: Fri Jul 29, 2011 5:39 am

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

Post 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.
Post Reply

Who is online

Users browsing this forum: No registered users and 27 guests