Cocos2d - 如何使用Box2d创建过山车

时间:2012-07-14 09:01:22

标签: objective-c cocos2d-iphone box2d

我想制作像过山车这样的游戏并尝试为它制作火车..

我尝试用箱体创建火车

火车代码:

-(void)addTrainAtPos:(CGPoint)pos:(int)n:(CCLayer *)Layer
{
    b2BodyDef BodyDef;
    BodyDef.type = b2_dynamicBody;

    b2PolygonShape box;
    b2FixtureDef fixtureDef;

    fixtureDef.density = 0.5f;
    fixtureDef.friction = 0.0f;
    fixtureDef.restitution = 0.0f;

    box.SetAsBox(ptm(15), ptm(10));
    fixtureDef.shape = &box;

    b2Body *Body;
    b2Body *LastBody;

    b2DistanceJointDef Djoint;

    BodyDef.position.Set(ptm(pos.x - (0 * (30 + Spacing))), ptm(pos.y));
    BodyDef.fixedRotation = true;
    Body = world->CreateBody(&BodyDef);
    Body->CreateFixture(&fixtureDef);

    LastBody = Body;

    for (int i=1; i<n; i++) 
    {
        BodyDef.position.Set(ptm(pos.x - (i * (30 + Spacing))), ptm(pos.y));
        BodyDef.fixedRotation = true;
        Body = world->CreateBody(&BodyDef);
        Body->CreateFixture(&fixtureDef);

        //Djoint.Initialize(LastBody, Body, LastBody->GetWorldCenter(), Body->GetWorldCenter());
        //world->CreateJoint(&Djoint);
        //LastBody = Body;

    }

}

我还尝试在列车之间创建联合并尝试使用继续LinearVelocity

来移动它
-(void)MoveTrain
{
    for(NSValue *Val in Bodies)
    {
        b2Body *Body = (b2Body *)[Val pointerValue];
        b2Vec2 old = Body->GetLinearVelocity();
        if(old.x < 20)
            Body->SetLinearVelocity(b2Vec2(Speed, old.y));
    }
}

但我的问题是,当我跳上火车时,我对第一台发动机施加了力量,但我的整列火车没有完全跳跃,而是我试图在一段时间内一个接一个地对所有转向架施加力,但我也没有完全如此请给我任何相关的代码来创建火车并移动并完美地跳转它。

0 个答案:

没有答案