1

Currently, the Cocos2d-Box2d project is using a b2Vec2 to create a Bounding Box for the edge of the game. Because of this, the bounding Box isn't affecting kinematic bodies, which are bodies that aren't affected by force(meaning that the bodies will usually fly off the screen). I'm trying to see if there's a way to either make the kinematic body connect with the screen. If not, i would appreciate it if someone explain to me how I should make a boundingbox with static bodies around the corner of the screens.

Wei Pan
  • 57
  • 7

1 Answers1

0

Here is two ways...Try any one

// Method - 1

b2BodyDef groundBodyDef;
groundBodyDef.position.Set(0, 0);  
b2Body              *mGroundBody ;


mGroundBody = self.world->CreateBody(&groundBodyDef);
NSString *strId = @"Ground Body";
mGroundBody->SetUserData(strId);

b2EdgeShape groundBox;      

    //bottom
    groundBox.Set(b2Vec2(0.0f,0.0f), b2Vec2(mS.width/PTM_RATIO,0.0f));
    mGroundBody->CreateFixture(&groundBox,0);

    // top
    groundBox.Set(b2Vec2(0,mS.height/PTM_RATIO), b2Vec2(mS.width/PTM_RATIO, mS.height/PTM_RATIO));
    mGroundBody->CreateFixture(&groundBox,0);


    // left
    groundBox.Set(b2Vec2(0,mS.height/PTM_RATIO), b2Vec2(0,0));
    mGroundBody->CreateFixture(&groundBox,0);

    // right
    groundBox.Set(b2Vec2(mS.width/PTM_RATIO,mS.height/PTM_RATIO), b2Vec2(mS.width/PTM_RATIO,0));
    mGroundBody->CreateFixture(&groundBox,0);

// Method - 2

//create 4 box2d walls...

    float bW = (IS_IPAD) ? (8) : 2 ;
    //top
    {
        b2BodyDef bodyDef;
        bodyDef.type = b2_staticBody;
        bodyDef.position.Set((mS.width*0.5f)/PTM_RATIO, (mS.height)/PTM_RATIO);
        bodyDef.linearDamping  = 0.0f;
        bodyDef.angularDamping = 0.0f;

        bodyDef.userData =  strId ;

        b2PolygonShape box;
        box.SetAsBox( ((mS.width*0.5f)/PTM_RATIO), (bW)/PTM_RATIO);

        b2FixtureDef fixDef;
        fixDef.shape = &box;
        fixDef.density = 1.0f;
        fixDef.friction = 0.1f;
        fixDef.restitution = 1.0f;
        fixDef.isSensor = false;

        b2Body *topBody = self.world->CreateBody(&bodyDef);
        topBody->CreateFixture(&fixDef);

    }

    //bottom
    {
        b2BodyDef bodyDef;
        bodyDef.type = b2_staticBody;
        bodyDef.position.Set((mS.width*0.5f)/PTM_RATIO, 0);
        bodyDef.linearDamping  = 0.0f;
        bodyDef.angularDamping = 0.0f;

        bodyDef.userData =  strId ;

        b2PolygonShape box;
        box.SetAsBox( ((mS.width*0.5f)/PTM_RATIO), (bW)/PTM_RATIO);

        b2FixtureDef fixDef;
        fixDef.shape = &box;
        fixDef.density = 1.0f;
        fixDef.friction = 0.1f;
        fixDef.restitution = 1.0f;
        fixDef.isSensor = false;

        b2Body *topBody = self.world->CreateBody(&bodyDef);
        topBody->CreateFixture(&fixDef);

    }

    //left
    {
        b2BodyDef bodyDef;
        bodyDef.type = b2_staticBody;
        bodyDef.position.Set(0, (mS.height*0.5f)/PTM_RATIO);
        bodyDef.linearDamping  = 0.0f;
        bodyDef.angularDamping = 0.0f;

        bodyDef.userData =  strId ;

        b2PolygonShape box;
        box.SetAsBox( ((bW)/PTM_RATIO), (mS.height*0.5f)/PTM_RATIO);

        b2FixtureDef fixDef;
        fixDef.shape = &box;
        fixDef.density = 1.0f;
        fixDef.friction = 0.1f;
        fixDef.restitution = 1.0f;
        fixDef.isSensor = false;

        b2Body *topBody = self.world->CreateBody(&bodyDef);
        topBody->CreateFixture(&fixDef);

    }

    //right
    {
        b2BodyDef bodyDef;
        bodyDef.type = b2_staticBody;
        bodyDef.position.Set((mS.width)/PTM_RATIO, (mS.height*0.5f)/PTM_RATIO);
        bodyDef.linearDamping  = 0.0f;
        bodyDef.angularDamping = 0.0f;

        bodyDef.userData =  strId ;

        b2PolygonShape box;
        box.SetAsBox( ((bW)/PTM_RATIO), (mS.height*0.5f)/PTM_RATIO);

        b2FixtureDef fixDef;
        fixDef.shape = &box;
        fixDef.density = 1.0f;
        fixDef.friction = 0.1f;
        fixDef.restitution = 1.0f;
        fixDef.isSensor = false;

        b2Body *topBody = self.world->CreateBody(&bodyDef);
        topBody->CreateFixture(&fixDef);
    }
Guru
  • 21,652
  • 10
  • 63
  • 102
  • Neither option seems to work. Anyway, I have reached a new problem. Originally, I thought that it was weird that the Box2d Kinematic Body doesn't collide with the Box2d Walls, but then I learned that Kinematic Bodies don't detect collision.... Which is weird because the Kinematic Body is colliding with a Dynamic Body I have... – Wei Pan Dec 15 '12 at 02:21
  • Nevermind, I figured it out. Kinematic Bodies still collide with Dynamic Bodies. – Wei Pan Dec 15 '12 at 02:49