Box2D Forums

It is currently Thu May 23, 2013 6:50 pm

All times are UTC - 8 hours [ DST ]




Post new topic Reply to topic  [ 8 posts ] 
Author Message
PostPosted: Thu Aug 25, 2011 11:01 pm 
Offline

Joined: Thu Aug 25, 2011 10:42 pm
Posts: 6
So I'm trying to create a distance joint between two bodies after they collide. And I've done this using the contact listener in the tick method. My problem is that I would like to limit the number of collision-joints each body can have. for example bodyA could collide with bodyB, they join, then bodyA collides with a third body, a second joint forms, after which point bodyA is no long eligible for joints.

Presumably I would tag the bodies somehow after the first collision happens, and then just use if statements to do the rest, I just don't know how that code would look or if it's even possible to add this kind of meta tag to bodies.

I'll mention here that all of the bodies in my scene are duplicates of the same b2BodyDef, therefore I can't use a sprite tag or userdata or anything to help filter. And even if I could that wouldn't store the info of whether or not the particular body has already had a collision-joint event.

If any of this drivel makes sense, I would REALLY appreciate some help because I've hit a brick wall

cheers!


Top
 Profile  
 
PostPosted: Thu Aug 25, 2011 11:24 pm 
Offline

Joined: Tue Jun 24, 2008 8:25 pm
Posts: 1517
Location: Tokyo
Can't use user data???
How about this, I think it's a typical way to handle things (just written off the top of my head, could contain typos+errors :D ):
Code:
//set a struct in the user data
struct bodyUserData {
   int numJointsAttached;
   ... other useful stuff...
};

//on body creation
bodyUserData* bud = new bodyUserData();
bud->numJointsAttached = 0;
body->SetUserData( bud );

//later, in collision callback etc
b2Body* body = contact->GetFixtureA()->GetBody();
bodyUserData* bud = (bodyUserData*)body->GetUserData();
if ( bud ) {//unless you set a bodyUserData in EVERY body, you should check this
   if ( bud->numJointsAttached < 2 ) {
      ...do cool stuff...
   }
}

//at body destruction
bodyUserData* bud = (bodyUserData*)body->GetUserData();
if ( bud )
   delete bud;
world->DestroyBody( body );


Top
 Profile  
 
PostPosted: Thu Aug 25, 2011 11:30 pm 
Offline

Joined: Thu Aug 25, 2011 10:42 pm
Posts: 6
well then, obviously I CAN use user data, if I knew what the heck I was doing! haha

Thanks much, I'll see where this takes me!

cheers!


Top
 Profile  
 
PostPosted: Thu Aug 25, 2011 11:34 pm 
Offline

Joined: Thu Aug 25, 2011 10:42 pm
Posts: 6
so, I'm failing to see where "bud->numJointsAttached = 0;" is being updated each time that body has a joint attached to it.


Top
 Profile  
 
PostPosted: Thu Aug 25, 2011 11:48 pm 
Offline

Joined: Tue Jun 24, 2008 8:25 pm
Posts: 1517
Location: Tokyo
well yeah, you would need to take care of that somewhere too - I was just giving you the overall idea.


Top
 Profile  
 
PostPosted: Thu Aug 25, 2011 11:57 pm 
Offline

Joined: Thu Aug 25, 2011 10:42 pm
Posts: 6
which I appreciate. I just didn't know if its possible to update userData of a previously created body within the tick method. I just assumed it wasn't which is why I figured userdata wouldn't help. hence the question in the first place


Top
 Profile  
 
PostPosted: Fri Aug 26, 2011 2:35 am 
Offline

Joined: Tue Jun 24, 2008 8:25 pm
Posts: 1517
Location: Tokyo
Sure, that's no problem - since you created the user data you can change it at any time you like. The caution about changing things within a time step is for things that Box2D manages.

However, I'm assuming you would increment the numJointsAttached value when you create a joint, which should be outside the time step anyway.


Top
 Profile  
 
PostPosted: Mon Feb 20, 2012 6:16 pm 
Offline

Joined: Wed Jan 11, 2012 9:16 am
Posts: 27
Location: Canada
I have the identical issue about limiting joints. Following your instructions I was able to create a structure for the body, i.e.,

//set a struct in the user data
struct bodyUserData {
int numJointsAttached;
int maxJoints;
//not sure if I need to add more to this list
};

But ran into problems when I tried to use it when creating the bodies, i.e.,

for(int i = 0; i < 4; i++) {

static int padding=20;

// Create block and add it to the layer
CCSprite *block = [CCSprite spriteWithFile:@"X.png"];
int xOffset = padding+block.contentSize.width/2+
((block.contentSize.width+padding)*i);
block.position = ccp(xOffset, 300);
block.tag = 1;
[self addChild:block];

// Create block body
b2BodyDef blockBodyDef;
blockBodyDef.type = b2_dynamicBody;
blockBodyDef.position.Set(xOffset/PTM_RATIO, 300/PTM_RATIO);
blockBodyDef.userData = block;

//below is where I tried to add the new struct but it kept complaining...

bodyUserData *bud = new bodyUserData();
bud->numJointsAttached = 0;
bud->maxJoints = 1;

//i.e., if I remove the above chunk it compiles and runs, but not with it.

b2Body *blockBody = world->CreateBody(&blockBodyDef);

// Create block shape
b2PolygonShape blockShape;
blockShape.SetAsBox(block.contentSize.width/PTM_RATIO/2,
block.contentSize.height/PTM_RATIO/2);

// Create shape definition and add to body
b2FixtureDef blockShapeDef;
blockShapeDef.shape = &blockShape;
blockShapeDef.density = 0.1;
blockShapeDef.friction = 0.0;
blockShapeDef.restitution = 1.0f;
blockBody->CreateFixture(&blockShapeDef);

}

Just wondering how you combine the new bodyUserData structure into the create body code (loop) above?

Also, you mentioned putting the Callback function outside of the step function (no wonder mine keeps crashing!)...I tried doing this, but am unclear about passing bodies to it...here's what I tried and it complains:

-(void) createJoint: (b2Body) bodyA: (b2Body) bodyB {

//I think I have to declare copies of bodyA ad bodyB here but don't know how

b2WeldJointDef weldJointDef;
weldJointDef.Initialize(bodyA, bodyB, b2Vec2(230.0f/PTM_RATIO,(155.0f)/PTM_RATIO));
weldJointDef.collideConnected = false;
weldJoint = (b2WeldJoint*)world->CreateJoint(&weldJointDef);
}


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

All times are UTC - 8 hours [ DST ]


Who is online

Users browsing this forum: Google [Bot] and 1 guest


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