使用 box2d cocos2d 不会改变体重

发布于 2024-12-29 09:59:26 字数 916 浏览 1 评论 0原文

我只是不明白这个大东西是如何运作的。 我已经阅读了 box2d 文档(其中有许多目前 cocos2d 中没有的命令),我试图理解大量的事情。

我知道如果我改变身体密度,它不会改变任何事情:

    //add body
b2BodyDef spriteBodyDef;
spriteBodyDef.type =b2_dynamicBody; //b2_staticBody;
spriteBodyDef.position.Set(pilot1.position.x/PTM_RATIO,pilot1.position.y/PTM_RATIO);
spriteBodyDef.userData = pilot1;
pilot1Body = world->CreateBody(&spriteBodyDef);    
b2CircleShape spriteShape; //b2polygon-for box shape
spriteShape.m_radius = 15.0/PTM_RATIO;
b2FixtureDef spriteShapeDef;
spriteShapeDef.restitution = 0.4f;
spriteShapeDef.shape = &spriteShape;
//spriteShapeDef.density=10.0f;

b2Fixture *fix=pilot1Body->GetFixtureList();
fix->SetDensity(1.5f);
pilot1Body->ResetMassData();
NSLog(@"mass2: %f", pilot1Body->GetMass());  
spriteShapeDef.isSensor = false;
pilot1Body->CreateFixture(&spriteShapeDef);
    [self addChild:pilot1];

非常感谢。

i just cant figure out how this mass thing works .
i have read the box2d docs, (which have many commands that are not in the cocos2d at the moment) and i was trying to understand the mass thing.

i know that if i change the body density it doesnt change a thing :

    //add body
b2BodyDef spriteBodyDef;
spriteBodyDef.type =b2_dynamicBody; //b2_staticBody;
spriteBodyDef.position.Set(pilot1.position.x/PTM_RATIO,pilot1.position.y/PTM_RATIO);
spriteBodyDef.userData = pilot1;
pilot1Body = world->CreateBody(&spriteBodyDef);    
b2CircleShape spriteShape; //b2polygon-for box shape
spriteShape.m_radius = 15.0/PTM_RATIO;
b2FixtureDef spriteShapeDef;
spriteShapeDef.restitution = 0.4f;
spriteShapeDef.shape = &spriteShape;
//spriteShapeDef.density=10.0f;

b2Fixture *fix=pilot1Body->GetFixtureList();
fix->SetDensity(1.5f);
pilot1Body->ResetMassData();
NSLog(@"mass2: %f", pilot1Body->GetMass());  
spriteShapeDef.isSensor = false;
pilot1Body->CreateFixture(&spriteShapeDef);
    [self addChild:pilot1];

thanks a lot.

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

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

发布评论

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

评论(1

热血少△年 2025-01-05 09:59:26

您需要使用b2Fixture::SetDensity为附加到主体的部分或全部灯具设置新的密度,然后调用b2Body::ResetMassData,例如:

    NSLog(@"mass1: %f", body->GetMass());
    b2Fixture *fix = body->GetFixtureList();
    while (fix) {
        fix->SetDensity(1.5f);
        fix = fix->GetNext();
    }
    body->ResetMassData();
    NSLog(@"mass2: %f", body->GetMass());

You need to set new density for some or all the fixtures attached to the body using b2Fixture::SetDensity and then call b2Body::ResetMassData, for example:

    NSLog(@"mass1: %f", body->GetMass());
    b2Fixture *fix = body->GetFixtureList();
    while (fix) {
        fix->SetDensity(1.5f);
        fix = fix->GetNext();
    }
    body->ResetMassData();
    NSLog(@"mass2: %f", body->GetMass());
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文