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!
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!
so, I'm failing to see where "bud->numJointsAttached = 0;" is being updated each time that body has a joint attached to it.
well yeah, you would need to take care of that somewhere too - I was just giving you the overall idea.
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
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.
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);
}