0
0
Fork 0
mirror of https://github.com/liabru/matter-js.git synced 2025-02-07 19:06:26 -05:00

fix air friction calculation

The code is slghtliy confusing because `velocityPrevX` and
`velocityPrevY` actually define a distance. The real velocity can be
calculated as `velocityPrevX / deltaTimePrev` and `velocityPrev /
deltaTimePrev` where `deltaTimePrev = deltaTimeScaled / correction`.

This is the formular without air friction:

```
velocity.x = velocityPrevX * correction + body.force.x / body.mass * deltaTimeSquared
```

Air friction is a force that acts in the opposite direction of the
velocity. It is proportional to velocity (for small velocity):

```
velocity.x = velocityPrevX * correction + (body.force.x - (body.frictionAir * velocityPrevX / deltaTimeScaled * correction)) / body.mass * deltaTimeSquared
```

which can be converted to:

```
velocity.x = velocityPrevX * (1 - body.frictionAir * deltaTimeScaled / body.mass) * correction + body.force.x / body.mass * deltaTimeSquared
```
This commit is contained in:
Tobias Bengfort 2018-09-24 22:06:25 +02:00
parent 7894b4b44d
commit 3a4361e5e6

View file

@ -602,10 +602,11 @@ var Axes = require('../geometry/Axes');
* @param {number} correction
*/
Body.update = function(body, deltaTime, timeScale, correction) {
var deltaTimeSquared = Math.pow(deltaTime * timeScale * body.timeScale, 2);
var deltaTimeScaled = deltaTime * timeScale * body.timeScale;
var deltaTimeSquared = Math.pow(deltaTimeScaled, 2);
// from the previous step
var frictionAir = 1 - body.frictionAir * timeScale * body.timeScale,
var frictionAir = 1 - body.frictionAir * deltaTimeScaled / body.mass,
velocityPrevX = body.position.x - body.positionPrev.x,
velocityPrevY = body.position.y - body.positionPrev.y;