0

我是游戏世界的新手,我很喜欢做一个物理身体跳跃。
这就是我定义身体的方式

Cycle = [CCSprite spriteWithFile:@"Panda.png"];
        [self addChild:Cycle z:3];

    // Create a world
    b2Vec2 gravity = b2Vec2(0.0f, -8.0f);
    world = new b2World(gravity);

    // Create edges around the entire screen
    b2BodyDef groundBodyDef;
    groundBodyDef.position.Set(0,0);

    b2Body *groundBody = world->CreateBody(&groundBodyDef);
    b2EdgeShape groundEdge;
    b2FixtureDef boxShapeDef;
    boxShapeDef.shape = &groundEdge;

    //wall definitions
    groundEdge.Set(b2Vec2(0,0), b2Vec2(screenSize.width/PTM_RATIO, 0));
    groundBody->CreateFixture(&boxShapeDef);

    groundEdge.Set(b2Vec2(0,0), b2Vec2(0,screenSize.height/PTM_RATIO));
    groundBody->CreateFixture(&boxShapeDef);

    groundEdge.Set(b2Vec2(0, screenSize.height/PTM_RATIO),
                   b2Vec2(screenSize.width/PTM_RATIO, screenSize.height/PTM_RATIO));
    groundBody->CreateFixture(&boxShapeDef);

    groundEdge.Set(b2Vec2(screenSize.width/PTM_RATIO, screenSize.height/PTM_RATIO),
                   b2Vec2(screenSize.width/PTM_RATIO, 0));
    groundBody->CreateFixture(&boxShapeDef);

    // Create ball body and shape
    b2BodyDef ballBodyDef;
    ballBodyDef.type = b2_dynamicBody;
    ballBodyDef.position.Set(300/PTM_RATIO,100/PTM_RATIO);
    ballBodyDef.userData = Cycle;
    body = world->CreateBody(&ballBodyDef);


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

    // Define the dynamic body fixture.
    b2FixtureDef fixtureDef;
    fixtureDef.shape = &dynamicBox;
    fixtureDef.density = 1.0f;
    fixtureDef.friction = 0.3f;
    body->CreateFixture(&fixtureDef);

并开始接触,我正在应用线性脉冲作为

b2Vec2 force = b2Vec2(30, 30);
body-> ApplyLinearImpulse(body->GetPosition(),force);

所以任何人都可以告诉我我做错了什么..
提前谢谢..

4

3 回答 3

1

阅读 iForce2D 的那篇文章。这应该解释得差不多了。

iForce2D - Box2D 教程 - 跳跃

于 2013-09-05T14:26:53.453 回答
0

你可以通过像这样改变身体的 Y 速度来跳跃

    b2Vec2 velocity = body_->GetLinearVelocity();
    velocity.y = 20;
    velocity.x = 0;//upwards - don't change x velocity
    body_->SetLinearVelocity( velocity );
于 2014-12-17T09:58:23.000 回答
0

记住你的精灵和身体的位置(bodyDef)应该是相同的,在这段代码中你的精灵的位置没有显示出来。似乎这是造成问题的原因。在此之后使用 setLinearVelicoty() 或 ApplylinearForce() 方法,而不是使用 ApplyLinearImpulse()。

于 2014-03-17T07:29:58.490 回答