Box2D,Lock body在一定位置(无法让SetPosition和SetTransform工作)

时间:2012-09-25 18:42:53

标签: box2d game-physics easeljs createjs

我正在使用Box2D来模拟一个小世界,用户可以拖动对象并将它们“粘贴”在某些预定位置(基本上将它们“锁定”在某个位置)。

我有这个代码来锁定某个位置的对象。一切正常,我不能让SetPosition(也不是SetTransform)工作。他们只是将对象移动到(0,0)。

仅供参考,使用EaselJS绘制世界。

// checks if mouse is dragging the object nearby one of the containers
if( isWithin(mouseX, mouseY, containers) ) {

    // make object 'straight'
    body.SetAngularVelocity(0);
    body.SetAngle(0);

    // makes the object a kinetic body
    body.SetType(b2Body.b2_kineticBody);

    // doesn't work. it always moves the object to (0,0)
    body.SetPosition(5,5); // I am using (5,5) for simplicity
                           // it should have the coordinates of the center of the container
    // alternative: (also doesn't work)
    //body.SetTransform(b2Vec2(5,5), body.GetAngle());
}

我做错了什么?

2 个答案:

答案 0 :(得分:0)

您的代码对我来说很合适。 这是我的应用程序中的动态正文代码。 不知道你的情况是否有意义。

        b2Vec2 delta = that->settings->initPos - that->ballBody->GetPosition();
        delta *= that->ballBody->GetMass();
        that->ballBody->ApplyLinearImpulse(delta, that->ballBody->GetPosition());

答案 1 :(得分:0)

尝试使用

body->SetTransform(b2Vec2(5,5), body->GetAngle());