box2d:禁用碰撞弹跳

发布于 2024-09-26 10:57:23 字数 1363 浏览 0 评论 0原文

我有一些盒子,使用 box2d 创建,其恢复设置为零。但是当它们掉落时 一个接一个地出现弹跳事件。但我不希望这样...我希望它们在跌倒另一个时不会移动。如果我关闭重力就可以完成。但我也想要重力。这是我的代码,

UIImage *imageOfSnowV1 = [ UIImage imageNamed:[NSString stringWithFormat:@"Object%d.png",currentlySelected]];
    CCTexture2D  *texOfSnowV1 = [[ [CCTexture2D alloc] initWithImage:imageOfSnowV1 ] autorelease];
    CCSprite *sprite = [CCSprite spriteWithTexture:texOfSnowV1  rect:CGRectMake(0, 0, 32, 32)];
    [self addChild:sprite];
    sprite.position = ccp(p.x, p.y);
    sprite.tag=[temp intValue];


    // Define the dynamic body.
    //Set up a 1m squared box in the physics world

    b2BodyDef bodyDef;
    bodyDef.type = b2_dynamicBody;

    bodyDef.position.Set(p.x/PTM_RATIO, p.y/PTM_RATIO);
    bodyDef.userData = sprite;
    b2Body *bodyS = world->CreateBody(&bodyDef);

    // Define another box shape for our dynamic body.

    b2PolygonShape dynamicBox;
    dynamicBox.SetAsBox(.5f, .5f);//These are mid points for our 1m box




    b2MassData massData;
    massData.mass = 0.1f;
        bodyS->SetMassData(&massData);


    // Define the dynamic body fixture.
    b2FixtureDef fixtureDef;
    fixtureDef.shape = &dynamicBox; 
    fixtureDef.density = 50.0f;
    fixtureDef.restitution=0.0f;
    fixtureDef.friction = 0.01f;


    bodyS->CreateFixture(&fixtureDef);

任何人都可以帮我?

I have some box,creating using box2d,which restitution's are set to zero.but when they fall
over one another there appear bounce event.but i don't want that...i want them not move when fall over another.it can be done if i switch off gravity.but i also want gravity.here is my code

UIImage *imageOfSnowV1 = [ UIImage imageNamed:[NSString stringWithFormat:@"Object%d.png",currentlySelected]];
    CCTexture2D  *texOfSnowV1 = [[ [CCTexture2D alloc] initWithImage:imageOfSnowV1 ] autorelease];
    CCSprite *sprite = [CCSprite spriteWithTexture:texOfSnowV1  rect:CGRectMake(0, 0, 32, 32)];
    [self addChild:sprite];
    sprite.position = ccp(p.x, p.y);
    sprite.tag=[temp intValue];


    // Define the dynamic body.
    //Set up a 1m squared box in the physics world

    b2BodyDef bodyDef;
    bodyDef.type = b2_dynamicBody;

    bodyDef.position.Set(p.x/PTM_RATIO, p.y/PTM_RATIO);
    bodyDef.userData = sprite;
    b2Body *bodyS = world->CreateBody(&bodyDef);

    // Define another box shape for our dynamic body.

    b2PolygonShape dynamicBox;
    dynamicBox.SetAsBox(.5f, .5f);//These are mid points for our 1m box




    b2MassData massData;
    massData.mass = 0.1f;
        bodyS->SetMassData(&massData);


    // Define the dynamic body fixture.
    b2FixtureDef fixtureDef;
    fixtureDef.shape = &dynamicBox; 
    fixtureDef.density = 50.0f;
    fixtureDef.restitution=0.0f;
    fixtureDef.friction = 0.01f;


    bodyS->CreateFixture(&fixtureDef);

can anyone help me?

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

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

发布评论

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

评论(3

小苏打饼 2024-10-03 10:57:23

我记得 box2d 默认情况下使用碰撞对象的最大恢复,因此即使您将动态主体恢复设置为 0,如果静态主体有任何大于 0 的值,那么该恢复将用于碰撞,您可以修改 b2MixRestitution 函数来满足您的需求。

干杯,
克日什托夫·扎布沃茨基

As I remember box2d by default uses maximal restitution of colliding objects so even if you have dynamic body restitution set to 0 if the static body have any larger than 0 then that restitution will be used for collision, you could modify b2MixRestitution function to meet your needs.

Cheers,
Krzysztof Zabłocki

御守 2024-10-03 10:57:23

我最近也遇到了同样的问题。我的解决方案是,当您在接触侦听器中检测到新的碰撞时,只需将 Y 坐标清零即可。它在这里完美地发挥了作用。

I've been having the same problem lately. My solution, just zero-out the Y coordinate when you detect a new collision in your contact listener. It does the trick perfectly here.

无尽的现实 2024-10-03 10:57:23

你必须增加速度迭代和位置迭代。如果物体速度很快,它们就会重叠。所以你需要更好的计算。取决于身体数量,您可能会遇到性能问题,只需使用此值即可。

int32 velocityIterations = 10;
int32 positionIterations = 8;
world->Step( timeStep, velocityIterations, positionIterations ); 

you have to increase velocityIterations and positionIterations. If the bodies are fast they will overlap. so you need better calculation. depends on bodies count you might get perfomance issues, just play with this values.

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