Proper way to break joints

Official forum for the Chipmunk2D Physics Library.
digicide
Posts: 15
Joined: Wed Aug 20, 2008 8:11 pm
Contact:

Proper way to break joints

Post by digicide »

I'm storing a joint as part of a struct, and as a void *. Then, when I want to break the joint, I'm casting it to a (cpJoint*). I've tried cpJointFree(theJoint) and cpJointDestroy(theJoint). Destroy seems to have no effect, and free crashes- the debugger shows that it hangs on trying to prestep joints. It looks to me like the reference to the joint hasn't been removed from the joints array.

Am I supposed to do that manually? Or should it be as easy as just calling the free or destroy function?
Android_X
Posts: 18
Joined: Wed Oct 31, 2007 7:55 pm
Contact:

Re: Proper way to break joints

Post by Android_X »

Did you remember to remove the joint from the space( with cpSpaceRemoveJoint) before freeing it?

Not removing shapes/bodies/joints from the space before freeing them will probably cause a segfault/crash.

Also a good thing to remember from the docs:
"When removing objects from the space, make sure you remove any other objects that reference it. For instance, when you remove a body, remove the joints and shapes attached to it."

Regards,
AX
digicide
Posts: 15
Joined: Wed Aug 20, 2008 8:11 pm
Contact:

Re: Proper way to break joints

Post by digicide »

Perfect. I had a feeling it was something simple I'd never learned. Thanks!
pabloruiz55
Posts: 22
Joined: Mon May 18, 2009 7:31 pm
Contact:

Re: Proper way to break joints

Post by pabloruiz55 »

Hi!

I am having a problem while breaking joints.

In my game, the player moves and as it touches some objects they "attach" to him.

For this, i create a joint between each touched object (10 objects max right now) and the player.

Then, if it reaches some point i want to reset those objects' positions and remove the joints. Here is where the problem comes...

Most times it works,but sometimes the objects' positions get reseted but the joint does not seem to break (i notice this since as soon a the objects move, they come back flying near the player...)

The more objects attached the more frequently it happens...

In a collision callback i call a function that does the following.

for(Object *b in game->objects)
{
if(b->carried)
{
cpSpaceRemoveJoint(game->space, b->joint);
cpJointDestroy(b->joint);
cpJointFree(b->joint);
b->carried = NO;
;

}
}
b->joint is a class member of the object that i use to store the joint so i can get it here to remove it...
resetPosition moves the object outside the screen preparing it to appear again. it just changes its position and velocity.

Hope you can help me figure this out.

Thanks!
pabloruiz55
Posts: 22
Joined: Mon May 18, 2009 7:31 pm
Contact:

Re: Proper way to break joints

Post by pabloruiz55 »

Any ideas? :mrgreen:
User avatar
slembcke
Site Admin
Posts: 4166
Joined: Tue Aug 14, 2007 7:13 pm
Contact:

Re: Proper way to break joints

Post by slembcke »

First of all, you shouldn't call both destroy and free. You only need to call destroy if you allocated the memory for the object, otherwise the free function will also destroy it for you. Other than that I'm not quite sure. Are you sure you aren't adding more than one joint?
Can't sleep... Chipmunks will eat me...
Check out our latest projects! -> http://howlingmoonsoftware.com/wordpress/
pabloruiz55
Posts: 22
Joined: Mon May 18, 2009 7:31 pm
Contact:

Re: Proper way to break joints

Post by pabloruiz55 »

I added the destroy and free to check it wasn't because of that, but having them or not did nothing different...but thanks for pointing that out.

I am pretty sure i have only one joint per object.

When the object collide i check if it has a joint, if not i create it.
The same when i should break it, for each object if they have a joint i remove it...

Could it have to do with the fact i am removing the joint in the collision callback?

I read that for the elimination of objects i would have to create a list and outside the callback remove them... maybe it applies to joints also?
User avatar
slembcke
Site Admin
Posts: 4166
Joined: Tue Aug 14, 2007 7:13 pm
Contact:

Re: Proper way to break joints

Post by slembcke »

No, it's safe to remove joints in a collision callback. Really it's just shapes and bodies that cause problems because they may have already been tagged by the collision detection when you remove them. Removing the joint will remove the joint. I'd still put money that you are accidentally adding more than one. Add some logging statements maybe. The only way you would notice if you added more than one is by removing one and having it still be jointed or to add so many of them it becomes a performance problem.
Can't sleep... Chipmunks will eat me...
Check out our latest projects! -> http://howlingmoonsoftware.com/wordpress/
pabloruiz55
Posts: 22
Joined: Mon May 18, 2009 7:31 pm
Contact:

Re: Proper way to break joints

Post by pabloruiz55 »

Hi! I was able to solve this problem apparently! Thanks! :mrgreen:

I am posting what was happening in case someone finds it useful...

The problem arised when the player was colliding with the object (that had the joint) and the destination that was supposed to remove it...

So, the 3 objects colliding, the collision callback between the object and destination was called AND the one between the object and the player was called too.

As soon as the joint was removed, i was created again as it seems so...

So i solved it by flaging the object and then at the end of the update loop allowing it to be jointed again.

I don't know if there is another way of solving this... i think it has to do with the order in which the colision callbacks where called... I don't know how that stuff works... :oops:

Anyway! Many thanks!
User avatar
slembcke
Site Admin
Posts: 4166
Joined: Tue Aug 14, 2007 7:13 pm
Contact:

Re: Proper way to break joints

Post by slembcke »

No, unfortunately that's probably the only way to do it. I really really hate boolean flags personally. It seems that any time that they are used there is probably a better way of solving something, but I don't think there is in this case.

The order that collision pairs are reported in is going to be fairly random and changes as objects move around.
Can't sleep... Chipmunks will eat me...
Check out our latest projects! -> http://howlingmoonsoftware.com/wordpress/
Post Reply

Who is online

Users browsing this forum: No registered users and 7 guests