【问题标题】:How to create move box2d body like Lift (body can move UP and Down automatically)如何创建像 Lift 一样的移动 box2d 身体(身体可以自动上下移动)
【发布时间】:2013-06-26 22:16:50
【问题描述】:

我在这里用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());
    }
}

【问题讨论】:

    标签: ios cocos2d-iphone box2d-iphone


    【解决方案1】:

    在此处查看关节部分:

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多