How do I implement deceleration for the player character?

Posted by tesselode on Game Development See other posts from Game Development or by tesselode
Published on 2012-10-07T15:09:04Z Indexed on 2012/10/07 21:56 UTC
Read the original article Hit count: 203

Filed under:
|

Using delta time with addition and subtraction is easy.

player.speed += 100 * dt

However, multiplication and division complicate things a bit. For example, let's say I want the player to double his speed every second.

player.speed = player.speed * 2 * dt

I can't do this because it'll slow down the player (unless delta time is really high). Division is the same way, except it'll speed things way up.

How can I handle multiplication and division with delta time?

Edit: it looks like my question has confused everyone. I really just wanted to be able to implement deceleration without this horrible mass of code:

else
    if speed > 0 then
        speed = speed - 20 * dt
        if speed < 0 then
            speed = 0
        end
    end
    if speed < 0 then
        speed = speed + 20 * dt
        if speed > 0 then
            speed = 0
        end
    end
end

Because that's way bigger than it needs to be. So far a better solution seems to be:

speed = speed - speed * whatever_number * dt

© Game Development or respective owner

Related posts about physics

Related posts about movement