最近在学习Box2d,遇到一个问题,就是创建的sprite的位置和b2body的位置不一致,所以想请大大们帮忙看看。相关部分代码.
Sprite* HelloWorld::createPhySprite(b2World* pWorld){
//创建一个测试sprite
auto pSp = Sprite::create("CloseNormal.png");
pSp->setAnchorPoint(Vec2::ZERO);
pSp->setPosition(Vec2(Director::getInstance()->getVisibleSize()/2));
// pSp->setContentSize(Size(1*PIXEL_TRANS_RADIO, 1*PIXEL_TRANS_RADIO));
// pSp->gets
//pSp->setTextureRect(Rect(0,0,pSp->getContentSize().width,pSp->getContentSize().height));
pSp->setTag(TAG_TEST);
//创建body描述
b2BodyDef* pBd = new b2BodyDef();
pBd->active = true;
pBd->type = b2BodyType::b2_dynamicBody;
pBd->bullet = false;
pBd->position.Set(pSp->getPosition().x/PIXEL_TRANS_RADIO, pSp->getPosition().y/PIXEL_TRANS_RADIO);
//创建fixture描述
b2FixtureDef* pFd = new b2FixtureDef();
b2PolygonShape* pRect = new b2PolygonShape();
//setAsBox设置的是半高和半宽
pRect->SetAsBox((pSp->getContentSize().width/2)/PIXEL_TRANS_RADIO,(pSp->getContentSize().height/2)/PIXEL_TRANS_RADIO);
pFd->shape = pRect;
b2Body *pB = pWorld->CreateBody(pBd);
pB->CreateFixture(pFd);
pB->SetUserData(pSp);
return pSp;
}
PS:其中的PIXEL_TRANS_RADIO为像素到米的转换值,值是30
最后的效果图如下:
可以看到DebugDraw得到的b2body位置和Sprite的位置有明显的偏差,所以想请求一下帮助,谢谢!