Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix air friction calculation #669

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open

Conversation

xi
Copy link

@xi xi commented Sep 24, 2018

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):

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

which can be converted to:

var frictionAir = 1 - body.frictionAir * deltaTimeScaled / body.mass;
velocity.x = velocityPrevX * frictionAir * correction + body.force.x / body.mass * deltaTimeSquared;

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
```
@getkey
Copy link

getkey commented Dec 26, 2018

Interesting, could this be the fix for #148?

@liabru
Copy link
Owner

liabru commented Sep 1, 2019

Thanks for raising this and sorry it's taken a while. I've been working on a wider scale update that touches on this and other related issues to do with timing and frame-rate, please see #777.

If you have any time, it would be great if you could try it and see if it fixes the problem raised here at least, thanks again.

@xi
Copy link
Author

xi commented Sep 2, 2019

As far as I can see, #777 only refactors the code but does not address the faulty equations as described in the pull request.

I will still try the refactoring sometime this week and give feedback.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants