Placing b2body(with sprite) at some angle and move it along with the sprite in cocos2dx | Techbirds

Step1:Making the sprite at the angle where we want to place the b2body:Assuming rX and rY are the scaling factors

CCSprite *rect=CCSprite::create(“rect.png”); rect->setScaleX(rX); rect->setScaleY(rY); rect->setRotation(-7); rect->setPosition(ccp((winSize.width*0.5)/PTM_RATIO, (winSize.height*0.5)/PTM_RATIO)); this->addChild(rect,1);

CCSprite *rect=CCSprite::create(“rect.png”);

rect->setScaleX(rX);

rect->setScaleY(rY);

rect->setRotation(-7);

rect->setPosition(ccp((winSize.width*0.5)/PTM_RATIO, (winSize.height*0.5)/PTM_RATIO));

this->addChild(rect,1);

Step2:Then make the corresponding b2body.Here i am loading the body from the physics editor,therefore adding it using the GB2ShapeCache by adding the plist file.

GB2ShapeCache::sharedGB2ShapeCache()->addShapesWithFile(“dummy.plist”); void class_name::add body() { b2BodyDef bodyDef; bodyDef.type=b2_dynamicBody; bodyDef.userData = rect; MyPhysicsBody *body=(MyPhysicsBody*)world->CreateBody(&bodyDef); body->setTypeFlag(1); body->SetTransform(b2Vec2((winSize.width*0.5)/PTM_RATIO,(winSize.height*0.5) /PTM_RATIO), rect->getRotation()); // add the fixture definitions to the body GB2ShapeCache *sc = GB2ShapeCache::sharedGB2ShapeCache(); sc->addFixturesToBody(body,”body_name in plist file”, rect); rect->setAnchorPoint(sc->anchorPointForShape(“body_name in plist file”)); }

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

GB2ShapeCache::sharedGB2ShapeCache()->addShapesWithFile(“dummy.plist”);

void class_name::add body()

{

b2BodyDef bodyDef;

bodyDef.type=b2_dynamicBody;

bodyDef.userData = rect;

MyPhysicsBody *body=(MyPhysicsBody*)world->CreateBody(&bodyDef);

body->setTypeFlag(1);

body->SetTransform(b2Vec2((winSize.width*0.5)/PTM_RATIO,(winSize.height*0.5) /PTM_RATIO), rect->getRotation());

// add the fixture definitions to the body

GB2ShapeCache *sc = GB2ShapeCache::sharedGB2ShapeCache();

sc->addFixturesToBody(body,”body_name in plist file”, rect);

rect->setAnchorPoint(sc->anchorPointForShape(“body_name in plist file”));

}

Step3:Now make some changes in the update function which will rotate the sprite with the angular position of the b2body for which the sprite is assigned as the user data:

void class_name::update(float dt) { world->Step(dt, 10, 10); //Iterate over the bodies in the physics world for (b2Body* b = world->GetBodyList(); b; b = b->GetNext()) { if (b->GetUserData() != NULL) { //Synchronize the AtlasSprites position and rotation with the corresponding body CCSprite* myActor = (CCSprite*)b->GetUserData(); myActor->setPosition( CCPointMake( b->GetPosition().x * PTM_RATIO, b->GetPosition().y * PTM_RATIO) ); myActor->setRotation( -1 * CC_RADIANS_TO_DEGREES(b->GetAngle()) ); } } }

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

void class_name::update(float dt)

{

world->Step(dt, 10, 10);

//Iterate over the bodies in the physics world

for (b2Body* b = world->GetBodyList(); b; b = b->GetNext())

{

   if (b->GetUserData() != NULL)

   {

     //Synchronize the AtlasSprites position and rotation with the corresponding body

      CCSprite* myActor = (CCSprite*)b->GetUserData();

      myActor->setPosition( CCPointMake( b->GetPosition().x * PTM_RATIO, b->GetPosition().y * PTM_RATIO) );

      myActor->setRotation( -1 * CC_RADIANS_TO_DEGREES(b->GetAngle()) );

   }

}

}

1,451 total views, 1 views today

Share this Onfacebook-8399824twitter-4167783linkedin-7502726google-5522393 Tags: android, C++, cocos2dx, Game-development, ios