Search code examples
cocos2d-iphonebox2d

specific body shape in cocos2d


I have sprites(which have bodies) that can fall down into a box, in a b2world.

I wonder in what way should I define that box body, so only when another body hit it from upside, it looks like he went inside that box, and I don't see it anymore ..

should I just put it in another z layer? how can I define a body to be open in its upside only ?

I have created the body like this:

-(void)basket //TAG5
{
    //define sprite
    basket=[CCSprite spriteWithFile:@"basket.png"];
    basket.tag=5;
    basket.position=ccp(200,50);
    b2BodyDef spriteBodyDef;
    spriteBodyDef.type = b2_staticBody;
    spriteBodyDef.position.Set(basket.position.x/PTM_RATIO,basket.position.y/PTM_RATIO);
    spriteBodyDef.userData = basket;
    basket1Body = world->CreateBody(&spriteBodyDef);

    b2PolygonShape spriteShape; //b2polygon-for box shape
    spriteShape.SetAsBox(basket.contentSize.width/PTM_RATIO/2,basket.contentSize.height/PTM_RATIO/2); //for b2polygon
    b2FixtureDef spriteShapeDef;
    spriteShapeDef.shape = &spriteShape;
    spriteShapeDef.density = 10.0;
    spriteShapeDef.isSensor = false;
    stand1Body->CreateFixture(&spriteShapeDef);
    [self addChild:basket]; 
}

I am using cocos2d and box2d. any explanation of a simple way of doing this will be appreciate .


Solution

  • To make the box you could use three polygons (left and right walls, and bottom), or edge shapes. Then yes, just draw something over the top so that the objects are hidden when they go into that area.