Prevent joints from breaking.

General discussion about LÖVE, Lua, game development, puns, and unicorns.
Post Reply
rnodal
Prole
Posts: 9
Joined: Sun Oct 10, 2010 11:26 pm

Prevent joints from breaking.

Post by rnodal »

Hello all,

I'm currently experiencing the situation where my joints break apart. I'm trying to simulate a chain using the RevoluteJoint.
I had it understood that once you join two bodies, in order for them to separate you have to do it manually. Has anyone experienced
the situation before?

Any suggestions as to what the problem may be would be great.

-r
Rad3k
Citizen
Posts: 69
Joined: Mon Aug 08, 2011 12:28 pm

Re: Prevent joints from breaking.

Post by Rad3k »

rnodal wrote:Hello all,

I'm currently experiencing the situation where my joints break apart.
Oh, that's pretty bad. I'm afraid you have to go see a doctor. sorry, I had to :P

Seriously though, what do you mean by breaking apart? Do the links break apart completely, and start behaving like there was no joint at all, or the joints just "stretch" under a load?
rnodal
Prole
Posts: 9
Joined: Sun Oct 10, 2010 11:26 pm

Re: Prevent joints from breaking.

Post by rnodal »

:awesome:

The first case. The bodies start to behave as if they were not connected at all.
I think I may have fixed it but I'm not sure.

Here is the old code:

Code: Select all

local centerx, centery = link:getBody():getWorldCenter()
joint = love.physics.newRevoluteJoint(link:getBody(), lastLink:getBody(), centerx - (width *  0.5), centery)
joint:setCollideConnected(true)
and here is the new code:

Code: Select all

local centerx, centery = link:getBody():getWorldCenter()
joint = love.physics.newRevoluteJoint(link:getBody(), lastLink:getBody(), centerx - (width *  0.5), centery)
joint:setCollideConnected(true)
table.insert(chain.joints, joint)
It seems that lua was doing some garbage collection and maybe that was causing the joints to get destroyed?
I'm not 100% sure to be honest. Any more input would be great.

-r
User avatar
bartbes
Sex machine
Posts: 4946
Joined: Fri Aug 29, 2008 10:35 am
Location: The Netherlands
Contact:

Re: Prevent joints from breaking.

Post by bartbes »

joint seems to be a global, so if you run this multiple times it gets overwritten. This, in turn, means that there is no longer a reference to the joint and it will, at some point, get garbage collected, meaning it will be removed from the simulation as well.
rnodal
Prole
Posts: 9
Joined: Sun Oct 10, 2010 11:26 pm

Re: Prevent joints from breaking.

Post by rnodal »

Thank you for answering.

A little bit before the loop I have:

Code: Select all

local joint = 0
I'm sorry for omitting the piece of information.

Thank.

-r
Rad3k
Citizen
Posts: 69
Joined: Mon Aug 08, 2011 12:28 pm

Re: Prevent joints from breaking.

Post by Rad3k »

rnodal wrote:It seems that lua was doing some garbage collection and maybe that was causing the joints to get destroyed?
You're probably correct. If you don't store the references to physics objects somewhere, they will get destroyed when Lua garbage-collects them - it's not enough to have them inside a world to prevent this. Your change should be enough to fix it if you don't discard the 'chain' table afterwards.
rnodal
Prole
Posts: 9
Joined: Sun Oct 10, 2010 11:26 pm

Re: Prevent joints from breaking.

Post by rnodal »

So far so good with the change I made.
Thank you all for taking the time to help me.

-r
Post Reply

Who is online

Users browsing this forum: Google [Bot] and 0 guests