Smooth movement. (SOLVED)

Questions about the LÖVE API, installing LÖVE and other support related questions go here.
Forum rules
Before you make a thread asking for help, read this.
User avatar
Murii
Party member
Posts: 216
Joined: Fri Jul 05, 2013 9:58 am
Location: Arad, Romania
Contact:

Re: Smooth movement.

Post 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 :)
User avatar
pgimeno
Party member
Posts: 3674
Joined: Sun Oct 18, 2015 2:58 pm

Re: Smooth movement.

Post 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.
Post Reply

Who is online

Users browsing this forum: Ahrefs [Bot] and 9 guests