身体不碰撞

发布于 2024-11-17 23:39:37 字数 2342 浏览 3 评论 0原文

我设置了2个物体,一个有重力的世界,一个带有步骤的刻度方法,所有的物体都移动得很好,我可以对它们施加力,重力也在起作用。

但没有碰撞。 当 body1 击中地面时,他只是走出屏幕,而不是击中它并像真正的球一样跳跃。 当 body1 击中 body2 时,就继续运动,因为什么也没有发生。

身体有形状,世界有边缘,但没有碰撞。 我在这里缺少什么?

以下是我从 init 调用的一些函数:

- (void)addBoxBodyForSprite:(CCSprite *)sprite {

    b2BodyDef spriteBodyDef;
    spriteBodyDef.type = b2_dynamicBody;
    spriteBodyDef.position.Set(sprite.position.x/PTM_RATIO,sprite.position.y/PTM_RATIO);
    spriteBodyDef.userData = sprite;
    spriteBody = world->CreateBody(&spriteBodyDef);

    b2PolygonShape spriteShape;
    spriteShape.SetAsBox(sprite.contentSize.width/PTM_RATIO/2,sprite.contentSize.height/PTM_RATIO/2);
    b2FixtureDef spriteShapeDef;
    spriteShapeDef.shape = &spriteShape;
    spriteShapeDef.density = 10.0;
    spriteShapeDef.isSensor = true;
    spriteBody->CreateFixture(&spriteShapeDef);

}


-(void)worldEdge
{
    CGSize winSize = [CCDirector sharedDirector].winSize;
    b2BodyDef groundBodyDef;
    groundBodyDef.position.Set(0,0);
    b2Body *groundBody = world->CreateBody(&groundBodyDef);
    b2PolygonShape groundBox;
    b2FixtureDef boxShapeDef;
    boxShapeDef.shape = &groundBox;
    groundBox.SetAsEdge(b2Vec2(0,0), b2Vec2(winSize.width/PTM_RATIO, 0));
    groundBody->CreateFixture(&boxShapeDef);
    groundBox.SetAsEdge(b2Vec2(0,0), b2Vec2(0, winSize.height/PTM_RATIO));
    groundBody->CreateFixture(&boxShapeDef);
    groundBox.SetAsEdge(b2Vec2(0, winSize.height/PTM_RATIO), b2Vec2(winSize.width/PTM_RATIO, winSize.height/PTM_RATIO));
    groundBody->CreateFixture(&boxShapeDef);
    groundBox.SetAsEdge(b2Vec2(winSize.width/PTM_RATIO, winSize.height/PTM_RATIO), b2Vec2(winSize.width/PTM_RATIO, 0));
    groundBody->CreateFixture(&boxShapeDef);
}




-(void)tick:(ccTime) dt
{


    world->Step(dt,10,10);
    for(b2Body *b=world->GetBodyList(); b; b=b->GetNext()) 
    {
        if(b->GetUserData() !=NULL )
           {
               CCSprite *sprite=(CCSprite *) b->GetUserData();//every b of the world will be update his position
               sprite.position=ccp( b->GetPosition().x * PTM_RATIO, b->GetPosition().y * PTM_RATIO  ) ;
               sprite.rotation=-1*CC_RADIANS_TO_DEGREES(b->GetAngle());
           }

    }

一切都进展顺利,但从未发生过碰撞。

i have set 2 bodies, a world with gravity,a tick method with step,and all the bodies are moving just fine,and i can apply forces on them, gravity is also working.

but no collision.
when body1 hit the ground he just go out of screen and not hit it and jump like a real ball.
when body1 hit body2 the just continue movement as nothing was happen.

the bodies have shape ,the world have edges, but no collision.
what am i missing here ?

here are some functions that i call from the init :

- (void)addBoxBodyForSprite:(CCSprite *)sprite {

    b2BodyDef spriteBodyDef;
    spriteBodyDef.type = b2_dynamicBody;
    spriteBodyDef.position.Set(sprite.position.x/PTM_RATIO,sprite.position.y/PTM_RATIO);
    spriteBodyDef.userData = sprite;
    spriteBody = world->CreateBody(&spriteBodyDef);

    b2PolygonShape spriteShape;
    spriteShape.SetAsBox(sprite.contentSize.width/PTM_RATIO/2,sprite.contentSize.height/PTM_RATIO/2);
    b2FixtureDef spriteShapeDef;
    spriteShapeDef.shape = &spriteShape;
    spriteShapeDef.density = 10.0;
    spriteShapeDef.isSensor = true;
    spriteBody->CreateFixture(&spriteShapeDef);

}


-(void)worldEdge
{
    CGSize winSize = [CCDirector sharedDirector].winSize;
    b2BodyDef groundBodyDef;
    groundBodyDef.position.Set(0,0);
    b2Body *groundBody = world->CreateBody(&groundBodyDef);
    b2PolygonShape groundBox;
    b2FixtureDef boxShapeDef;
    boxShapeDef.shape = &groundBox;
    groundBox.SetAsEdge(b2Vec2(0,0), b2Vec2(winSize.width/PTM_RATIO, 0));
    groundBody->CreateFixture(&boxShapeDef);
    groundBox.SetAsEdge(b2Vec2(0,0), b2Vec2(0, winSize.height/PTM_RATIO));
    groundBody->CreateFixture(&boxShapeDef);
    groundBox.SetAsEdge(b2Vec2(0, winSize.height/PTM_RATIO), b2Vec2(winSize.width/PTM_RATIO, winSize.height/PTM_RATIO));
    groundBody->CreateFixture(&boxShapeDef);
    groundBox.SetAsEdge(b2Vec2(winSize.width/PTM_RATIO, winSize.height/PTM_RATIO), b2Vec2(winSize.width/PTM_RATIO, 0));
    groundBody->CreateFixture(&boxShapeDef);
}




-(void)tick:(ccTime) dt
{


    world->Step(dt,10,10);
    for(b2Body *b=world->GetBodyList(); b; b=b->GetNext()) 
    {
        if(b->GetUserData() !=NULL )
           {
               CCSprite *sprite=(CCSprite *) b->GetUserData();//every b of the world will be update his position
               sprite.position=ccp( b->GetPosition().x * PTM_RATIO, b->GetPosition().y * PTM_RATIO  ) ;
               sprite.rotation=-1*CC_RADIANS_TO_DEGREES(b->GetAngle());
           }

    }

everything is moving allright but no collision ever.

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

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

发布评论

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

评论(1

浪菊怪哟 2024-11-24 23:39:37

具体问题是这一行:

spriteShapeDef.isSensor = true;

在box2d中,当你将fixture设置为传感器时,它不会与其他fixture发生碰撞。

The specific problem is this line:

spriteShapeDef.isSensor = true;

In box2d, when you set an fixture as a sensor, it will not collide with other fixtures.

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