0

当我遇到两个问题时,我正在“玩” kripken 的 box2d。我选择了这个前叉,因为它似乎是最快和最常用的。

  1. API 在 bodyDef 上定义了位置,但你“必须”在 body 上给出它。
  2. 力量,冲动,......保持附着在身体上,产生意想不到的恒定速度。

有人以前有这些行为吗?有人有任何提示吗?

这来自一个复杂的应用程序,但我已经简化了演示:

<html>
<head>
    <script src="http://kripken.github.io/box2d.js/box2d.js"></script>
</head>
<body>
<script>
    // gravity 0 for top view scene
    var world = new Box2D.b2World( new Box2D.b2Vec2(0, 0), true);

    var bodyDef = new Box2D.b2BodyDef();
    bodyDef.set_type( Box2D.b2_dynamicBody );
    bodyDef.set_position(40,40);

    var body = world.CreateBody(bodyDef);
    // ISSUE 1
    // without these two lines real position is 0,0
    body.GetPosition().set_x(40);
    body.GetPosition().set_y(40);

    var dynamicBox = new Box2D.b2PolygonShape();
    dynamicBox.SetAsBox(5.0, 5.0);

    var fixtureDef = new Box2D.b2FixtureDef();
    fixtureDef.set_shape(dynamicBox);
    fixtureDef.set_density(1);
    fixtureDef.set_friction( 0.8);
    fixtureDef.set_restitution( 0.5);

    body.CreateFixture(fixtureDef);

    //ISSUE 2 
    // Never ending movements
    //body.ApplyLinearImpulse(new Box2D.b2Vec2(5,5),body.GetWorldCenter());
    body.ApplyForce(new Box2D.b2Vec2(50,50),body.GetWorldCenter());

    function update() {
        world.Step(1/30, 10, 10);
        world.ClearForces();    
        console.log(body.GetPosition().get_x()+","+body.GetPosition().get_x());
    }

    setInterval(update, 1000/60);

</script>
</body>
</html>
4

2 回答 2

0

我遇到了更多问题,所以最后我切换到了 box2dweb。较旧但经过更多测试且更稳定。

于 2013-11-25T02:11:54.213 回答
0

对于问题 1,set_position 应该有一个 b2Vec2 参数。尝试这个:

bodyDef.set_position( new b2Vec2( 40, 40 ) );
于 2013-11-21T23:40:45.873 回答