Box2D Forums

It is currently Thu May 23, 2013 9:04 pm

All times are UTC - 8 hours [ DST ]




Post new topic Reply to topic  [ 4 posts ] 
Author Message
PostPosted: Fri Aug 07, 2009 12:39 pm 
Offline

Joined: Thu Apr 09, 2009 9:04 am
Posts: 8
Hi,
I seem to be misunderstanding how to destroy joints and bodies. I have an ragdoll actor with multiple members and connecting joints (revolute). When a limb/member takes enough damage, I destroy one joint and the member detaches from the main body. When a leg is damaged then it detaches from pelvis, for example. But if damaged too much, the leg body is destroyed.

So, my code basically looks like:

Code:
if (rightLegHitPoints  < 0 && !rightLegDetached) {
   rightLegDetached = 1;
   world->DestroyJoint(rightHip);
   rightHip = NULL;
}

// then later on if the detached leg continues to
// recieve damage, I destroy the leg body
if (rightLegHitPoints < -50) {
   world->DestroyBody(rightLeg); // THIS LINE seems to the issue
   rightLeg = NULL;
}


This call to DestroyBody seems to cause a problem when it calls
b2World::DestroyJoint in the //'Remove from the doubly linked list'
area (j->m_prev->m_next = j->m_next;). I'm guessing this happens since I already destroyed the one of the joints on the body?

What is the right way to do this? Is it my job to remove the joint from the body's joint list when I explicitly destroy the joint?

Any tips appreciated.


Top
 Profile  
 
PostPosted: Fri Aug 07, 2009 1:59 pm 
Offline

Joined: Mon Jan 07, 2008 10:51 am
Posts: 1911
You do not need do more that this. I think it most likely you are attempting to delete a body or joint twice. Bear in mind all joints on a body will be destroyed when the body is.


Top
 Profile  
 
PostPosted: Fri Aug 07, 2009 9:50 pm 
Offline

Joined: Thu Apr 09, 2009 9:04 am
Posts: 8
Indeed, you are right as usual. I see now that I am trying to destroy a joint twice, but I thought my code would handle it properly - famous last words! Here is what was happening.

I destroy the pelvis body, thus implicitly the right hip and left hip are also destroyed. I have the implicit destruction listener that looks like this
Code:
SayGoodbye(b2Joint* j) {
  j = NULL;
}


Then sometime later I try to destroy the right hip and boom goes the dynamite. Even though I'm checking for a null joint thusly:

Code:
// here i would expect that rightHip is NULL
// since I set it to null in the SayGoodbye method above
// during implicit destruction, but somehow it is not null!
if (rightHip != NULL){
  world->DestroyJoint(rightHip);
  rightHip = NULL;
}


So, this leads me to believe that the SayGoodbye method is incorrect somehow. Not sure why, I just want to be sure to nullify the pointers on implicit destruction...
Any help is appreciated. Thx


Top
 Profile  
 
PostPosted: Sat Aug 08, 2009 2:39 am 
Offline

Joined: Mon Jan 07, 2008 10:51 am
Posts: 1911
Your saygoodbye function does nothing. Read a good article on pointers and you'll see why.


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 4 posts ] 

All times are UTC - 8 hours [ DST ]


Who is online

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


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Powered by phpBB® Forum Software © phpBB Group