基本上我有2個多邊形爲我的身體。當我爲userData添加一個精靈時,紋理的位置不在我想要的位置。我想要做的是調整體內紋理的位置。這裏是我設置的地方的代碼示例:如何在box2d body中設置精靈的位置?
CCSpriteSheet *sheet = (CCSpriteSheet*) [self getChildByTag:kTagSpriteSheet];
CCSprite *pigeonSprite = [CCSprite spriteWithSpriteSheet:sheet rect:CGRectMake(0,0,40,32)];
[sheet addChild:pigeonSprite z:0 tag:kPigeonSprite];
pigeonSprite.position = ccp(p.x, p.y);
bodyDef.position.Set(p.x/PTM_RATIO, p.y/PTM_RATIO);
bodyDef.userData = sprite;
b2Body *body = world->CreateBody(&bodyDef);
b2CircleShape dynamicCircle;
dynamicCircle.m_radius = .25f;
dynamicCircle.m_p.Set(0.0f, 1.0f);
// Define the dynamic body fixture.
b2FixtureDef circleDef;
circleDef.shape = &dynamicCircle;
circleDef.density = 1.0f;
circleDef.friction = 0.3f;
body->CreateFixture(&circleDef);
b2Vec2 vertices[3];
vertices[0].Set(-0.5f, 0.0f);
vertices[1].Set(0.5f, 0.0f);
vertices[2].Set(0.0f, 1.0f);
b2PolygonShape triangle;
triangle.Set(vertices, 3);
b2FixtureDef triangleDef1;
triangleDef1.shape = ▵
triangleDef1.density = 1.0f;
triangleDef1.friction = 0.3f;
body->CreateFixture(&triangleDef1);
我可以建議嘗試從代碼片段中刪除所有不必要的東西嗎?例如,密度和摩擦與精靈定位幾乎沒有任何關係。就我而言,我不願意通過一大塊代碼來回答一個簡單的問題。另一方面,如果您不確定某件事情是否不會影響您的問題,那麼最好不要讓它出現。但請儘量降低到最低程度。 – Felixyz 2010-03-20 19:23:59