Cocos2D Box2d OOP- how do you do it???

Long time reader, first time poster.

I can't seem to combine OOP in Objective-C with Box2D. I'm making a zombie game with a bunch of ragdolls, each having 10 bodies. I want to have a "Zombie" class (and subsequent subclasses) to keep them organized because each has slightly different behavior, but as soon as I put my zombie-maker code into a class other than my main game class, the app breaks!

The problem seems to be creating new fixtures from the external class. I can log whether the world is locked in the Zombie class, and it says no, but if I try to create a fixture on the next line the app crashes saying that the world is locked! Bah!

I made another game before and worked around this by putting all my code for the whole game in one, massively ugly class. Ideally, I wouldn't have to do that again.

Maybe I'm managing this the wrong way. So I guess my question comes down to this- how do I manage box2d bodies using OOP without having 10,000 lines of code in my main class?
Here's my source code in the Zombie class:

Code: Select all

-(void)makeBodies
{
    CGPoint p = self.position;
    b2Body *zB = [self getBodyForShape:@"z1body" atPosition:p zPos:0];
     b2Body *zH = [self getBodyForShape:@"z1head" atPosition:ccp(p.x+15,p.y+52) zPos:1];
     b2Body *zRau = [self getBodyForShape:@"z1rau" atPosition:ccp(p.x-15,p.y+40) zPos:1];
     b2Body *zRal = [self getBodyForShape:@"z1ral" atPosition:ccp(p.x-25,p.y+25) zPos:1];
     b2Body *zLau = [self getBodyForShape:@"z1lau" atPosition:ccp(p.x+12,p.y+40) zPos:-1];
     b2Body *zLal = [self getBodyForShape:@"z1lal" atPosition:ccp(p.x+15,p.y+20) zPos:0];
     b2Body *zRlu = [self getBodyForShape:@"z1rlu" atPosition:ccp(p.x-5,p.y+2) zPos:1];
     b2Body *zRll = [self getBodyForShape:@"z1rll" atPosition:ccp(p.x-8,p.y-28) zPos:1];
     b2Body *zLlu = [self getBodyForShape:@"z1llu" atPosition:ccp(p.x+5,p.y+2) zPos:0];
     b2Body *zLll = [self getBodyForShape:@"z1lll" atPosition:ccp(p.x+15,p.y-22) zPos:0];
     [self makeJointWithBody:zB body2:zH limit1:-.1 limit2:.1];
     [self makeJointWithBody:zB body2:zRau limit1:-.2 limit2:.2];
     [self makeJointWithBody:zRau body2:zRal limit1:-.2 limit2:.2];
     [self makeJointWithBody:zB body2:zLau limit1:-.2 limit2:.2];
     [self makeJointWithBody:zLau body2:zLal limit1:-.2 limit2:.2];
     [self makeJointWithBody:zB body2:zRlu limit1:-.1 limit2:.1];
     [self makeJointWithBody:zRlu body2:zRll limit1:-.1 limit2:.1];
     [self makeJointWithBody:zB body2:zLlu limit1:-.1 limit2:.1];
     [self makeJointWithBody:zLlu body2:zLll limit1:-.1 limit2:.1];
     
}

-(void)makeJointWithBody: (b2Body *)body1 body2:(b2Body *)body2 limit1:(float)limit1 limit2:(float)limit2
{
    b2RevoluteJointDef jointDef;
   
    jointDef.Initialize(body1,body2,body2->GetPosition());
    if (limit1 != 0 || limit2 != 0) {
        jointDef.lowerAngle = limit1 * b2_pi; // -90 degrees
       
        jointDef.upperAngle = limit2 * b2_pi; // 45 degrees
       
        jointDef.enableLimit = true;
    }
   
    world->CreateJoint(&jointDef);
}


-(b2Body *)getBodyForShape: (NSString *)shape atPosition:(CGPoint)p zPos:(int)z
{
    CCSprite *sprite = [CCSprite spriteWithSpriteFrameName:[NSString stringWithFormat:@"%@.png",shape]];
    sprite.position = p;
    [self addChild:sprite z:z];
   // Define the dynamic body.
   //Set up a 1m squared box in the physics world
   b2BodyDef bodyDef;
   bodyDef.type = b2_dynamicBody;
   
   bodyDef.position.Set(p.x/PTM_RATIO, p.y/PTM_RATIO);
   bodyDef.userData = sprite;
   
   b2Body *body = [(Game *)self.parent makeBodyWithDef:bodyDef];
   
   
    [[GB2ShapeCache sharedShapeCache] addFixturesToBody:body forShapeName:shape];
    [sprite setAnchorPoint:[[GB2ShapeCache sharedShapeCache] anchorPointForShape:shape]];
    return body;
}
trockr wrote:...whether the world is locked in the Zombie class, and it says no, but if I try to create a fixture on the next line the app crashes saying that the world is locked
That doesn't sound right at all, I think you should look into that a bit more. Is there any possibility the world is stepping while you are making the fixtures, eg from another thread or a timer? Is it only for fixtures and not when you create the bodies themselves? Your code doesn't seem to show where you actually create the fixtures...
The fixtures are added here:

-(b2Body *)getBodyForShape: (NSString *)shape atPosition:(CGPoint)p zPos:(int)z
{
// other code
[[GB2ShapeCache sharedShapeCache] addFixturesToBody:body forShapeName:shape];
// other code
}

GB2ShapeCache is handling all the shape/fixture creation. I made them using the PhysicsEditor app.

If I disable the Timer before creating the Zombie object, and enable it afterwords, it still crashes. But not for the same reason. I'm getting a bad access error (see attached screenshot). I've had a million of those before, but I'm not sure what's wrong this time. Is there something that should always be retained when working with box2d that normally wouldn't need a retain call?
Alright, I fixed it. For some reason, the world was NULL in the Zombie class. I was getting the reference by calling a getter method (getWorld:) in the Game class that returned the world. When I passed the world in from the Game to the Zombie in an initWithWorld: method, it just worked. Still doesn't make sense to me, but oh well.

Thanks for helping me on this!
Hey so I'm having a similar problem, Just trying to figure this out, in my project in the hello world layer I create a world and all that jazz, in that same layer I add a body and a bunch of fixtures I put them in a method in the hello world layer and it works everything compiles and runs great, but in trying to make the add bodies method separate in their own class, I get an error saying world is not declared in my body class, how do i get my class to accses the world of another layer?

Original thread: https://box2d.org/forum/viewtopic.php?t=7132