Zohaib
Zohaib

Reputation: 2865

Adding box2d Body on Sprite Cocos2d

I have a bucket in which i want to add box2d body . not on whole bucket but on left, right and bottom so i can throw my ball inside bucket. Here is my Bucket. I have added box2d body on left and right side of my bucket. like this

enter image description here

It is working fine for me But when i add body on bottom than my game is crashing.

Here is my code for adding 2 body on bucket with comment bottom body as well.

-(b2Body *) createBucket
    {
        CCSprite* bucket = [CCSprite spriteWithFile:@"simple-bucket-md.png"];
        [self addChild:bucket z:3];

        b2Body* b_bucket;

        //set this to avoid updating this object in the tick schedule
        bucket.userData = (void *)YES;

        b2BodyDef bodyDef;
        bodyDef.type = b2_staticBody;
        CGPoint startPos = ccp(400,150);
        bodyDef.position = [self toMeters:startPos];
        bodyDef.userData = bucket;
        bodyDef.gravityScale = 0;

        b2PolygonShape dynamicBox;
            //----------------------------------
        // THis IS body for Left Side
            //----------------------------------
        int num = 5;
        b2Vec2 verts[] = {
            b2Vec2(-29.1f / PTM_RATIO, 25.0f / PTM_RATIO),
            b2Vec2(-25.4f / PTM_RATIO, -14.9f / PTM_RATIO),
            b2Vec2(-18.7f / PTM_RATIO, -14.9f / PTM_RATIO),
            b2Vec2(-21.8f / PTM_RATIO, 26.7f / PTM_RATIO),
            b2Vec2(-28.9f / PTM_RATIO, 25.1f / PTM_RATIO)
        };

        dynamicBox.Set(verts, num);

        b2FixtureDef fixtureDef;
        fixtureDef.shape = &dynamicBox;
        fixtureDef.friction = 0.7;
        fixtureDef.density = 10.0f;
        fixtureDef.restitution = 0.7;

        b_bucket = world->CreateBody(&bodyDef);
        b_bucket->CreateFixture(&fixtureDef);
        //----------------------------------
        // THis IS body for Right Side
        //----------------------------------
        int num1 = 5;
        b2Vec2 verts1[] = {
            b2Vec2(16.8f / PTM_RATIO, 27.0f / PTM_RATIO),
            b2Vec2(15.9f / PTM_RATIO, -11.5f / PTM_RATIO),
            b2Vec2(22.1f / PTM_RATIO, -10.7f / PTM_RATIO),
            b2Vec2(24.6f / PTM_RATIO, 26.9f / PTM_RATIO),
            b2Vec2(16.9f / PTM_RATIO, 26.7f / PTM_RATIO)
        };


        dynamicBox.Set(verts1, num1);
        fixtureDef.shape = &dynamicBox;
        b_bucket-> CreateFixture(&fixtureDef);

        //----------------------------------
        // THis IS body for Bottom 
        //----------------------------------
        /*
        int num2 = 5;
        b2Vec2 verts2[] = {
            b2Vec2(-23.0f / PTM_RATIO, -21.6f / PTM_RATIO),
            b2Vec2(18.9f / PTM_RATIO, -21.0f / PTM_RATIO),
            b2Vec2(18.2f / PTM_RATIO, -26.1f / PTM_RATIO),
            b2Vec2(-22.8f / PTM_RATIO, -25.9f / PTM_RATIO),
            b2Vec2(-23.0f / PTM_RATIO, -21.7f / PTM_RATIO)
        };
        dynamicBox.Set(verts2, num2);
        fixtureDef.shape = &dynamicBox;
        b_bucket-> CreateFixture(&fixtureDef);
        */

        return b_bucket;
    }

Upvotes: 1

Views: 610

Answers (1)

ssantos
ssantos

Reputation: 16536

Your vertices are clockwise ordered, while they should be counter-clockwise.

You must create polygons with a counter clockwise winding (CCW). We must be careful because the notion of CCW is with respect to a right-handed coordinate system with the z-axis pointing out of the plane. This might turn out to be clockwise on your screen, depending on your coordinate system conventions.

Counter clock-wise vertices

Also, in order to create a 4 sides polygon (which it seems what you're trying), you just need 4 vertices. Try something like this.-

int num2 = 4;
b2Vec2 verts2[] = {
    b2Vec2(-22.8f / PTM_RATIO, -25.9f / PTM_RATIO),
    b2Vec2(18.2f / PTM_RATIO, -26.1f / PTM_RATIO),
    b2Vec2(18.9f / PTM_RATIO, -21.0f / PTM_RATIO),
    b2Vec2(-23.0f / PTM_RATIO, -21.6f / PTM_RATIO)        
};
dynamicBox.Set(verts2, num2);
fixtureDef.shape = &dynamicBox;
b_bucket-> CreateFixture(&fixtureDef);

Notice that you can also remove the fifth vertex of your lateral bodies.

Upvotes: 2

Related Questions