ohho
ohho

Reputation: 51941

How to define irregular-shaped sprites inside a Cocos2D physics world

I'd like to create sprites out of the 26 English characters and let them collide with each other inside a frame (for example, like X and E in diagram below).

enter image description here

What's the recommended way to create the 26 shapes?

Upvotes: 0

Views: 1184

Answers (1)

rptwsthi
rptwsthi

Reputation: 10172

You take 26 image Sprite for each character of the alphabet (.png image will be prefered), then define the fixture of each character, (with the help of vertexHelper Refer THIS Link for knowing more about vertextHelper), like for 'I' it will be:

 //OBSTACLE_Adjustments
    {
        CCSprite *sprite = [CCSprite spriteWithFile:@"I.png"];
        sprite.position = ccp(96,96);
        sprite.tag = 2;
        [self addChild:sprite];

        //SHAPE 1:
        {
            b2BodyDef bodyDefinition;
            bodyDefinition.type = b2_dynamicBody;
            bodyDefinition.position.Set(96/PTM_RATIO, 96/PTM_RATIO);
            bodyDefinition.userData = sprite;

            obstacle_AdjustmentsBody= world->CreateBody(&bodyDefinition);

            // Create body shape
            b2PolygonShape bodyShape;
            //row 1, col 1
            int num = 4;
            b2Vec2 verts[] = {
                b2Vec2(6.5f / PTM_RATIO, -25.0f / PTM_RATIO),
                b2Vec2(6.5f / PTM_RATIO, 27.0f / PTM_RATIO),
                b2Vec2(-7.5f / PTM_RATIO, 27.0f / PTM_RATIO),
                b2Vec2(-7.5f / PTM_RATIO, -26.0f / PTM_RATIO)
            };

            bodyShape.Set(verts, num);

            // Create shape definition and add to body
            b2FixtureDef bodyFixtureDefinition;
            bodyFixtureDefinition.shape = &bodyShape;
            bodyFixtureDefinition.density = 100.0f;
            bodyFixtureDefinition.friction = 0.01f;
            bodyFixtureDefinition.restitution = 0.50f;              
            obstacle_AdjustmentsFixture=obstacle_AdjustmentsBody->CreateFixture(&bodyFixtureDefinition);            
        }
}

here obstacle_AdjustmentsBody is b2Body, and obstacle_AdjustmentsFixture is b2Fixture.

HERE IS SHAPE FOR X:

 //OBSTACLE_ALPHABATE
    {
        CCSprite *sprite = [CCSprite spriteWithFile:@"X.png"];
        sprite.position = ccp(96,96);
        sprite.tag = 2;
        [self addChild:sprite];

        //SHAPE FIRST LEG OF X:
        {
            b2BodyDef bodyDefinition;
            bodyDefinition.type = b2_dynamicBody;
            bodyDefinition.position.Set(96/PTM_RATIO, 96/PTM_RATIO);
            bodyDefinition.userData = sprite;

            obstacle_AdjustmentsBody= world->CreateBody(&bodyDefinition);

            // Create body shape
            b2PolygonShape bodyShape;
            //row 1, col 1
            int num = 4;
            b2Vec2 verts[] = {
                b2Vec2(108.0f / PTM_RATIO, -71.5f / PTM_RATIO),
                b2Vec2(56.0f / PTM_RATIO, 2.5f / PTM_RATIO),
                b2Vec2(47.0f / PTM_RATIO, -3.5f / PTM_RATIO),
                b2Vec2(96.0f / PTM_RATIO, -75.5f / PTM_RATIO)
            };

            bodyShape.Set(verts, num);

            // Create shape definition and add to body
            b2FixtureDef bodyFixtureDefinition;
            bodyFixtureDefinition.shape = &bodyShape;
            bodyFixtureDefinition.density = 100.0f;
            bodyFixtureDefinition.friction = 0.01f;
            bodyFixtureDefinition.restitution = 0.50f;              
            obstacle_AdjustmentsFixture=obstacle_AdjustmentsBody->CreateFixture(&bodyFixtureDefinition);            
        }

        //SHAPE SECOND LEG OF X:
        {
            b2BodyDef bodyDefinition;
            bodyDefinition.type = b2_dynamicBody;
            bodyDefinition.position.Set(96/PTM_RATIO, 96/PTM_RATIO);
            bodyDefinition.userData = sprite;

            obstacle_AdjustmentsBody= world->CreateBody(&bodyDefinition);

            // Create body shape
            b2PolygonShape bodyShape;
            //row 1, col 1
            int num = 4;
            b2Vec2 verts[] = {
                b2Vec2(117.0f / PTM_RATIO, -4.5f / PTM_RATIO),
                b2Vec2(113.0f / PTM_RATIO, -1.5f / PTM_RATIO),
                b2Vec2(54.0f / PTM_RATIO, -51.5f / PTM_RATIO),
                b2Vec2(61.0f / PTM_RATIO, -56.5f / PTM_RATIO)
            };

            bodyShape.Set(verts, num);

            // Create shape definition and add to body
            b2FixtureDef bodyFixtureDefinition;
            bodyFixtureDefinition.shape = &bodyShape;
            bodyFixtureDefinition.density = 100.0f;
            bodyFixtureDefinition.friction = 0.01f;
            bodyFixtureDefinition.restitution = 0.50f;              
            obstacle_AdjustmentsFixture=obstacle_AdjustmentsBody->CreateFixture(&bodyFixtureDefinition);            
        }
}

Upvotes: 4

Related Questions