如何手动改变物体的特性?

发布于 2025-01-07 02:00:12 字数 522 浏览 0 评论 0原文

我有一些正在成长的身体。我把这个身体添加到了世界上。

...
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())
    {

    }
}

I have some body which grows. I added this body to the world.

...
someBody = world->CreateBody(&bodyDef);
someFixture = tapBody->CreateFixture(&someFixtureDef);
...

I think I do not need to paste all code here.

So I added body to the world. The question is how to change position, restitution... of the body which is already in the world? Am I allowed to do that here? (because I am getting errors when I try to change something in the tick method).

-(void) tick: (ccTime) dt
{
    world->Step(dt, velocityIterations, positionIterations);
    for (b2Body* b = world->GetBodyList(); b; b = b->GetNext())
    {

    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

森罗 2025-01-14 02:00:12

也许您可以通过破坏并创建一个新的固定装置来完成您所需要的,而不是破坏和重新创建整个身体...

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);

}

对于任何错字抱歉...还没有测试过任何这些...

Maybe instead of destroying and recreating the entire body you could accomplish what you need by destroying and creating a new fixture...

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);

}

Sorry for any typos... haven't tested any of this...

余生再见 2025-01-14 02:00:12

首先,在勾选方法中,确保您正在设置速度和位置迭代,然后查看所有改变它们的物体:

-(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 物体的方法,例如 ->ApplyLinearImpuse ->ApplyForce ...如果您想显式设置位置和角度,请查看 ->SetTransform()

我希望这会有所帮助!
塔姆斯

First in the tick method make sure that you are setting the velocity and position iterations and then look through all of the bodies altering them:

-(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...();
    }
}

Look at the methods in box2d documentation to move a box2d body, for example->ApplyLinearImpuse ->ApplyForce...If you want to explicitly set the position and angle, then look at ->SetTransform()

I hope this helps!
Tams

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文