Search code examples
box2dcocos2d-x

CCSprite not positioned on top of b2body


I am having trouble in positioning the ccsprite on b2body.

I want to align ccsprite on box2d body. Right now its having a gap in between them, i dont want that gap.

I am using GB2ShapeCache by Andreas Loew, for getting fixtures on body.

enter image description here

PTM_RATIO = 32

Here is my code:

string stageName="Stage1";

CCSize s = CCDirector::sharedDirector()->getWinSize();
CCPoint center=ccp((s.width/2),(s.height/2));

CCSprite *sprite = new CCSprite();
sprite->initWithFile("Stage1.png");
sprite->setAnchorPoint(CCPoint(0.50, 0.5));
sprite->setPosition(center);
this->addChild(sprite);

// Define the dynamic body.
//Set up a 1m squared box in the physics world
b2BodyDef bodyDef;
bodyDef.type = b2_dynamicBody;
bodyDef.userData=sprite;
bodyDef.position.Set(center.x/PTM_RATIO,center.y/PTM_RATIO);
//bodyDef.position=b2Vec2(10, 10);

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

//adding fixtudre to body
GB2ShapeCache::sharedGB2ShapeCache()->addFixturesToBody(body, "Stage1");

//weld joint at center
b2WeldJointDef *weldJoint= new b2WeldJointDef();
weldJoint->Initialize(groundBody, body, b2Vec2(center.x/PTM_RATIO,center.y/PTM_RATIO));
world->CreateJoint(weldJoint);

Solution

  • I find the solution that my sprite image was having trailing spaces on all the 4 sides. Once i removed it and re-build the polygon using physic editor my problem got solved. Code is perfectly working alright Problem was with image