Page 2 of 2

Re: Smooth movement.

Posted: Fri Dec 18, 2015 8:13 pm
by Murii
micha wrote:The key idea of smooth movement is to change velocity gradually not in jumps.

Code: Select all

player.xvel = player.xvel * (1 - math.min(dt*player.friction, 1))
First, ignore the math.min and look at that:

Code: Select all

player.xvel = player.xvel * (1 - dt*player.friction)
this can be expanded and you get:

Code: Select all

player.xvel = player.xvel - player.xvel * dt * player.friction
That means that the player's x-velocity is changed by (player.xvel*dt*player.friction). This term defines how fast the player slows down. You can see, that if the player is very fast, then he slows down fast. If he is slow (player.xvel almost zero) then the slowing down is slow.
This piece of code:

Code: Select all

player.xvel = player.xvel * (1 - dt*player.friction)
is not equal to :

Code: Select all

player.xvel = player.xvel - player.xvel * dt * player.friction
but instead if you note the result of

Code: Select all

1 - dt*player.friction
with "y" :

Code: Select all

 player.xvel = player.xvel * y 
.

So this means that the final result is going to be totally different micha :)

Re: Smooth movement.

Posted: Fri Dec 18, 2015 9:54 pm
by pgimeno
Murii wrote:This piece of code:

Code: Select all

player.xvel = player.xvel * (1 - dt*player.friction)
is not equal to :

Code: Select all

player.xvel = player.xvel - player.xvel * dt * player.friction
It is. Due to distributive law, A*(B+C) equals (A*B)+(A*C), and similarly A*(B-C) equals (A*B)-(A*C). Here A=player.xvel, B=1, C=dt*player.friction. Therefore:

Code: Select all

player.xvel*(1 - dt*player.friction) = (player.xvel*1) - (player.xvel*dt*player.friction)
___________  _   __________________     ___________ _     ___________ __________________
     A       B           C                   A      B          A               C
which is the same as Micha said.

There can be differences in the results between the two formulas due to floating point precision, but we would be talking about differences in the 20th significant digit or so.