Box2D动态机身掉下屏幕

时间:2011-07-01 01:21:29

标签: objective-c ios cocoa-touch physics box2d

我基本上有一个长方形物体(像长矛),我有一个地面体。问题是,当长矛撞击地面时,它不会反弹,它会掉落在屏幕上。在这里我的物理设置:(忽略球参考,它假设被称为矛(矩形))

-(id) init {

    if( (self=[super init])) {

        CGSize winSize = [CCDirector sharedDirector].winSize;

        self.isAccelerometerEnabled = YES;
        self.isTouchEnabled = YES;

        // Create sprite and add it to the layer
        _ball = [CCSprite spriteWithFile:@"SPEAR.png" rect:CGRectMake(0, 0, 100, 10)];
        _ball.position = ccp(100, 100);
        [self addChild:_ball];

        // Create a world
        b2Vec2 gravity = b2Vec2(0.0f, -20.0f);
        bool doSleep = true;
        _world = new b2World(gravity, doSleep);

        // Create edges around the entire screen
        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);

        // Create ball body and shape
        b2BodyDef ballBodyDef;
        ballBodyDef.type = b2_dynamicBody;
        ballBodyDef.position.Set(100/PTM_RATIO, 100/PTM_RATIO);
        ballBodyDef.userData = _ball;
        b2Body *_body = _world->CreateBody(&ballBodyDef);


        b2PolygonShape spearShape;
        spearShape.SetAsBox(100/PTM_RATIO, 10/PTM_RATIO);
        b2FixtureDef ballShapeDef;
        ballShapeDef.shape = &spearShape;


        ballShapeDef.density = 1.0f;
        ballShapeDef.friction = 0.9f;
        ballShapeDef.restitution = 0.89f;

        b2Vec2 force;
        force.Set(_body->GetLinearVelocity().x+5.0f, _body->GetLinearVelocity().y+10.0f);
        _body->SetLinearVelocity(force);

        [self schedule:@selector(tick:)];
    }
    return self;
}

- (void)tick:(ccTime) dt {

    _world->Step(dt, 10, 10);
    for(b2Body *b = _world->GetBodyList(); b; b=b->GetNext()) {    
        if (b->GetUserData() != NULL) {
            CCSprite *ballData = (CCSprite *)b->GetUserData();
            ballData.position = ccp(b->GetPosition().x * PTM_RATIO,
                                    b->GetPosition().y * PTM_RATIO);
            ballData.rotation = -1 * CC_RADIANS_TO_DEGREES(b->GetAngle());
        }        
    }
}

4 个答案:

答案 0 :(得分:1)

我没有看到你的代码中的任何地方将形状附加到球和矛。如果身体没有形状 - 它不会碰撞。

答案 1 :(得分:1)

我认为只有灯具在box2d中发生碰撞。 但是你的矛没有固定装置。 您只能创建夹具def而不是夹具本身。

将恢复原状设置为fixtureDef后,添加以下行:

_body->CreateFixture(&ballShapeDef);

一切都应该没问题!

我知道有点晚了。

答案 2 :(得分:0)

我有一段时间没有使用过cocos2d,但是我记得你需要创建一个空间来添加对象,所以他们彼此都知道了。

答案 3 :(得分:0)

Box2D中的边缘装置不会相互碰撞。使地面或矛成为多边形。