我有一些成长的身体。我把这个身体添加到世界各地。
...
someBody = world->CreateBody(&bodyDef);
someFixture = tapBody->CreateFixture(&someFixtureDef);
...
我想我不需要在这里粘贴所有代码。
所以我给世界增添了身体。问题是如何改变已经存在于世界的身体的位置,恢复原状?我可以这样做这里吗? (因为当我尝试在tick
方法中更改某些内容时遇到错误。)
-(void) tick: (ccTime) dt
{
world->Step(dt, velocityIterations, positionIterations);
for (b2Body* b = world->GetBodyList(); b; b = b->GetNext())
{
}
}
答案 0 :(得分:1)
首先在tick方法中确保设置速度和位置迭代,然后查看所有改变它们的实体:
-(void)update:(ccTime)dt {
int32 velocityIterations = 8;
int32 positionIterations = 3;
world->Step(dt, velocityIterations, positionIterations);
for(b2Body *b = world->GetBodyList(); b != NULL; b = b->GetNext()) {
//Do something with the body for example: b->ApplyLinearImpulse...();
}
}
查看box2d文档中移动box2d主体的方法,例如 - &gt; ApplyLinearImpuse - &gt; ApplyForce ...如果要显式设置位置和角度,请查看 - &gt; SetTransform()< / p>
我希望这有帮助! 噬细胞
答案 1 :(得分:1)
也许不是破坏和重建整个身体,而是通过摧毁和创造新的装置来完成你所需要的......
for (b2Body* b = world->GetBodyList(); b; b = b->GetNext())
{
// This assumes you only have one fixture
b2Fixture* f = b->GetFixtureList();
f = f->GetNext();
// Code here to create a new fixture/shape with different size (or whatever)
// Destory old fixture and create new one
b->DestoryFixture(f);
b2Fixture* someFixture = b->CreateFixture(&someFixtureDef);
}
对不起任何错别字......没有测试任何这个......