我正在使用cocos2d-x的box2d。
像这张照片一样,我的char具有盒形主体,
击中我的char时会旋转。
但是我希望我的char的身体不旋转。
我知道精灵不会旋转。
但是我不知道如何固定身体的旋转。
myChar的代码
void HelloWorld::makeMe()
{
Sprite* pSprite = Sprite::create("shipBeige_manned.png");
pSprite->setPosition(Vec2(100,100));
this->addChild(pSprite);
b2BodyDef bodyDef;
bodyDef.type = b2_dynamicBody;
bodyDef.position.Set(100 / PTM_RATIO, 100 / PTM_RATIO);
bodyDef.userData = pSprite;
b2Body* body = _world->CreateBody(&bodyDef);
b2PolygonShape pho;
pho.SetAsBox(1.24f, 1.22f);
b2FixtureDef fixtureDef;
fixtureDef.shape = &pho;
fixtureDef.density = 1.0f;
fixtureDef.friction = 0.2f;
fixtureDef.restitution = 0.0f;
body->CreateFixture(&fixtureDef);
pSprite->setName("me");
}
更新(刻度)代码
void HelloWorld::tick(float dt)
{
int velocityIterations = 8;
int positionIterations = 3;
_world->Step(dt, velocityIterations, positionIterations);
for (b2Body* b = _world->GetBodyList(); b; b = b->GetNext())
{
if (b->GetUserData() != nullptr) {
Sprite* spriteData = (Sprite *)b->GetUserData();
spriteData->setPosition(Vec2(b->GetPosition().x * PTM_RATIO,
b->GetPosition().y * PTM_RATIO));
spriteData->setRotation(-1 * CC_RADIANS_TO_DEGREES(b->GetAngle()));
}
}
}
答案 0 :(得分:0)
我对Char的身体使用SetFixedRotation。
void HelloWorld::makeMe()
{
Sprite* pSprite = Sprite::create("shipBeige_manned.png");
pSprite->setPosition(Vec2(100,100));
this->addChild(pSprite);
b2BodyDef bodyDef;
bodyDef.type = b2_dynamicBody;
bodyDef.position.Set(100 / PTM_RATIO, 100 / PTM_RATIO);
bodyDef.userData = pSprite;
b2Body* body = _world->CreateBody(&bodyDef);
body->SetFixedRotation(true); <----add this code
b2PolygonShape pho;
pho.SetAsBox(1.24f, 1.22f);
b2FixtureDef fixtureDef;
fixtureDef.shape = &pho;
// 밀도
fixtureDef.density = 1.0f;
// 마찰력 - 0 ~ 1
fixtureDef.friction = 0.2f;
// 반발력 - 물체가 다른 물체에 닿았을때 팅기는 값
fixtureDef.restitution = 0.0f;
body->CreateFixture(&fixtureDef);
pSprite->setName("me");
}