0

在这里,我正在 box2d 中制作一个小游戏。在那我想要一个像 Lift 一样自动上下移动的身体。我使用此代码体尝试了此代码,当我触摸时可以移动。但我想将 y 位置 0 移动到 480,然后再次将 480 反转到 0。

enter code here

//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 *body = 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

    // Define the dynamic body fixture.
    b2FixtureDef fixtureDef;
    fixtureDef.shape = &dynamicBox; 
    fixtureDef.density = 1.0f;
    fixtureDef.friction = 0.3f;
    body->CreateFixture(&fixtureDef);
- (void)ccTouchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
    //Add a new body/atlas sprite at the touched location
    for( UITouch *touch in touches ) {
        CGPoint location = [touch locationInView: [touch view]];

        location = [[CCDirector sharedDirector] convertToGL: location];

        //[self addNewSpriteWithCoords: location];
        b2Vec2 force = b2Vec2(0, 20);
        _body->ApplyLinearImpulse(force, _body->GetPosition());
    }
}
4

1 回答 1

0

请参阅此处的关节部分:

http://www.box2d.org/manual.html

棱柱形接头可能对此有好处。此外,如果它应该是电梯/升降机,您可能需要为此考虑运动机构,但这实际上取决于您希望实现的目标。一定要检查身体部分的运动学和动态(和静态)身体之间的区别。

于 2013-06-26T14:16:50.967 回答