Reputation: 2061
I am new to physicsjs and am creating some test simulations in order to become familiar with the library.
I want to simulate multiple boxes sliding across the screen which all experience different degrees of friction. So far I have 3 boxes which start on the left border of the screen and all have a pos xvel. I am unsure what the best approach is to add friction to the simulation. all 3 boxes should not be affected the same way by friction. Therefor i need some way of applying a general friction algorithm to all the boxs, however the amount of friction needs to depend on what box it is currently acting on.
Upvotes: 3
Views: 784
Reputation: 156
Empty space friction can be done this way. No matter what the ball is doing it is slowing down.
It listens to the tick event and then on every update it reduces the velocity by the amount of friction.
```
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();
```
Upvotes: 0
Reputation: 1193
Friction is built in (but it's not the greatest algorithm).
Just use:
Physics.body('rectangle', {
width: 40,
height: 40,
cof: 0.5, // <-- change the friction. range [0, 1]
x: ...
...
});
Upvotes: 3