3

我是physicsjs的新手,并且正在创建一些测试模拟以熟悉该库。

我想模拟多个在屏幕上滑动的盒子,它们都经历不同程度的摩擦。到目前为止,我有 3 个框从屏幕的左边框开始,并且都有一个 pos xvel。我不确定在模拟中增加摩擦的最佳方法是什么。所有 3 个盒子都不应受到摩擦的相同影响。因此,我需要某种方式将通用摩擦算法应用于所有盒子,但是摩擦量需要取决于它当前作用于哪个盒子。

4

2 回答 2

3

摩擦是内置的(但它不是最好的算法)。

只需使用:

Physics.body('rectangle', {
     width: 40,
     height: 40,
     cof: 0.5, // <-- change the friction. range [0, 1]
     x: ...
     ...
});
于 2014-05-15T16:19:06.783 回答
0

空的空间摩擦可以通过这种方式完成。无论球在做什么,它都在减速。

它侦听滴答事件,然后在每次更新时通过摩擦量降低速度。

```

function applyFriction(obj, friction) {
    // invert it so that 0.1 friction is a multiplication of 0.9 to x.
    // it makes more sense to apply 0.1 as friction that way.
    var value = 1 - friction;
    obj.state.vel.set(obj.state.vel.x * value, obj.state.vel.y * value);
}

// subscribe to the ticker
Physics.util.ticker.on(function (time) {
    applyFriction(ball, 0.1);// apply friction to the ball.
    world.step(time);
});

// start the ticker
Physics.util.ticker.start();

```

于 2016-02-25T15:39:10.030 回答